-
Notifications
You must be signed in to change notification settings - Fork 121
/
UrlController.php
92 lines (78 loc) · 2.37 KB
/
UrlController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreUrl;
use App\Models\Url;
use App\Models\Visit;
use App\Services\QrCodeService;
use App\Services\UHubLinkService;
use App\Test;
class UrlController extends Controller
{
/**
* UrlController constructor.
*/
public function __construct(
public Url $url,
public UHubLinkService $uHubLinkService,
) {
$this->middleware('urlhublinkchecker')->only('create');
}
/**
* Shorten long URLs.
*
* @param StoreUrl $request \App\Http\Requests\StoreUrl
* @return \Illuminate\Http\RedirectResponse
*/
public function create(StoreUrl $request)
{
$url = $this->uHubLinkService->create($request);
return to_route('su_detail', $url->keyword);
}
/**
* View the shortened URL details.
*
* @param string $urlKey A unique key to identify the shortened URL
* @return \Illuminate\Contracts\View\View
*/
public function showDetail(string $urlKey)
{
$url = Url::with('visits')->whereKeyword($urlKey)->firstOrFail();
$data = [
'url' => $url,
'visit' => app(Visit::class),
];
if (config('urlhub.qrcode')) {
$qrCode = app(QrCodeService::class)->execute($url->short_url);
$data = array_merge($data, ['qrCode' => $qrCode]);
}
return view('frontend.short', $data);
}
/**
* Delete a shortened URL on user request.
*
* @param Url $hash_id \App\Models\Url
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function delete(Url $hash_id)
{
$this->authorize('forceDelete', $hash_id);
$hash_id->delete();
return to_route('home');
}
/**
* UrlHub only allows users (registered & unregistered) to have a unique
* link. You can duplicate it and it will generated a new unique random
* key.
*
* @param string $urlKey A unique key to identify the shortened URL
* @return \Illuminate\Http\RedirectResponse
*/
public function duplicate(string $urlKey)
{
$this->uHubLinkService->duplicate($urlKey);
return to_route('su_detail', $this->uHubLinkService->new_keyword)
->withFlashSuccess(__('The link has successfully duplicated.'));
}
}