Test Failed
Push — develop ( c35c51...0616cb )
by Stone
04:36 queued 10s
created

Trick::getPrimaryImages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Entity;
4
5
use App\Serializer\TagSerializer;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Symfony\Component\Validator\Constraints as Assert;
9
use Gedmo\Mapping\Annotation as Gedmo;
10
use Doctrine\ORM\Mapping as ORM;
11
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
12
13
/**
14
 * @ORM\Entity(repositoryClass="App\Repository\TrickRepository")
15
 * @Gedmo\Loggable
16
 * @UniqueEntity(fields="name", message="this trick already exists")
17
 */
18
class Trick extends AppEntity
19
{
20
21
    const NUMBER_OF_DISPLAYED_TRICKS = 10;
22
23
    /**
24
     * @ORM\Id()
25
     * @ORM\GeneratedValue()
26
     * @ORM\Column(type="integer")
27
     */
28
    private $id;
29
30
    /**
31
     * @ORM\Column(type="string", length=255)
32
     * @Gedmo\Versioned
33
     * @Assert\Length(
34
     *     min=5,
35
     *     max=255,
36
     *     minMessage = "Title must be at least {{ limit }} characters",
37
     *     maxMessage = "Title can not exceed {{ limit }} characters"
38
     * )
39
     */
40
    private $name;
41
42
    /**
43
     * @ORM\Column(type="text")
44
     * @Gedmo\Versioned
45
     */
46
    private $text;
47
48
    /**
49
     * @ORM\Column(type="string", length=255, unique=true)
50
     * @Gedmo\Slug(fields={"name"})
51
     */
52
    private $slug;
53
54
    /**
55
     * @ORM\Column(type="datetime")
56
     * @Gedmo\Timestampable(on="create")
57
     */
58
    private $createdAt;
59
60
    /**
61
     * @ORM\Column(type="datetime")
62
     * @Gedmo\Timestampable(on="update")
63
     */
64
    private $editedAt;
65
66
    /**
67
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="tricks")
68
     * @ORM\JoinColumn(nullable=false)
69
     */
70
    private $category;
71
72
    /**
73
     * @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="trick", cascade={"persist"})
74
     */
75
    private $tags;
76
77
    /**
78
     * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="trick", cascade={"remove", "persist"})
79
     * @ORM\OrderBy({"createdAt" = "DESC"})
80
     */
81
    private $comments;
82
83
    /**
84
     * @ORM\OneToMany(targetEntity="App\Entity\Image", mappedBy="trick", cascade={"remove", "persist"})
85
     */
86
    private $images;
87
88
    public function __construct()
89
    {
90
        $this->tags = new ArrayCollection();
91
        $this->comments = new ArrayCollection();
92
        $this->images = new ArrayCollection();
93
    }
94
95
    public function getId(): ?int
96
    {
97
        return $this->id;
98
    }
99
100
    public function getName(): ?string
101
    {
102
        return $this->name;
103
    }
104
105
    public function setName(string $name): self
106
    {
107
        $this->name = $name;
108
109
        return $this;
110
    }
111
112
    public function getText(): ?string
113
    {
114
        return $this->text;
115
    }
116
117
    public function setText(string $text): self
118
    {
119
        $this->text = $text;
120
121
        return $this;
122
    }
123
124
    public function getSlug(): ?string
125
    {
126
        return $this->slug;
127
    }
128
129
    public function setSlug(string $slug): self
130
    {
131
        $this->slug = $slug;
132
133
        return $this;
134
    }
135
136
    public function getCreatedAt(): ?\DateTimeInterface
137
    {
138
        return $this->createdAt;
139
    }
140
141
    public function setCreatedAt(\DateTimeInterface $createdAt): self
142
    {
143
        $this->createdAt = $createdAt;
144
145
        return $this;
146
    }
147
148
    public function getEditedAt(): ?\DateTimeInterface
149
    {
150
        return $this->editedAt;
151
    }
152
153
    public function setEditedAt(\DateTimeInterface $editedAt): self
154
    {
155
        $this->editedAt = $editedAt;
156
157
        return $this;
158
    }
159
160
    public function getCategory(): ?Category
161
    {
162
        return $this->category;
163
    }
164
165
    public function setCategory(?Category $category): self
166
    {
167
        $this->category = $category;
168
169
        return $this;
170
    }
171
172
    public function __toString()
173
    {
174
        return $this->name;
175
    }
176
177
    /**
178
     * @return Collection|Tag[]
179
     */
180
    public function getTags(): Collection
181
    {
182
        return $this->tags;
183
    }
184
185
    public function getTagsJson()
186
    {
187
188
        $tagSerializer = new TagSerializer();
189
        return $tagSerializer->trickTagsJson($this);
190
191
    }
192
193
    public function addTag(Tag $tag): self
194
    {
195
        if (!$this->tags->contains($tag)) {
196
            $this->tags[] = $tag;
197
            $tag->addTrick($this);
198
        }
199
200
        return $this;
201
    }
202
203
    public function removeTag(Tag $tag): self
204
    {
205
        if ($this->tags->contains($tag)) {
206
            $this->tags->removeElement($tag);
207
            $tag->removeTrick($this);
208
        }
209
210
        return $this;
211
    }
212
213
    /**
214
     * @return Collection|Comment[]
215
     */
216
    public function getComments(): Collection
217
    {
218
        return $this->comments;
219
    }
220
221
    public function addComment(Comment $comment): self
222
    {
223
        if (!$this->comments->contains($comment)) {
224
            $this->comments[] = $comment;
225
            $comment->setTrick($this);
226
        }
227
228
        return $this;
229
    }
230
231
    public function removeComment(Comment $comment): self
232
    {
233
        if ($this->comments->contains($comment)) {
234
            $this->comments->removeElement($comment);
235
            // set the owning side to null (unless already changed)
236
            if ($comment->getTrick() === $this) {
237
                $comment->setTrick(null);
238
            }
239
        }
240
241
        return $this;
242
    }
243
244
    /**
245
     * @return Collection|Image[]
246
     */
247
    public function getImages(): Collection
248
    {
249
        return $this->images;
250
    }
251
252
    public function addImage(Image $image): self
253
    {
254
        if (!$this->images->contains($image)) {
255
            $this->images[] = $image;
256
            $image->setTrick($this);
257
        }
258
259
        return $this;
260
    }
261
262
    public function removeImage(Image $image): self
263
    {
264
        if ($this->images->contains($image)) {
265
            $this->images->removeElement($image);
266
            // set the owning side to null (unless already changed)
267
            if ($image->getTrick() === $this) {
268
                $image->setTrick(null);
269
            }
270
        }
271
272
        return $this;
273
    }
274
275
    /**
276
     * Get the primary images associated with the trick
277
     * @return Collection|null
278
     */
279
    public function getPrimaryImages(): ?Collection
280
    {
281
        return $this->getImages()->filter(function (Image $image){
282
            return $image->getPrimaryImage() === true;
283
        });
284
    }
285
}
286