1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\Core\Entity; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\Common\Collections\Collection; |
10
|
|
|
use Shlinkio\Shlink\Common\Entity\AbstractEntity; |
11
|
|
|
use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface; |
12
|
|
|
use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver; |
13
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException; |
14
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta; |
15
|
|
|
use Zend\Diactoros\Uri; |
16
|
|
|
|
17
|
|
|
use function array_reduce; |
18
|
|
|
use function count; |
19
|
|
|
use function Functional\contains; |
20
|
|
|
use function Functional\invoke; |
21
|
|
|
use function Shlinkio\Shlink\Core\generateRandomShortCode; |
22
|
|
|
|
23
|
|
|
class ShortUrl extends AbstractEntity |
24
|
|
|
{ |
25
|
|
|
/** @var string */ |
26
|
|
|
private $longUrl; |
27
|
|
|
/** @var string */ |
28
|
|
|
private $shortCode; |
29
|
|
|
/** @var Chronos */ |
30
|
|
|
private $dateCreated; |
31
|
|
|
/** @var Collection|Visit[] */ |
32
|
|
|
private $visits; |
33
|
|
|
/** @var Collection|Tag[] */ |
34
|
|
|
private $tags; |
35
|
|
|
/** @var Chronos|null */ |
36
|
|
|
private $validSince; |
37
|
|
|
/** @var Chronos|null */ |
38
|
|
|
private $validUntil; |
39
|
|
|
/** @var integer|null */ |
40
|
|
|
private $maxVisits; |
41
|
|
|
/** @var Domain|null */ |
42
|
46 |
|
private $domain; |
43
|
|
|
/** @var bool */ |
44
|
|
|
private $customSlugWasProvided; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
46 |
|
string $longUrl, |
48
|
|
|
?ShortUrlMeta $meta = null, |
49
|
46 |
|
?DomainResolverInterface $domainResolver = null |
50
|
46 |
|
) { |
51
|
46 |
|
$meta = $meta ?? ShortUrlMeta::createEmpty(); |
52
|
46 |
|
|
53
|
46 |
|
$this->longUrl = $longUrl; |
54
|
46 |
|
$this->dateCreated = Chronos::now(); |
55
|
46 |
|
$this->visits = new ArrayCollection(); |
56
|
46 |
|
$this->tags = new ArrayCollection(); |
57
|
46 |
|
$this->validSince = $meta->getValidSince(); |
58
|
|
|
$this->validUntil = $meta->getValidUntil(); |
59
|
|
|
$this->maxVisits = $meta->getMaxVisits(); |
60
|
17 |
|
$this->customSlugWasProvided = $meta->hasCustomSlug(); |
61
|
|
|
$this->shortCode = $meta->getCustomSlug() ?? generateRandomShortCode(); |
62
|
17 |
|
$this->domain = ($domainResolver ?? new SimpleDomainResolver())->resolveDomain($meta->getDomain()); |
63
|
|
|
} |
64
|
|
|
|
65
|
12 |
|
public function getLongUrl(): string |
66
|
|
|
{ |
67
|
12 |
|
return $this->longUrl; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getShortCode(): string |
71
|
10 |
|
{ |
72
|
|
|
return $this->shortCode; |
73
|
10 |
|
} |
74
|
10 |
|
|
75
|
|
|
public function getDateCreated(): Chronos |
76
|
|
|
{ |
77
|
10 |
|
return $this->dateCreated; |
78
|
|
|
} |
79
|
10 |
|
|
80
|
|
|
/** |
81
|
|
|
* @return Collection|Tag[] |
82
|
|
|
*/ |
83
|
|
|
public function getTags(): Collection |
84
|
|
|
{ |
85
|
19 |
|
return $this->tags; |
86
|
|
|
} |
87
|
19 |
|
|
88
|
|
|
/** |
89
|
|
|
* @param Collection|Tag[] $tags |
90
|
|
|
*/ |
91
|
|
|
public function setTags(Collection $tags): self |
92
|
|
|
{ |
93
|
3 |
|
$this->tags = $tags; |
94
|
|
|
return $this; |
95
|
3 |
|
} |
96
|
3 |
|
|
97
|
|
|
public function updateMeta(ShortUrlMeta $shortCodeMeta): void |
98
|
|
|
{ |
99
|
1 |
|
if ($shortCodeMeta->hasValidSince()) { |
100
|
|
|
$this->validSince = $shortCodeMeta->getValidSince(); |
101
|
1 |
|
} |
102
|
1 |
|
if ($shortCodeMeta->hasValidUntil()) { |
103
|
|
|
$this->validUntil = $shortCodeMeta->getValidUntil(); |
104
|
1 |
|
} |
105
|
1 |
|
if ($shortCodeMeta->hasMaxVisits()) { |
106
|
|
|
$this->maxVisits = $shortCodeMeta->getMaxVisits(); |
107
|
1 |
|
} |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @throws ShortCodeCannotBeRegeneratedException |
112
|
11 |
|
*/ |
113
|
|
|
public function regenerateShortCode(): self |
114
|
11 |
|
{ |
115
|
|
|
// In ShortUrls where a custom slug was provided, do nothing |
116
|
|
|
if ($this->customSlugWasProvided) { |
117
|
11 |
|
throw ShortCodeCannotBeRegeneratedException::forShortUrlWithCustomSlug(); |
118
|
|
|
} |
119
|
11 |
|
|
120
|
|
|
// The short code can be regenerated only on ShortUrl which have not been persisted yet |
121
|
|
|
if ($this->id !== null) { |
122
|
12 |
|
throw ShortCodeCannotBeRegeneratedException::forShortUrlAlreadyPersisted(); |
123
|
|
|
} |
124
|
12 |
|
|
125
|
|
|
$this->shortCode = generateRandomShortCode(); |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getValidSince(): ?Chronos |
130
|
|
|
{ |
131
|
|
|
return $this->validSince; |
132
|
4 |
|
} |
133
|
|
|
|
134
|
4 |
|
public function getValidUntil(): ?Chronos |
135
|
4 |
|
{ |
136
|
|
|
return $this->validUntil; |
137
|
|
|
} |
138
|
11 |
|
|
139
|
|
|
public function getVisitsCount(): int |
140
|
11 |
|
{ |
141
|
|
|
return count($this->visits); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param Collection|Visit[] $visits |
146
|
|
|
* @return ShortUrl |
147
|
|
|
* @internal |
148
|
12 |
|
*/ |
149
|
|
|
public function setVisits(Collection $visits): self |
150
|
12 |
|
{ |
151
|
12 |
|
$this->visits = $visits; |
152
|
12 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
12 |
|
public function getMaxVisits(): ?int |
156
|
|
|
{ |
157
|
12 |
|
return $this->maxVisits; |
158
|
12 |
|
} |
159
|
|
|
|
160
|
|
|
public function maxVisitsReached(): bool |
161
|
|
|
{ |
162
|
|
|
return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits; |
163
|
|
|
} |
164
|
8 |
|
|
165
|
|
|
public function toString(array $domainConfig): string |
166
|
8 |
|
{ |
167
|
1 |
|
return (string) (new Uri())->withPath($this->shortCode) |
168
|
|
|
->withScheme($domainConfig['schema'] ?? 'http') |
169
|
8 |
|
->withHost($this->resolveDomain($domainConfig['hostname'] ?? '')); |
170
|
|
|
} |
171
|
|
|
|
172
|
8 |
|
private function resolveDomain(string $fallback = ''): string |
173
|
|
|
{ |
174
|
|
|
if ($this->domain === null) { |
175
|
8 |
|
return $fallback; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $this->domain->getAuthority(); |
179
|
8 |
|
} |
180
|
8 |
|
|
181
|
8 |
|
public function matchesCriteria(ShortUrlMeta $meta, array $tags): bool |
182
|
|
|
{ |
183
|
3 |
|
if ($meta->hasMaxVisits() && $meta->getMaxVisits() !== $this->maxVisits) { |
184
|
8 |
|
return false; |
185
|
8 |
|
} |
186
|
|
|
if ($meta->hasDomain() && $meta->getDomain() !== $this->resolveDomain()) { |
187
|
|
|
return false; |
188
|
8 |
|
} |
189
|
|
|
if ($meta->hasValidSince() && ! $meta->getValidSince()->eq($this->validSince)) { |
|
|
|
|
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
if ($meta->hasValidUntil() && ! $meta->getValidUntil()->eq($this->validUntil)) { |
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$shortUrlTags = invoke($this->getTags(), '__toString'); |
197
|
|
|
$hasAllTags = count($shortUrlTags) === count($tags) && array_reduce( |
198
|
|
|
$tags, |
199
|
|
|
function (bool $hasAllTags, string $tag) use ($shortUrlTags) { |
200
|
|
|
return $hasAllTags && contains($shortUrlTags, $tag); |
201
|
|
|
}, |
202
|
|
|
true |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
return $hasAllTags; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|