Completed
Push — master ( cb6756...21ef1d )
by Alejandro
16s queued 12s
created

ShortUrl::getDateCreated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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