Completed
Push — develop ( 8d438a...1f78f5 )
by Alejandro
16s queued 12s
created

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