Completed
Push — master ( f7ceef...6986d0 )
by Alejandro
16s
created

ShortUrl::getValidSince()   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
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Entity;
5
6
use Cake\Chronos\Chronos;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping as ORM;
10
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
11
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
12
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
13
use function count;
14
15
/**
16
 * Class ShortUrl
17
 * @author
18
 * @link
19
 *
20
 * @ORM\Entity(repositoryClass=ShortUrlRepository::class)
21
 * @ORM\Table(name="short_urls")
22
 */
23
class ShortUrl extends AbstractEntity
24
{
25
    /**
26
     * @var string
27
     * @ORM\Column(name="original_url", type="string", nullable=false, length=1024)
28
     */
29
    private $longUrl;
30
    /**
31
     * @var string
32
     * @ORM\Column(
33
     *     name="short_code",
34
     *     type="string",
35
     *     nullable=false,
36
     *     length=255,
37
     *     unique=true
38
     * )
39
     */
40
    private $shortCode;
41
    /**
42
     * @var Chronos
43
     * @ORM\Column(name="date_created", type="chronos_datetime")
44
     */
45
    private $dateCreated;
46
    /**
47
     * @var Collection|Visit[]
48
     * @ORM\OneToMany(targetEntity=Visit::class, mappedBy="shortUrl", fetch="EXTRA_LAZY")
49
     */
50
    private $visits;
51
    /**
52
     * @var Collection|Tag[]
53
     * @ORM\ManyToMany(targetEntity=Tag::class, cascade={"persist"})
54
     * @ORM\JoinTable(name="short_urls_in_tags", joinColumns={
55
     *     @ORM\JoinColumn(name="short_url_id", referencedColumnName="id")
56
     * }, inverseJoinColumns={
57
     *     @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
58
     * })
59
     */
60
    private $tags;
61
    /**
62
     * @var Chronos|null
63
     * @ORM\Column(name="valid_since", type="chronos_datetime", nullable=true)
64
     */
65
    private $validSince;
66
    /**
67
     * @var Chronos|null
68
     * @ORM\Column(name="valid_until", type="chronos_datetime", nullable=true)
69
     */
70
    private $validUntil;
71
    /**
72
     * @var integer
73
     * @ORM\Column(name="max_visits", type="integer", nullable=true)
74
     */
75
    private $maxVisits;
76
77 35
    public function __construct(string $longUrl, ShortUrlMeta $meta = null)
78
    {
79 35
        $meta = $meta ?? ShortUrlMeta::createEmpty();
80
81 35
        $this->longUrl = $longUrl;
82 35
        $this->dateCreated = Chronos::now();
83 35
        $this->visits = new ArrayCollection();
84 35
        $this->tags = new ArrayCollection();
85 35
        $this->validSince = $meta->getValidSince();
86 35
        $this->validUntil = $meta->getValidUntil();
87 35
        $this->maxVisits = $meta->getMaxVisits();
88 35
        $this->shortCode = $meta->getCustomSlug() ?? ''; // TODO logic to calculate short code should be passed somehow
89 35
    }
90
91 12
    public function getLongUrl(): string
92
    {
93 12
        return $this->longUrl;
94
    }
95
96 8
    public function getShortCode(): string
97
    {
98 8
        return $this->shortCode;
99
    }
100
101
    // TODO Short code is currently calculated based on the ID, so a setter is needed
102 9
    public function setShortCode(string $shortCode): self
103
    {
104 9
        $this->shortCode = $shortCode;
105 9
        return $this;
106
    }
107
108 5
    public function getDateCreated(): Chronos
109
    {
110 5
        return $this->dateCreated;
111
    }
112
113
    /**
114
     * @return Collection|Tag[]
115
     */
116 6
    public function getTags(): Collection
117
    {
118 6
        return $this->tags;
119
    }
120
121
    /**
122
     * @param Collection|Tag[] $tags
123
     */
124 2
    public function setTags(Collection $tags): self
125
    {
126 2
        $this->tags = $tags;
127 2
        return $this;
128
    }
129
130 1
    public function updateMeta(ShortUrlMeta $shortCodeMeta): void
131
    {
132 1
        if ($shortCodeMeta->hasValidSince()) {
133 1
            $this->validSince = $shortCodeMeta->getValidSince();
134
        }
135 1
        if ($shortCodeMeta->hasValidUntil()) {
136 1
            $this->validUntil = $shortCodeMeta->getValidUntil();
137
        }
138 1
        if ($shortCodeMeta->hasMaxVisits()) {
139 1
            $this->maxVisits = $shortCodeMeta->getMaxVisits();
140
        }
141 1
    }
142
143 1
    public function getValidSince(): ?Chronos
144
    {
145 1
        return $this->validSince;
146
    }
147
148 1
    public function getValidUntil(): ?Chronos
149
    {
150 1
        return $this->validUntil;
151
    }
152
153 7
    public function getVisitsCount(): int
154
    {
155 7
        return count($this->visits);
156
    }
157
158
    /**
159
     * @param Collection|Visit[] $visits
160
     * @return ShortUrl
161
     * @internal
162
     */
163 4
    public function setVisits(Collection $visits): self
164
    {
165 4
        $this->visits = $visits;
166 4
        return $this;
167
    }
168
169 1
    public function getMaxVisits(): ?int
170
    {
171 1
        return $this->maxVisits;
172
    }
173
174
    public function maxVisitsReached(): bool
175
    {
176
        return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
177
    }
178
}
179