|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Linkeys\UrlSigner; |
|
4
|
|
|
|
|
5
|
|
|
use \DateTime; |
|
6
|
|
|
use Linkeys\UrlSigner\Contracts\UrlSigner as UrlSignerContract; |
|
7
|
|
|
use Linkeys\UrlSigner\Contracts\Models\Link as LinkContract; |
|
8
|
|
|
use Linkeys\UrlSigner\Models\Link; |
|
|
|
|
|
|
9
|
|
|
use Linkeys\UrlSigner\Support\GroupRepository\GroupRepository; |
|
10
|
|
|
use Linkeys\UrlSigner\Support\LinkRepository\LinkRepository; |
|
11
|
|
|
use Linkeys\UrlSigner\Support\UrlManipulator\UrlManipulator; |
|
12
|
|
|
|
|
13
|
|
|
class UrlSigner implements UrlSignerContract |
|
14
|
|
|
{ |
|
15
|
|
|
protected $linkRepository; |
|
16
|
|
|
|
|
17
|
|
|
protected $urlParser; |
|
18
|
|
|
|
|
19
|
|
|
protected $queryParameter = 'uuid'; |
|
20
|
|
|
|
|
21
|
|
|
private $group; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var GroupRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
private $groupRepository; |
|
26
|
|
|
|
|
27
|
6 |
|
public function __construct(LinkRepository $linkRepository, GroupRepository $groupRepository, UrlManipulator $urlParser) |
|
28
|
|
|
{ |
|
29
|
6 |
|
$this->linkRepository = $linkRepository; |
|
30
|
6 |
|
$this->urlParser = $urlParser; |
|
31
|
6 |
|
$this->groupRepository = $groupRepository; |
|
32
|
6 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a new link |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $url |
|
38
|
|
|
* @param array|null $data |
|
39
|
|
|
* @param DateTime|int|string|null $expiry |
|
40
|
|
|
* @param null $clickLimit |
|
|
|
|
|
|
41
|
|
|
* @return Link |
|
42
|
|
|
* @throws \Exception |
|
43
|
|
|
*/ |
|
44
|
5 |
|
public function generate(string $url, $data = [], $expiry = null, $clickLimit = null) : LinkContract |
|
45
|
|
|
{ |
|
46
|
5 |
|
$link = $this->linkRepository->create([ |
|
47
|
5 |
|
'url' => $url, |
|
48
|
5 |
|
'data' => $data, |
|
49
|
5 |
|
'click_limit' => $clickLimit, |
|
50
|
5 |
|
'expiry' => $expiry |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
5 |
|
if($this->group !== null) { |
|
54
|
2 |
|
$this->groupRepository->pushLink($this->group, $link); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
5 |
|
return $link; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function sign(string $url, $data = [], $expiry = null, $clickLimit = null) : LinkContract |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->generate($url, $data, $expiry, $clickLimit); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
public function group(callable $callback, $expiry = null, $clickLimit = null) |
|
66
|
|
|
{ |
|
67
|
3 |
|
$group = $this->groupRepository->create([ |
|
68
|
3 |
|
'expiry' => $expiry, |
|
69
|
3 |
|
'clickLimit' => $clickLimit |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
3 |
|
$this->group = $group; |
|
73
|
3 |
|
$callback($this); |
|
74
|
3 |
|
$this->group = null; |
|
75
|
|
|
|
|
76
|
3 |
|
return $group; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: