Completed
Push — master ( aca90e...8ef0e7 )
by Alejandro
10s
created

ShortUrl   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 77.19%

Importance

Changes 0
Metric Value
dl 0
loc 240
rs 10
c 0
b 0
f 0
ccs 44
cts 57
cp 0.7719
wmc 22
lcom 1
cbo 2

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getOriginalUrl() 0 4 1
A setOriginalUrl() 0 5 1
A getShortCode() 0 4 1
A setShortCode() 0 5 1
A getDateCreated() 0 4 1
A setDateCreated() 0 5 1
A getTags() 0 4 1
A setTags() 0 5 1
A addTag() 0 5 1
A setValidSince() 0 5 1
A setValidUntil() 0 5 1
A getVisitsCount() 0 4 1
A setVisits() 0 5 1
A setMaxVisits() 0 5 1
A maxVisitsReached() 0 4 2
A jsonSerialize() 0 10 2
A getValidSince() 0 4 1
A getValidUntil() 0 4 1
A getMaxVisits() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Entity;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
10
11
/**
12
 * Class ShortUrl
13
 * @author
14
 * @link
15
 *
16
 * @ORM\Entity(repositoryClass="Shlinkio\Shlink\Core\Repository\ShortUrlRepository")
17
 * @ORM\Table(name="short_urls")
18
 */
19
class ShortUrl extends AbstractEntity implements \JsonSerializable
20
{
21
    /**
22
     * @var string
23
     * @ORM\Column(name="original_url", type="string", nullable=false, length=1024)
24
     */
25
    protected $originalUrl;
26
    /**
27
     * @var string
28
     * @ORM\Column(
29
     *     name="short_code",
30
     *     type="string",
31
     *     nullable=false,
32
     *     length=10,
33
     *     unique=true
34
     * )
35
     */
36
    protected $shortCode;
37
    /**
38
     * @var \DateTime
39
     * @ORM\Column(name="date_created", type="datetime")
40
     */
41
    protected $dateCreated;
42
    /**
43
     * @var Collection|Visit[]
44
     * @ORM\OneToMany(targetEntity=Visit::class, mappedBy="shortUrl", fetch="EXTRA_LAZY")
45
     */
46
    protected $visits;
47
    /**
48
     * @var Collection|Tag[]
49
     * @ORM\ManyToMany(targetEntity=Tag::class, cascade={"persist"})
50
     * @ORM\JoinTable(name="short_urls_in_tags", joinColumns={
51
     *     @ORM\JoinColumn(name="short_url_id", referencedColumnName="id")
52
     * }, inverseJoinColumns={
53
     *     @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
54
     * })
55
     */
56
    protected $tags;
57
    /**
58
     * @var \DateTime
59
     * @ORM\Column(name="valid_since", type="datetime", nullable=true)
60
     */
61
    protected $validSince;
62
    /**
63
     * @var \DateTime
64
     * @ORM\Column(name="valid_until", type="datetime", nullable=true)
65
     */
66
    protected $validUntil;
67
    /**
68
     * @var integer
69
     * @ORM\Column(name="max_visits", type="integer", nullable=true)
70
     */
71
    protected $maxVisits;
72
73
    /**
74
     * ShortUrl constructor.
75
     */
76 17
    public function __construct()
77
    {
78 17
        $this->dateCreated = new \DateTime();
79 17
        $this->visits = new ArrayCollection();
80 17
        $this->shortCode = '';
81 17
        $this->tags = new ArrayCollection();
82 17
    }
83
84
    /**
85
     * @return string
86
     */
87 3
    public function getOriginalUrl(): string
88
    {
89 3
        return $this->originalUrl;
90
    }
91
92
    /**
93
     * @param string $originalUrl
94
     * @return $this
95
     */
96 7
    public function setOriginalUrl(string $originalUrl)
97
    {
98 7
        $this->originalUrl = $originalUrl;
99 7
        return $this;
100
    }
101
102
    /**
103
     * @return string
104
     */
105 1
    public function getShortCode(): string
106
    {
107 1
        return $this->shortCode;
108
    }
109
110
    /**
111
     * @param string $shortCode
112
     * @return $this
113
     */
114 4
    public function setShortCode(string $shortCode)
115
    {
116 4
        $this->shortCode = $shortCode;
117 4
        return $this;
118
    }
119
120
    /**
121
     * @return \DateTime
122
     */
123
    public function getDateCreated(): \DateTime
124
    {
125
        return $this->dateCreated;
126
    }
127
128
    /**
129
     * @param \DateTime $dateCreated
130
     * @return $this
131
     */
132
    public function setDateCreated(\DateTime $dateCreated)
133
    {
134
        $this->dateCreated = $dateCreated;
135
        return $this;
136
    }
137
138
    /**
139
     * @return Collection|Tag[]
140
     */
141 1
    public function getTags(): Collection
142
    {
143 1
        return $this->tags;
144
    }
145
146
    /**
147
     * @param Collection|Tag[] $tags
148
     * @return $this
149
     */
150 2
    public function setTags(Collection $tags)
151
    {
152 2
        $this->tags = $tags;
153 2
        return $this;
154
    }
155
156
    /**
157
     * @param Tag $tag
158
     * @return $this
159
     */
160
    public function addTag(Tag $tag)
161
    {
162
        $this->tags->add($tag);
163
        return $this;
164
    }
165
166
    /**
167
     * @return \DateTime|null
168
     */
169 1
    public function getValidSince()
170
    {
171 1
        return $this->validSince;
172
    }
173
174
    /**
175
     * @param \DateTime|null $validSince
176
     * @return $this|self
177
     */
178 4
    public function setValidSince($validSince): self
179
    {
180 4
        $this->validSince = $validSince;
181 4
        return $this;
182
    }
183
184
    /**
185
     * @return \DateTime|null
186
     */
187 1
    public function getValidUntil()
188
    {
189 1
        return $this->validUntil;
190
    }
191
192
    /**
193
     * @param \DateTime|null $validUntil
194
     * @return $this|self
195
     */
196 4
    public function setValidUntil($validUntil): self
197
    {
198 4
        $this->validUntil = $validUntil;
199 4
        return $this;
200
    }
201
202 2
    public function getVisitsCount(): int
203
    {
204 2
        return count($this->visits);
205
    }
206
207
    /**
208
     * @param Collection $visits
209
     * @return ShortUrl
210
     * @internal
211
     */
212
    public function setVisits(Collection $visits): self
213
    {
214
        $this->visits = $visits;
215
        return $this;
216
    }
217
218
    /**
219
     * @return int|null
220
     */
221 1
    public function getMaxVisits()
222
    {
223 1
        return $this->maxVisits;
224
    }
225
226
    /**
227
     * @param int|null $maxVisits
228
     * @return $this|self
229
     */
230 4
    public function setMaxVisits($maxVisits): self
231
    {
232 4
        $this->maxVisits = $maxVisits;
233 4
        return $this;
234
    }
235
236
    public function maxVisitsReached(): bool
237
    {
238
        return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
239
    }
240
241
    /**
242
     * Specify data which should be serialized to JSON
243
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
244
     * @return mixed data which can be serialized by <b>json_encode</b>,
245
     * which is a value of any type other than a resource.
246
     * @since 5.4.0
247
     */
248 2
    public function jsonSerialize()
249
    {
250
        return [
251 2
            'shortCode' => $this->shortCode,
252 2
            'originalUrl' => $this->originalUrl,
253 2
            'dateCreated' => $this->dateCreated !== null ? $this->dateCreated->format(\DateTime::ATOM) : null,
254 2
            'visitsCount' => $this->getVisitsCount(),
255 2
            'tags' => $this->tags->toArray(),
256
        ];
257
    }
258
}
259