Completed
Push — master ( 96faaf...c1c325 )
by Alejandro
03:51
created

ShortUrl::setVisits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 9.4285
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
namespace Shlinkio\Shlink\Core\Entity;
3
4
use Doctrine\Common\Collections\ArrayCollection;
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
8
9
/**
10
 * Class ShortUrl
11
 * @author
12
 * @link
13
 *
14
 * @ORM\Entity(repositoryClass="Shlinkio\Shlink\Core\Repository\ShortUrlRepository")
15
 * @ORM\Table(name="short_urls")
16
 */
17
class ShortUrl extends AbstractEntity implements \JsonSerializable
18
{
19
    /**
20
     * @var string
21
     * @ORM\Column(name="original_url", type="string", nullable=false, length=1024)
22
     */
23
    protected $originalUrl;
24
    /**
25
     * @var string
26
     * @ORM\Column(
27
     *     name="short_code",
28
     *     type="string",
29
     *     nullable=false,
30
     *     length=10,
31
     *     unique=true
32
     * )
33
     */
34
    protected $shortCode;
35
    /**
36
     * @var \DateTime
37
     * @ORM\Column(name="date_created", type="datetime")
38
     */
39
    protected $dateCreated;
40
    /**
41
     * @var Collection|Visit[]
42
     * @ORM\OneToMany(targetEntity=Visit::class, mappedBy="shortUrl", fetch="EXTRA_LAZY")
43
     */
44
    protected $visits;
45
    /**
46
     * @var Collection|Tag[]
47
     * @ORM\ManyToMany(targetEntity=Tag::class, cascade={"persist"})
48
     * @ORM\JoinTable(name="short_urls_in_tags", joinColumns={
49
     *     @ORM\JoinColumn(name="short_url_id", referencedColumnName="id")
50
     * }, inverseJoinColumns={
51
     *     @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
52
     * })
53
     */
54
    protected $tags;
55
56
    /**
57
     * ShortUrl constructor.
58
     */
59 14
    public function __construct()
60
    {
61 14
        $this->setDateCreated(new \DateTime());
62 14
        $this->setVisits(new ArrayCollection());
63 14
        $this->setShortCode('');
64 14
        $this->tags = new ArrayCollection();
65 14
    }
66
67
    /**
68
     * @return string
69
     */
70 3
    public function getOriginalUrl()
71
    {
72 3
        return $this->originalUrl;
73
    }
74
75
    /**
76
     * @param string $originalUrl
77
     * @return $this
78
     */
79 6
    public function setOriginalUrl($originalUrl)
80
    {
81 6
        $this->originalUrl = (string) $originalUrl;
82 6
        return $this;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 1
    public function getShortCode()
89
    {
90 1
        return $this->shortCode;
91
    }
92
93
    /**
94
     * @param string $shortCode
95
     * @return $this
96
     */
97 14
    public function setShortCode($shortCode)
98
    {
99 14
        $this->shortCode = $shortCode;
100 14
        return $this;
101
    }
102
103
    /**
104
     * @return \DateTime
105
     */
106
    public function getDateCreated()
107
    {
108
        return $this->dateCreated;
109
    }
110
111
    /**
112
     * @param \DateTime $dateCreated
113
     * @return $this
114
     */
115 14
    public function setDateCreated($dateCreated)
116
    {
117 14
        $this->dateCreated = $dateCreated;
118 14
        return $this;
119
    }
120
121
    /**
122
     * @return Visit[]|Collection
123
     */
124
    public function getVisits()
125
    {
126
        return $this->visits;
127
    }
128
129
    /**
130
     * @param Visit[]|Collection $visits
131
     * @return $this
132
     */
133 14
    public function setVisits($visits)
134
    {
135 14
        $this->visits = $visits;
136 14
        return $this;
137
    }
138
139
    /**
140
     * @return Collection|Tag[]
141
     */
142 1
    public function getTags()
143
    {
144 1
        return $this->tags;
145
    }
146
147
    /**
148
     * @param Collection|Tag[] $tags
149
     * @return $this
150
     */
151 1
    public function setTags($tags)
152
    {
153 1
        $this->tags = $tags;
154 1
        return $this;
155
    }
156
157
    /**
158
     * @param Tag $tag
159
     * @return $this
160
     */
161
    public function addTag(Tag $tag)
162
    {
163
        $this->tags->add($tag);
164
        return $this;
165
    }
166
167
    /**
168
     * Specify data which should be serialized to JSON
169
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
170
     * @return mixed data which can be serialized by <b>json_encode</b>,
171
     * which is a value of any type other than a resource.
172
     * @since 5.4.0
173
     */
174 2
    public function jsonSerialize()
175
    {
176
        return [
177 2
            'shortCode' => $this->shortCode,
178 2
            'originalUrl' => $this->originalUrl,
179 2
            'dateCreated' => isset($this->dateCreated) ? $this->dateCreated->format(\DateTime::ISO8601) : null,
180 2
            'visitsCount' => count($this->visits),
181 2
            'tags' => $this->tags->toArray(),
182 2
        ];
183
    }
184
}
185