Passed
Push — develop ( 34195e...b04746 )
by Stone
08:36 queued 03:47
created

Trick::addComment()   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
    /**
77
     * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="trick", cascade={"remove", "persist"})
78
     */
79
    private $comments;
80
81
    public function __construct()
82
    {
83
        $this->tags = new ArrayCollection();
84
        $this->comments = new ArrayCollection();
85
    }
86
87
    public function getId(): ?int
88
    {
89
        return $this->id;
90
    }
91
92
    public function getName(): ?string
93
    {
94
        return $this->name;
95
    }
96
97
    public function setName(string $name): self
98
    {
99
        $this->name = $name;
100
101
        return $this;
102
    }
103
104
    public function getText(): ?string
105
    {
106
        return $this->text;
107
    }
108
109
    public function setText(string $text): self
110
    {
111
        $this->text = $text;
112
113
        return $this;
114
    }
115
116
    public function getSlug(): ?string
117
    {
118
        return $this->slug;
119
    }
120
121
    public function setSlug(string $slug): self
122
    {
123
        $this->slug = $slug;
124
125
        return $this;
126
    }
127
128
    public function getCreatedAt(): ?\DateTimeInterface
129
    {
130
        return $this->createdAt;
131
    }
132
133
    public function setCreatedAt(\DateTimeInterface $createdAt): self
134
    {
135
        $this->createdAt = $createdAt;
136
137
        return $this;
138
    }
139
140
    public function getEditedAt(): ?\DateTimeInterface
141
    {
142
        return $this->editedAt;
143
    }
144
145
    public function setEditedAt(\DateTimeInterface $editedAt): self
146
    {
147
        $this->editedAt = $editedAt;
148
149
        return $this;
150
    }
151
152
    public function getCategory(): ?Category
153
    {
154
        return $this->category;
155
    }
156
157
    public function setCategory(?Category $category): self
158
    {
159
        $this->category = $category;
160
161
        return $this;
162
    }
163
164
    public function __toString()
165
    {
166
        return $this->name;
167
    }
168
169
    /**
170
     * @return Collection|Tag[]
171
     */
172
    public function getTags(): Collection
173
    {
174
        return $this->tags;
175
    }
176
177
    public function addTag(Tag $tag): self
178
    {
179
        if (!$this->tags->contains($tag)) {
180
            $this->tags[] = $tag;
181
            $tag->addTrick($this);
182
        }
183
184
        return $this;
185
    }
186
187
    public function removeTag(Tag $tag): self
188
    {
189
        if ($this->tags->contains($tag)) {
190
            $this->tags->removeElement($tag);
191
            $tag->removeTrick($this);
192
        }
193
194
        return $this;
195
    }
196
197
    /**
198
     * @return Collection|Comment[]
199
     */
200
    public function getComments(): Collection
201
    {
202
        return $this->comments;
203
    }
204
205
    public function addComment(Comment $comment): self
206
    {
207
        if (!$this->comments->contains($comment)) {
208
            $this->comments[] = $comment;
209
            $comment->setTrick($this);
210
        }
211
212
        return $this;
213
    }
214
215
    public function removeComment(Comment $comment): self
216
    {
217
        if ($this->comments->contains($comment)) {
218
            $this->comments->removeElement($comment);
219
            // set the owning side to null (unless already changed)
220
            if ($comment->getTrick() === $this) {
221
                $comment->setTrick(null);
222
            }
223
        }
224
225
        return $this;
226
    }
227
}
228