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
|
5 |
|
public function __construct(LinkRepository $linkRepository, GroupRepository $groupRepository, UrlManipulator $urlParser) |
28
|
|
|
{ |
29
|
5 |
|
$this->linkRepository = $linkRepository; |
30
|
5 |
|
$this->urlParser = $urlParser; |
31
|
5 |
|
$this->groupRepository = $groupRepository; |
32
|
5 |
|
} |
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
|
4 |
|
public function generate(string $url, $data = [], $expiry = null, $clickLimit = null) : LinkContract |
45
|
|
|
{ |
46
|
4 |
|
$link = $this->linkRepository->create([ |
47
|
4 |
|
'url' => $url, |
48
|
4 |
|
'data' => $data, |
49
|
4 |
|
'click_limit' => $clickLimit, |
50
|
4 |
|
'expiry' => $expiry |
51
|
|
|
]); |
52
|
|
|
|
53
|
4 |
|
if($this->group !== null) { |
54
|
2 |
|
$this->groupRepository->pushLink($this->group, $link); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
return $link; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
public function group(callable $callback, $expiry = null, $clickLimit = null) |
61
|
|
|
{ |
62
|
3 |
|
$group = $this->groupRepository->create([ |
63
|
3 |
|
'expiry' => $expiry, |
64
|
3 |
|
'clickLimit' => $clickLimit |
65
|
|
|
]); |
66
|
|
|
|
67
|
3 |
|
$this->group = $group; |
68
|
3 |
|
$callback($this); |
69
|
3 |
|
$this->group = null; |
70
|
|
|
|
71
|
3 |
|
return $group; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: