Passed
Push — develop ( 6aacf8...6c0b7f )
by BENARD
11:52
created

Video   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 24
eloc 63
c 2
b 0
f 0
dl 0
loc 270
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A getVideoId() 0 3 1
A setType() 0 4 1
A getLibVideo() 0 3 1
A getComments() 0 3 1
A majTypeAndVideoId() 0 15 4
A setVideoId() 0 4 1
A getType() 0 3 1
A setGame() 0 5 1
A getUrl() 0 3 1
A getGame() 0 3 1
A setLibVideo() 0 5 1
A __toString() 0 3 1
A setUrl() 0 5 1
A setId() 0 4 1
A getEmbeddedUrl() 0 9 3
A getSluggableFields() 0 3 1
A getNbComment() 0 3 1
A setNbComment() 0 5 1
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Entity;
4
5
use ApiPlatform\Core\Annotation\ApiFilter;
6
use ApiPlatform\Core\Annotation\ApiResource;
7
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
8
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
9
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
10
use Doctrine\Common\Collections\Collection;
11
use Doctrine\ORM\Mapping as ORM;
12
use Gedmo\Timestampable\Traits\TimestampableEntity;
13
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
14
use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait;
15
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
16
use Symfony\Component\Validator\Constraints as Assert;
17
use VideoGamesRecords\CoreBundle\Traits\Entity\DescriptionTrait;
18
use VideoGamesRecords\CoreBundle\Traits\Entity\IsActiveTrait;
19
use VideoGamesRecords\CoreBundle\Traits\Entity\LikeCountTrait;
20
use VideoGamesRecords\CoreBundle\Traits\Entity\Player\PlayerTrait;
21
use VideoGamesRecords\CoreBundle\Traits\Entity\ThumbnailTrait;
22
use VideoGamesRecords\CoreBundle\Traits\Entity\TitleTrait;
23
use VideoGamesRecords\CoreBundle\Traits\Entity\ViewCountTrait;
24
use VideoGamesRecords\CoreBundle\ValueObject\VideoType;
25
26
/**
27
 * @ORM\Table(
28
 *     name="vgr_video",
29
 *     uniqueConstraints={
30
 *         @ORM\UniqueConstraint(name="unq_video", columns={"type", "videoId"})
31
 *     }
32
 * )
33
 * @ORM\Entity(repositoryClass="VideoGamesRecords\CoreBundle\Repository\VideoRepository")
34
 * @ORM\EntityListeners({"VideoGamesRecords\CoreBundle\EventListener\Entity\VideoListener"})
35
 * @ApiResource(attributes={"order"={"id": "DESC"}})
36
 * @ApiFilter(
37
 *     OrderFilter::class,
38
 *     properties={
39
 *         "id": "DESC"
40
 *     },
41
 *     arguments={"orderParameterName"="order"}
42
 * )
43
 * @ApiFilter(BooleanFilter::class, properties={"boolActive"})
44
 * @ApiFilter(
45
 *     SearchFilter::class,
46
 *     properties={
47
 *          "libVideo": "partial",
48
 *          "game": "exact",
49
 *          "type": "exact",
50
 *          "player": "exact",
51
 *          "isActive": "exact"
52
 *      }
53
 * )
54
 * @DoctrineAssert\UniqueEntity(fields={"url"})
55
 * @DoctrineAssert\UniqueEntity(fields={"type", "videoId"})
56
 */
57
class Video implements SluggableInterface
58
{
59
    use TimestampableEntity;
60
    use SluggableTrait;
61
    use PlayerTrait;
62
    use ViewCountTrait;
63
    use LikeCountTrait;
64
    use TitleTrait;
65
    use DescriptionTrait;
66
    use ThumbnailTrait;
67
    use IsActiveTrait;
68
69
    /**
70
     * @ORM\Column(name="id", type="integer")
71
     * @ORM\Id
72
     * @ORM\GeneratedValue(strategy="IDENTITY")
73
     */
74
    private ?int $id = null;
75
76
    /**
77
     * @ORM\Column(name="type", type="string", length=30, nullable=false)
78
     */
79
    private string $type = VideoType::TYPE_YOUTUBE;
80
81
    /**
82
     * @Assert\NotNull(message="video.videoId.not_null")
83
     * @ORM\Column(name="videoId", type="string", length=50, nullable=true)
84
     */
85
    private ?string $videoId = null;
86
87
    /**
88
     * @Assert\NotBlank()
89
     * @Assert\Length(min="5", max="255")
90
     * @ORM\Column(name="url", type="string", length=255, nullable=true)
91
     */
92
    private ?string $url = null;
93
94
    /**
95
     * @Assert\NotBlank()
96
     * @Assert\Length(min="5", max="200")
97
     * @ORM\Column(name="libVideo", type="string", length=200, nullable=false)
98
     */
99
    private string $libVideo;
100
101
    /**
102
     * @ORM\Column(name="nbComment", type="integer", nullable=false, options={"default":0})
103
     */
104
    private int $nbComment = 0;
105
106
    /**
107
     * @Assert\NotNull
108
     * @ORM\ManyToOne(targetEntity="VideoGamesRecords\CoreBundle\Entity\Game")
109
     * @ORM\JoinColumns({
110
     *   @ORM\JoinColumn(name="idGame", referencedColumnName="id", nullable=true, onDelete="SET NULL")
111
     * })
112
     */
113
    private ?Game $game;
114
115
    /**
116
     * @var Collection<VideoComment>
117
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\VideoComment", mappedBy="video")
118
     */
119
    private Collection $comments;
120
121
    /**
122
     * @return string
123
     */
124
    public function __toString()
125
    {
126
        return sprintf('Video [%s]', $this->id);
127
    }
128
129
    /**
130
     * Set id
131
     * @param integer $id
132
     * @return Video
133
     */
134
    public function setId(int $id): Video
135
    {
136
        $this->id = $id;
137
        return $this;
138
    }
139
140
    /**
141
     * Get id
142
     * @return int|null
143
     */
144
    public function getId(): ?int
145
    {
146
        return $this->id;
147
    }
148
149
150
    /**
151
     * Set type
152
     * @param string $type
153
     * @return Video
154
     */
155
    public function setType(string $type): Video
156
    {
157
        $this->type = $type;
158
        return $this;
159
    }
160
161
    /**
162
     * Get type
163
     * @return VideoType
164
     */
165
    public function getType(): VideoType
166
    {
167
        return new VideoType($this->type);
168
    }
169
170
171
    /**
172
     * Set videoId
173
     * @param string $videoId
174
     * @return $this
175
     */
176
    public function setVideoId(string $videoId): Video
177
    {
178
        $this->videoId = $videoId;
179
        return $this;
180
    }
181
182
    /**
183
     * Get videoId
184
     * @return string|null
185
     */
186
    public function getVideoId(): ?string
187
    {
188
        return $this->videoId;
189
    }
190
191
    /**
192
     * Set libVideo
193
     * @param string $libVideo
194
     * @return Video
195
     */
196
    public function setLibVideo(string $libVideo): Video
197
    {
198
        $this->libVideo = $libVideo;
199
200
        return $this;
201
    }
202
203
    /**
204
     * Get libVideo
205
     * @return string
206
     */
207
    public function getLibVideo(): string
208
    {
209
        return $this->libVideo;
210
    }
211
212
    /**
213
     * Set url
214
     * @param string|null $url
215
     * @return Video
216
     */
217
    public function setUrl(?string $url): Video
218
    {
219
        $this->url = $url;
220
        $this->majTypeAndVideoId();
221
        return $this;
222
    }
223
224
    /**
225
     * Get url
226
     * @return string|null
227
     */
228
    public function getUrl(): ?string
229
    {
230
        return $this->url;
231
    }
232
233
    /**
234
     * Set game
235
     * @param Game|null $game
236
     * @return Video
237
     */
238
    public function setGame(Game $game = null): Video
239
    {
240
        $this->game = $game;
241
242
        return $this;
243
    }
244
245
    /**
246
     * Set nbComment
247
     * @param integer $nbComment
248
     * @return $this
249
     */
250
    public function setNbComment(int $nbComment): Video
251
    {
252
        $this->nbComment = $nbComment;
253
254
        return $this;
255
    }
256
257
    /**
258
     * Get nbComment
259
     * @return integer
260
     */
261
    public function getNbComment(): int
262
    {
263
        return $this->nbComment;
264
    }
265
266
    /**
267
     * Get game
268
     * @return Game|null
269
     */
270
    public function getGame(): ?Game
271
    {
272
        return $this->game;
273
    }
274
275
    /**
276
     * @return Collection
277
     */
278
    public function getComments(): Collection
279
    {
280
        return $this->comments;
281
    }
282
283
    /**
284
     *
285
     */
286
    public function majTypeAndVideoId()
287
    {
288
        if (strpos($this->getUrl(), 'youtube')) {
0 ignored issues
show
Bug introduced by
It seems like $this->getUrl() can also be of type null; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

288
        if (strpos(/** @scrutinizer ignore-type */ $this->getUrl(), 'youtube')) {
Loading history...
289
            $this->setType(VideoType::TYPE_YOUTUBE);
290
            $explode = explode('=', $this->getUrl());
0 ignored issues
show
Bug introduced by
It seems like $this->getUrl() can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

290
            $explode = explode('=', /** @scrutinizer ignore-type */ $this->getUrl());
Loading history...
291
            $this->setVideoId($explode[1]);
292
        } elseif (strpos($this->getUrl(), 'youtu.be')) {
293
            $this->setType(VideoType::TYPE_YOUTUBE);
294
            $this->setVideoId(substr($this->getUrl(), strripos($this->getUrl(), '/') + 1, strlen($this->getUrl()) - 1));
0 ignored issues
show
Bug introduced by
It seems like $this->getUrl() can also be of type null; however, parameter $haystack of strripos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

294
            $this->setVideoId(substr($this->getUrl(), strripos(/** @scrutinizer ignore-type */ $this->getUrl(), '/') + 1, strlen($this->getUrl()) - 1));
Loading history...
Bug introduced by
It seems like $this->getUrl() can also be of type null; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

294
            $this->setVideoId(substr(/** @scrutinizer ignore-type */ $this->getUrl(), strripos($this->getUrl(), '/') + 1, strlen($this->getUrl()) - 1));
Loading history...
Bug introduced by
It seems like $this->getUrl() can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

294
            $this->setVideoId(substr($this->getUrl(), strripos($this->getUrl(), '/') + 1, strlen(/** @scrutinizer ignore-type */ $this->getUrl()) - 1));
Loading history...
295
        } elseif (strpos($this->getUrl(), 'twitch')) {
296
            $this->setType(VideoType::TYPE_TWITCH);
297
            $explode = explode('/', $this->getUrl());
298
            $this->setVideoId($explode[count($explode) - 1]);
299
        } else {
300
            $this->setType(VideoType::TYPE_UNKNOWN);
301
        }
302
    }
303
304
305
    /**
306
     * @return string
307
     */
308
    public function getEmbeddedUrl(): string
309
    {
310
        if ($this->getType()->getValue() == VideoType::TYPE_YOUTUBE) {
311
            return 'https://www.youtube.com/embed/' . $this->getVideoId();
312
        } elseif ($this->getType()->getValue() == VideoType::TYPE_TWITCH) {
313
            return 'https://player.twitch.tv/?autoplay=false&video=v' . $this->getVideoId(
314
                ) . '&parent=' . $_SERVER['SERVER_NAME'];
315
        } else {
316
            return $this->getUrl();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getUrl() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
317
        }
318
    }
319
320
    /**
321
     * Returns an array of the fields used to generate the slug.
322
     * @return string[]
323
     */
324
    public function getSluggableFields(): array
325
    {
326
        return ['libVideo'];
327
    }
328
}
329