Completed
Pull Request — master (#179)
by Alejandro
12:20
created

ShortUrl::setDateCreated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
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
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=255,
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 5
    public function getLongUrl(): string
88
    {
89 5
        return $this->originalUrl;
90
    }
91
92
    /**
93
     * @param string $longUrl
94
     * @return $this
95
     */
96 9
    public function setLongUrl(string $longUrl): self
97
    {
98 9
        $this->originalUrl = $longUrl;
99 9
        return $this;
100
    }
101
102
    /**
103
     * @return string
104
     * @deprecated Use getLongUrl() instead
105
     */
106 3
    public function getOriginalUrl(): string
107
    {
108 3
        return $this->getLongUrl();
109
    }
110
111
    /**
112
     * @param string $originalUrl
113
     * @return $this
114
     * @deprecated Use setLongUrl() instead
115
     */
116 7
    public function setOriginalUrl(string $originalUrl): self
117
    {
118 7
        return $this->setLongUrl($originalUrl);
119
    }
120
121
    /**
122
     * @return string
123
     */
124 3
    public function getShortCode(): string
125
    {
126 3
        return $this->shortCode;
127
    }
128
129
    /**
130
     * @param string $shortCode
131
     * @return $this
132
     */
133 4
    public function setShortCode(string $shortCode)
134
    {
135 4
        $this->shortCode = $shortCode;
136 4
        return $this;
137
    }
138
139
    /**
140
     * @return \DateTime
141
     */
142 2
    public function getDateCreated(): \DateTime
143
    {
144 2
        return $this->dateCreated;
145
    }
146
147
    /**
148
     * @param \DateTime $dateCreated
149
     * @return $this
150
     */
151
    public function setDateCreated(\DateTime $dateCreated)
152
    {
153
        $this->dateCreated = $dateCreated;
154
        return $this;
155
    }
156
157
    /**
158
     * @return Collection|Tag[]
159
     */
160 3
    public function getTags(): Collection
161
    {
162 3
        return $this->tags;
163
    }
164
165
    /**
166
     * @param Collection|Tag[] $tags
167
     * @return $this
168
     */
169 2
    public function setTags(Collection $tags)
170
    {
171 2
        $this->tags = $tags;
172 2
        return $this;
173
    }
174
175
    /**
176
     * @param Tag $tag
177
     * @return $this
178
     */
179
    public function addTag(Tag $tag)
180
    {
181
        $this->tags->add($tag);
182
        return $this;
183
    }
184
185
    /**
186
     * @return \DateTime|null
187
     */
188 1
    public function getValidSince()
189
    {
190 1
        return $this->validSince;
191
    }
192
193
    /**
194
     * @param \DateTime|null $validSince
195
     * @return $this|self
196
     */
197 4
    public function setValidSince($validSince): self
198
    {
199 4
        $this->validSince = $validSince;
200 4
        return $this;
201
    }
202
203
    /**
204
     * @return \DateTime|null
205
     */
206 1
    public function getValidUntil()
207
    {
208 1
        return $this->validUntil;
209
    }
210
211
    /**
212
     * @param \DateTime|null $validUntil
213
     * @return $this|self
214
     */
215 4
    public function setValidUntil($validUntil): self
216
    {
217 4
        $this->validUntil = $validUntil;
218 4
        return $this;
219
    }
220
221 2
    public function getVisitsCount(): int
222
    {
223 2
        return count($this->visits);
224
    }
225
226
    /**
227
     * @param Collection $visits
228
     * @return ShortUrl
229
     * @internal
230
     */
231
    public function setVisits(Collection $visits): self
232
    {
233
        $this->visits = $visits;
234
        return $this;
235
    }
236
237
    /**
238
     * @return int|null
239
     */
240 1
    public function getMaxVisits()
241
    {
242 1
        return $this->maxVisits;
243
    }
244
245
    /**
246
     * @param int|null $maxVisits
247
     * @return $this|self
248
     */
249 4
    public function setMaxVisits($maxVisits): self
250
    {
251 4
        $this->maxVisits = $maxVisits;
252 4
        return $this;
253
    }
254
255
    public function maxVisitsReached(): bool
256
    {
257
        return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
258
    }
259
}
260