|
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
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ShortUrl |
|
14
|
|
|
* @author |
|
15
|
|
|
* @link |
|
16
|
|
|
* |
|
17
|
|
|
* @ORM\Entity(repositoryClass=ShortUrlRepository::class) |
|
18
|
|
|
* @ORM\Table(name="short_urls") |
|
19
|
|
|
*/ |
|
20
|
|
|
class ShortUrl extends AbstractEntity |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
* @ORM\Column(name="original_url", type="string", nullable=false, length=1024) |
|
25
|
|
|
*/ |
|
26
|
|
|
private $originalUrl; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
* @ORM\Column( |
|
30
|
|
|
* name="short_code", |
|
31
|
|
|
* type="string", |
|
32
|
|
|
* nullable=false, |
|
33
|
|
|
* length=255, |
|
34
|
|
|
* unique=true |
|
35
|
|
|
* ) |
|
36
|
|
|
*/ |
|
37
|
|
|
private $shortCode; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var \DateTime |
|
40
|
|
|
* @ORM\Column(name="date_created", type="datetime") |
|
41
|
|
|
*/ |
|
42
|
|
|
private $dateCreated; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var Collection|Visit[] |
|
45
|
|
|
* @ORM\OneToMany(targetEntity=Visit::class, mappedBy="shortUrl", fetch="EXTRA_LAZY") |
|
46
|
|
|
*/ |
|
47
|
|
|
private $visits; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var Collection|Tag[] |
|
50
|
|
|
* @ORM\ManyToMany(targetEntity=Tag::class, cascade={"persist"}) |
|
51
|
|
|
* @ORM\JoinTable(name="short_urls_in_tags", joinColumns={ |
|
52
|
|
|
* @ORM\JoinColumn(name="short_url_id", referencedColumnName="id") |
|
53
|
|
|
* }, inverseJoinColumns={ |
|
54
|
|
|
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id") |
|
55
|
|
|
* }) |
|
56
|
|
|
*/ |
|
57
|
|
|
private $tags; |
|
58
|
|
|
/** |
|
59
|
|
|
* @var \DateTime |
|
60
|
|
|
* @ORM\Column(name="valid_since", type="datetime", nullable=true) |
|
61
|
|
|
*/ |
|
62
|
|
|
private $validSince; |
|
63
|
|
|
/** |
|
64
|
|
|
* @var \DateTime |
|
65
|
|
|
* @ORM\Column(name="valid_until", type="datetime", nullable=true) |
|
66
|
|
|
*/ |
|
67
|
|
|
private $validUntil; |
|
68
|
|
|
/** |
|
69
|
|
|
* @var integer |
|
70
|
|
|
* @ORM\Column(name="max_visits", type="integer", nullable=true) |
|
71
|
|
|
*/ |
|
72
|
|
|
private $maxVisits; |
|
73
|
|
|
|
|
74
|
30 |
|
public function __construct() |
|
75
|
|
|
{ |
|
76
|
30 |
|
$this->shortCode = ''; |
|
77
|
30 |
|
$this->dateCreated = new \DateTime(); |
|
78
|
30 |
|
$this->visits = new ArrayCollection(); |
|
|
|
|
|
|
79
|
30 |
|
$this->tags = new ArrayCollection(); |
|
|
|
|
|
|
80
|
30 |
|
} |
|
81
|
|
|
|
|
82
|
12 |
|
public function getLongUrl(): string |
|
83
|
|
|
{ |
|
84
|
12 |
|
return $this->originalUrl; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
19 |
|
public function setLongUrl(string $longUrl): self |
|
88
|
|
|
{ |
|
89
|
19 |
|
$this->originalUrl = $longUrl; |
|
90
|
19 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @deprecated Use getLongUrl() instead |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function getOriginalUrl(): string |
|
97
|
|
|
{ |
|
98
|
2 |
|
return $this->getLongUrl(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @deprecated Use setLongUrl() instead |
|
103
|
|
|
*/ |
|
104
|
7 |
|
public function setOriginalUrl(string $originalUrl): self |
|
105
|
|
|
{ |
|
106
|
7 |
|
return $this->setLongUrl($originalUrl); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
8 |
|
public function getShortCode(): string |
|
110
|
|
|
{ |
|
111
|
8 |
|
return $this->shortCode; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
9 |
|
public function setShortCode(string $shortCode): self |
|
115
|
|
|
{ |
|
116
|
9 |
|
$this->shortCode = $shortCode; |
|
117
|
9 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
5 |
|
public function getDateCreated(): \DateTime |
|
121
|
|
|
{ |
|
122
|
5 |
|
return $this->dateCreated; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function setDateCreated(\DateTime $dateCreated): self |
|
126
|
|
|
{ |
|
127
|
|
|
$this->dateCreated = $dateCreated; |
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return Collection|Tag[] |
|
133
|
|
|
*/ |
|
134
|
6 |
|
public function getTags(): Collection |
|
135
|
|
|
{ |
|
136
|
6 |
|
return $this->tags; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param Collection|Tag[] $tags |
|
141
|
|
|
*/ |
|
142
|
2 |
|
public function setTags(Collection $tags): self |
|
143
|
|
|
{ |
|
144
|
2 |
|
$this->tags = $tags; |
|
145
|
2 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function addTag(Tag $tag): self |
|
149
|
|
|
{ |
|
150
|
|
|
$this->tags->add($tag); |
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
public function getValidSince(): ?\DateTime |
|
155
|
|
|
{ |
|
156
|
1 |
|
return $this->validSince; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
4 |
|
public function setValidSince(?\DateTime $validSince): self |
|
160
|
|
|
{ |
|
161
|
4 |
|
$this->validSince = $validSince; |
|
162
|
4 |
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
1 |
|
public function getValidUntil(): ?\DateTime |
|
166
|
|
|
{ |
|
167
|
1 |
|
return $this->validUntil; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
4 |
|
public function setValidUntil(?\DateTime $validUntil): self |
|
171
|
|
|
{ |
|
172
|
4 |
|
$this->validUntil = $validUntil; |
|
173
|
4 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
7 |
|
public function getVisitsCount(): int |
|
177
|
|
|
{ |
|
178
|
7 |
|
return \count($this->visits); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param Collection|Visit[] $visits |
|
183
|
|
|
* @return ShortUrl |
|
184
|
|
|
* @internal |
|
185
|
|
|
*/ |
|
186
|
4 |
|
public function setVisits(Collection $visits): self |
|
187
|
|
|
{ |
|
188
|
4 |
|
$this->visits = $visits; |
|
189
|
4 |
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
1 |
|
public function getMaxVisits(): ?int |
|
193
|
|
|
{ |
|
194
|
1 |
|
return $this->maxVisits; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
4 |
|
public function setMaxVisits(?int $maxVisits): self |
|
198
|
|
|
{ |
|
199
|
4 |
|
$this->maxVisits = $maxVisits; |
|
200
|
4 |
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function maxVisitsReached(): bool |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..