Passed
Push — develop ( 02b19a...b765c6 )
by Stone
04:36
created

Trick::addTag()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Symfony\Component\Validator\Constraints as Assert;
8
use Gedmo\Mapping\Annotation as Gedmo;
9
use Doctrine\ORM\Mapping as ORM;
10
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
11
12
/**
13
 * @ORM\Entity(repositoryClass="App\Repository\TrickRepository")
14
 * @Gedmo\Loggable
15
 * @UniqueEntity(fields="name", message="this trick already exists")
16
 */
17
class Trick extends AppEntity
18
{
19
20
    const NUMBER_OF_DISPLAYED_TRICKS = 10;
21
22
    /**
23
     * @ORM\Id()
24
     * @ORM\GeneratedValue()
25
     * @ORM\Column(type="integer")
26
     */
27
    private $id;
28
29
    /**
30
     * @ORM\Column(type="string", length=255)
31
     * @Gedmo\Versioned
32
     * @Assert\Length(
33
     *     min=5,
34
     *     max=255,
35
     *     minMessage = "Title must be at least {{ limit }} characters",
36
     *     maxMessage = "Title can not exceed {{ limit }} characters"
37
     * )
38
     */
39
    private $name;
40
41
    /**
42
     * @ORM\Column(type="text")
43
     * @Gedmo\Versioned
44
     */
45
    private $text;
46
47
    /**
48
     * @ORM\Column(type="string", length=255, unique=true)
49
     * @Gedmo\Slug(fields={"name"})
50
     */
51
    private $slug;
52
53
    /**
54
     * @ORM\Column(type="datetime")
55
     * @Gedmo\Timestampable(on="create")
56
     */
57
    private $createdAt;
58
59
    /**
60
     * @ORM\Column(type="datetime")
61
     * @Gedmo\Timestampable(on="update")
62
     */
63
    private $editedAt;
64
65
    /**
66
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="tricks")
67
     * @ORM\JoinColumn(nullable=false)
68
     */
69
    private $category;
70
71
    /**
72
     * @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="trick", cascade={"persist"})
73
     */
74
    private $tags;
75
76
    public function __construct()
77
    {
78
        $this->tags = new ArrayCollection();
79
    }
80
81
    public function getId(): ?int
82
    {
83
        return $this->id;
84
    }
85
86
    public function getName(): ?string
87
    {
88
        return $this->name;
89
    }
90
91
    public function setName(string $name): self
92
    {
93
        $this->name = $name;
94
95
        return $this;
96
    }
97
98
    public function getText(): ?string
99
    {
100
        return $this->text;
101
    }
102
103
    public function setText(string $text): self
104
    {
105
        $this->text = $text;
106
107
        return $this;
108
    }
109
110
    public function getSlug(): ?string
111
    {
112
        return $this->slug;
113
    }
114
115
    public function setSlug(string $slug): self
116
    {
117
        $this->slug = $slug;
118
119
        return $this;
120
    }
121
122
    public function getCreatedAt(): ?\DateTimeInterface
123
    {
124
        return $this->createdAt;
125
    }
126
127
    public function setCreatedAt(\DateTimeInterface $createdAt): self
128
    {
129
        $this->createdAt = $createdAt;
130
131
        return $this;
132
    }
133
134
    public function getEditedAt(): ?\DateTimeInterface
135
    {
136
        return $this->editedAt;
137
    }
138
139
    public function setEditedAt(\DateTimeInterface $editedAt): self
140
    {
141
        $this->editedAt = $editedAt;
142
143
        return $this;
144
    }
145
146
    public function getCategory(): ?Category
147
    {
148
        return $this->category;
149
    }
150
151
    public function setCategory(?Category $category): self
152
    {
153
        $this->category = $category;
154
155
        return $this;
156
    }
157
158
    public function __toString()
159
    {
160
        return $this->name;
161
    }
162
163
    /**
164
     * @return Collection|Tag[]
165
     */
166
    public function getTags(): Collection
167
    {
168
        return $this->tags;
169
    }
170
171
    public function addTag(Tag $tag): self
172
    {
173
        if (!$this->tags->contains($tag)) {
174
            $this->tags[] = $tag;
175
            $tag->addTrick($this);
176
        }
177
178
        return $this;
179
    }
180
181
    public function removeTag(Tag $tag): self
182
    {
183
        if ($this->tags->contains($tag)) {
184
            $this->tags->removeElement($tag);
185
            $tag->removeTrick($this);
186
        }
187
188
        return $this;
189
    }
190
}
191