Completed
Push — master ( e17a12...0c99d9 )
by Christophe
07:05 queued 04:25
created

Media::getScore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace AudioCoreEntity\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
8
use Doctrine\ORM\Mapping\UniqueConstraint;
9
use Symfony\Component\Validator\Constraints as Assert;
10
use Symfony\Component\Serializer\Annotation\Groups;
11
use DeejayPoolBundle\Entity\AvdItem;
12
use DeejayPoolBundle\Entity\SvItem;
13
use DeejayPoolBundle\Entity\FranchisePoolItem;
14
15
/**
16
 * Media
17
 *
18
 * @ORM\Table(
19
 *      indexes={@ORM\Index(name="profider_filename", columns={"providerId", "fileName"})},
20
 *      uniqueConstraints={@UniqueConstraint(name="full_file_path_md5", columns={"fullFilePathMd5"})},
21
 *      options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"}
22
 *
23
 * )
24
 * @ORM\Entity(repositoryClass="AudioCoreEntity\Repository\MediaRepository")
25
 */
26
class Media
27
{
28
    const MEDIA_TYPE_AUDIO          = 1;
29
    const MEDIA_TYPE_VIDEO          = 2;
30
    /**
31
     * @var string
32
     *
33
     * @ORM\Column(type="string", length=255, nullable=true)
34
     * @Groups({"media-read"})
35
     */
36
    protected $artist;
37
38
    /**
39
     * @var int
40
     *
41
     * @ORM\Column(type="integer", nullable=true)
42
     * @Groups({"media-read"})
43
     */
44
    protected $bpm;
45
    /**
46
     * @var string
47
     *
48
     * @ORM\Column(type="text", nullable=true)
49
     * @Groups({"media-read"})
50
     *
51
     */
52
    protected $fullPath;
53
    /**
54
     * @var string
55
     *
56
     * @ORM\Column(type="string", length=32, nullable=false)
57
     * @Groups({"media-read"})
58
     *
59
     */
60
    protected $fullFilePathMd5;
61
    /**
62
     * @var string
63
     *
64
     * @ORM\Column(type="string", length=200, nullable=true)
65
     * @Groups({"media-read"})
66
     *
67
     */
68
    protected $dirName;
69
    /**
70
     * @var string
71
     *
72
     * @ORM\Column(type="string", length=255, nullable=true)
73
     * @Groups({"media-read", "artist-read", "genre-read"})
74
     */
75
    protected $title;
76
    /**
77
     * @var \DateTime
78
     *
79
     * @ORM\Column(type="datetime", nullable=true)
80
     * @Groups({"media-read"})
81
     */
82
    protected $releaseDate;
83
    /**
84
     * @var string
85
     *
86
     * @ORM\Column(type="string", length=40, nullable=true)
87
     * @Groups({"media-read"})
88
     */
89
    protected $version;
90
    /**
91
     * @var string
92
     *
93
     * @ORM\Column(type="string", length=255, nullable=true)
94
     * @Groups({"media-read"})
95
     */
96
    protected $fileName;
97
    /**
98
     * @var string
99
     *
100
     * @ORM\Column(type="boolean", nullable=false, options={"default":false})
101
     * @Groups({"media-read"})
102
     */
103
    protected $exist = false;
104
    /**
105
     * @var bool
106
     * @todo remove property and associated method
107
     * @ORM\Column(type="boolean", nullable=false, options={"default":false})
108
     * @Groups({"media-read", "genre-read", "artist-read"})
109
     */
110
    protected $tagged = false;
111
    /**
112
     * @var string
113
     *
114
     * @ORM\Column(type="integer", nullable=true)
115
     * @Groups({"media-read"})
116
     */
117
    protected $score = 0;
118
    /**
119
     * @var \DateTime
120
     *
121
     * @ORM\Column(type="datetime", nullable=true)
122
     * @Groups({"media-read"})
123
     */
124
    protected $deletedAt;
125
    /**
126
     * @var integer
127
     *
128
     * @ORM\Column(name="id", type="integer")
129
     * @ORM\Id
130
     * @ORM\GeneratedValue(strategy="AUTO")
131
     */
132
    protected $id;
133
    /**
134
     * @ORM\ManyToMany(targetEntity="\AudioCoreEntity\Entity\Genre", inversedBy="medias", cascade={"persist", "detach", "refresh"}, fetch="EXTRA_LAZY")
135
     * @ORM\JoinTable(name="media_genre",
136
     *      joinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id")},
137
     *      inverseJoinColumns={@ORM\JoinColumn(name="genre_id", referencedColumnName="id")}
138
     *      )
139
     * @var ArrayCollection<Genre>
140
     * @Groups({"media-read"})
141
     **/
142
    protected $genres;
143
    /**
144
     * @ORM\ManyToMany(targetEntity="\AudioCoreEntity\Entity\Artist", inversedBy="medias", cascade={"persist", "detach", "refresh"}, fetch="EXTRA_LAZY")
145
     * @ORM\JoinTable(
146
     *      joinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id")},
147
     *      inverseJoinColumns={@ORM\JoinColumn(name="artist_id", referencedColumnName="id")}
148
     *      )
149
     * @var ArrayCollection<Artist>
150
     * @Groups({"media-read"})
151
     **/
152
    protected $artists;
153
    /**
154
     * @var integer
155
     *
156
     * @ORM\Column(type="integer", nullable=true)
157
     * @Groups({"media-read"})
158
     */
159
    protected $type;
160
    /**
161
     * @var integer
162
     *
163
     * @ORM\Column(type="integer", length=4, nullable=true)
164
     * @Groups({"media-read", "artist-read", "genre-read"})
165
     */
166
    protected $year;
167
    /**
168
     *
169
     */
170 8
    public function __construct()
171
    {
172 8
        $this->genres = new ArrayCollection();
173 8
        $this->artists = new ArrayCollection();
174 8
    }
175
176 3
    public static function getTypes()
177
    {
178
        return [
179 3
            'audio'         => self::MEDIA_TYPE_AUDIO,
180 3
            'video'         => self::MEDIA_TYPE_VIDEO,
181 3
        ];
182
    }
183
184
    /**
185
     * Get id
186
     *
187
     * @return integer
188
     */
189 1
    public function getId()
190
    {
191 1
        return $this->id;
192
    }
193
194
    /**
195
     * Get artist.
196
     *
197
     * @return string
198
     */
199 1
    public function getArtist()
200
    {
201 1
        return $this->artist;
202
    }
203
204
    /**
205
     * Set artist.
206
     *
207
     * @param string $artist
208
     *
209
     * @return Media
210
     */
211 1
    public function setArtist($artist)
212
    {
213 1
        if ($artist) {
214 1
            $this->artist = substr(trim($artist), 0, 254);
215 1
        }
216
217 1
        return $this;
218
    }
219
220
    /**
221
     * @return int
222
     */
223 2
    public function getBpm()
224
    {
225 2
        return $this->bpm;
226
    }
227
228
    /**
229
     * @param int $bpm
230
     * @return Media
231
     */
232 2
    public function setBpm($bpm)
233
    {
234 2
        if (filter_var($bpm, FILTER_VALIDATE_INT) || filter_var($bpm, FILTER_VALIDATE_FLOAT)) {
235 2
            if ( $bpm <= 160 && $bpm >= 60) {
236 2
                $this->bpm = abs($bpm);
0 ignored issues
show
Documentation Bug introduced by
It seems like abs($bpm) can also be of type double. However, the property $bpm is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
237 2
            }
238 2
        }
239 2
        return $this;
240
    }
241
242
    /**
243
     * @return string
244
     */
245 1
    public function getExist()
246
    {
247 1
        return $this->exist;
248
    }
249
250
    /**
251
     * @param string $exist
252
     * @return $this
253
     */
254 1
    public function setExist($exist)
255
    {
256 1
        $this->exist = $exist;
257 1
        return $this;
258
    }
259
260
    /**
261
     * Get fullPath.
262
     *
263
     * @return string
264
     */
265 1
    public function getFullPath()
266
    {
267 1
        return $this->fullPath;
268
    }
269
270
    /**
271
     * Set fullPath.
272
     *
273
     * @param string $fullPath
274
     *
275
     * @return Media
276
     */
277 1
    public function setFullPath($fullPath)
278
    {
279 1
        $pattern = '#('.DIRECTORY_SEPARATOR.')\1+#';
280 1
        $replacement = DIRECTORY_SEPARATOR;
281 1
        $fullPath = preg_replace($pattern, $replacement, $fullPath);
282
283 1
        $this->fullPath = $fullPath;
284
285 1
        $this->exist = file_exists($fullPath) ? true : true;
0 ignored issues
show
Documentation Bug introduced by
The property $exist was declared of type string, but file_exists($fullPath) ? true : true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
286 1
        $splFile = new \SplFileInfo($this->fullPath);
287 1
        $this->setFileName($splFile->getBasename());
288 1
        $this->setFullFilePathMd5(md5($this->fullPath));
289 1
        $paths = explode('/', $splFile->getPath());
290 1
        $this->setDirName(end($paths));
291
292 1
        return $this;
293
    }
294
295
    /**
296
     * @return string
297
     */
298 1
    public function getFullFilePathMd5()
299
    {
300 1
        return $this->fullFilePathMd5;
301
    }
302
303
    /**
304
     * @param string $fullFilePathMd5
305
     * @return Media
306
     */
307 1
    public function setFullFilePathMd5($fullFilePathMd5)
308
    {
309 1
        if (strlen($fullFilePathMd5) == 32) {
310 1
            $this->fullFilePathMd5 = $fullFilePathMd5;
311 1
        }
312 1
        return $this;
313
    }
314
315
316
317
    /**
318
     * Get title.
319
     *
320
     * @return string
321
     */
322 1
    public function getTitle()
323
    {
324 1
        return $this->title;
325
    }
326
327
    /**
328
     * Set title.
329
     *
330
     * @param string $title
331
     *
332
     * @return Media
333
     */
334 1
    public function setTitle($title)
335
    {
336 1
        if ($title) {
337 1
            $this->title = trim($title);
338 1
        }
339
340 1
        return $this;
341
    }
342
343
    /**
344
     * Get type
345
     *
346
     * @return integer
347
     */
348 1
    public function getType()
349
    {
350 1
        return $this->type;
351
    }
352
353
    /**
354
     * Set type
355
     *
356
     * @param integer $type
357
     * @return Media
358
     */
359 2
    public function setType($type)
360
    {
361 2
        if (!in_array($type, self::getTypes())) {
362 1
            throw new \InvalidArgumentException(sprintf('%s is not a valid type. See %s', $type, self::class.'::getTypes()'));
363
        }
364
365 1
        $this->type = $type;
366
367 1
        return $this;
368
    }
369
370
    /**
371
     * Get releaseDate.
372
     *
373
     * @return \DateTime
374
     */
375 1
    public function getReleaseDate()
376
    {
377 1
        return $this->releaseDate;
378
    }
379
380
    /**
381
     * Set releaseDate.
382
     *
383
     * @param \DateTime $releaseDate
384
     *
385
     * @return Media
386
     */
387 1
    public function setReleaseDate($releaseDate)
388
    {
389 1
        $this->releaseDate = $releaseDate;
390
391 1
        return $this;
392
    }
393
394
    /**
395
     * @param Genre $genre
396
     * @return $this
397
     */
398 1
    public function addGenre(Genre $genre)
399
    {
400 1
        if (!$this->genres->contains($genre)) {
401 1
            $this->genres->add($genre);
402 1
        }
403 1
        return $this;
404
    }
405
406
    /**
407
     * @param Genre $genre
408
     * @return $this
409
     */
410 1
    public function removeGenre(Genre $genre)
411
    {
412 1
        if ($this->genres->contains($genre)) {
413 1
            $this->genres->removeElement($genre);
414 1
        }
415 1
        return $this;
416
    }
417
418
    /**
419
     * @return ArrayCollection
420
     */
421 1
    public function getGenres()
422
    {
423 1
        return $this->genres;
424
    }
425
426
    /**
427
     * Set Genres.
428
     * @param ArrayCollection $genres
429
     * @return $this
430
     */
431 1
    public function setGenres(ArrayCollection $genres)
432
    {
433 1
        $this->genres = $genres;
434
435 1
        return $this;
436
    }
437
    /**
438
     * @param Artist $artist
439
     * @return $this
440
     */
441 1
    public function addArtist(Artist $artist)
442
    {
443 1
        if (!$this->artists->contains($artist)) {
444 1
            $this->artists->add($artist);
445 1
        }
446 1
        return $this;
447
    }
448
449
    /**
450
     * @param Genre $artist
451
     * @return $this
452
     */
453 1
    public function removeArtist(Artist $artist)
454
    {
455 1
        if ($this->artists->contains($artist)) {
456 1
            $this->artists->removeElement($artist);
457 1
        }
458 1
        return $this;
459
    }
460
461
    /**
462
     * @return ArrayCollection
463
     */
464 1
    public function getArtists()
465
    {
466 1
        return $this->artists;
467
    }
468
469
    /**
470
     * @param ArrayCollection $artists
471
     * @return $this
472
     */
473 1
    public function setArtists(ArrayCollection $artists)
474
    {
475 1
        $this->artists = $artists;
476
477 1
        return $this;
478
    }
479
480
    /**
481
     * Get version.
482
     *
483
     * @return string
484
     */
485 1
    public function getVersion()
486
    {
487 1
        return $this->version;
488
    }
489
490
    /**
491
     * Set version.
492
     *
493
     * @param string $version
494
     *
495
     * @return Media
496
     */
497 1
    public function setVersion($version)
498
    {
499 1
        $this->version = trim($version);
500
501 1
        return $this;
502
    }
503
504
    /**
505
     * @return string
506
     */
507 1
    public function getFileName()
508
    {
509 1
        return $this->fileName;
510
    }
511
512
    /**
513
     * @param string $fileName
514
     * @return $this
515
     */
516 1
    public function setFileName($fileName)
517
    {
518 1
        $this->fileName = $fileName;
519
520 1
        return $this;
521
    }
522
523
    /**
524
     * @return string
525
     */
526 1
    public function getScore()
527
    {
528 1
        return $this->score;
529
    }
530
531
    /**
532
     * @param string $score
533
     * @return Media
534
     */
535 1
    public function setScore($score)
536
    {
537 1
        if (filter_var($score, FILTER_VALIDATE_INT) || filter_var($score, FILTER_VALIDATE_FLOAT)) {
538 1
            $this->score = $score;
539 1
        }
540 1
        return $this;
541
    }
542
543
    /**
544
     * @return \DateTime
545
     */
546 1
    public function getDeletedAt()
547
    {
548 1
        return $this->deletedAt;
549
    }
550
551
    /**
552
     * @param \DateTime $deletedAt
553
     * @return Media
554
     */
555 1
    public function setDeletedAt($deletedAt)
556
    {
557 1
        $this->deletedAt = $deletedAt;
558 1
        return $this;
559
    }
560
561
    /**
562
     * @return string
563
     */
564 1
    public function getDirName()
565
    {
566 1
        return $this->dirName;
567
    }
568
569
    /**
570
     * @param string $dirName
571
     * @return Media
572
     */
573 1
    public function setDirName($dirName)
574
    {
575 1
        $this->dirName = $dirName;
576 1
        return $this;
577
    }
578
    public function isUntaged() {
579
        // @codeCoverageIgnoreStart
580
        return !$this->tagged;
581
        // @codeCoverageIgnoreEnd
582
    }
583
584
    /**
585
     * @return int
586
     */
587 1
    public function getYear()
588
    {
589 1
        return $this->year;
590
    }
591
592
    /**
593
     * @param int $year
594
     * @return Media
595
     */
596 2
    public function setYear($year)
597
    {
598 2
        if (preg_match('/\d{4}/', $year)) {
599 1
            $this->year = $year;
600 1
        }
601 2
        return $this;
602
    }
603
604
    /**
605
     * @return string
606
     */
607
    public function getTagged()
608
    {
609
        // @codeCoverageIgnoreStart
610
        return $this->tagged;
611
        // @codeCoverageIgnoreEnd
612
    }
613
614
    /**
615
     * @param bool $tagged
616
     * @return Media
617
     */
618
    public function setTagged($tagged)
619
    {
620
        // @codeCoverageIgnoreStart
621
        $this->tagged = $tagged;
622
        return $this;
623
        // @codeCoverageIgnoreEnd
624
    }
625
626
}
627