Completed
Push — master ( 29a47a...e17a12 )
by Christophe
22s
created

Media::getProviders()   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 string
78
     *
79
     * @ORM\Column(type="text", nullable=true)
80
     * @Groups({"media-read"})
81
     */
82
    protected $providerUrl;
83
    /**
84
     * @var \DateTime
85
     *
86
     * @ORM\Column(type="datetime", nullable=true)
87
     * @Groups({"media-read"})
88
     */
89
    protected $releaseDate;
90
    /**
91
     * @var string
92
     *
93
     * @ORM\Column(type="string", length=40, nullable=true)
94
     * @Groups({"media-read"})
95
     */
96
    protected $version;
97
    /**
98
     * @var string
99
     *
100
     * @ORM\Column(type="string", length=255, nullable=true)
101
     * @Groups({"media-read"})
102
     */
103
    protected $fileName;
104
    /**
105
     * @var string
106
     *
107
     * @ORM\Column(type="boolean", nullable=false, options={"default":false})
108
     * @Groups({"media-read"})
109
     */
110
    protected $exist = false;
111
    /**
112
     * @var bool
113
     * @todo remove property and associated method
114
     * @ORM\Column(type="boolean", nullable=false, options={"default":false})
115
     * @Groups({"media-read", "genre-read", "artist-read"})
116
     */
117
    protected $tagged = false;
118
    /**
119
     * @var string
120
     *
121
     * @ORM\Column(type="integer", nullable=true)
122
     * @Groups({"media-read"})
123
     */
124
    protected $score = 0;
125
    /**
126
     * @var \DateTime
127
     *
128
     * @ORM\Column(type="datetime", nullable=true)
129
     * @Groups({"media-read"})
130
     */
131
    protected $deletedAt;
132
    /**
133
     * @var integer
134
     *
135
     * @ORM\Column(name="id", type="integer")
136
     * @ORM\Id
137
     * @ORM\GeneratedValue(strategy="AUTO")
138
     */
139
    protected $id;
140
    /**
141
     * @ORM\ManyToMany(targetEntity="\AudioCoreEntity\Entity\Genre", inversedBy="medias", cascade={"persist", "detach", "refresh"}, fetch="EXTRA_LAZY")
142
     * @ORM\JoinTable(name="media_genre",
143
     *      joinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id")},
144
     *      inverseJoinColumns={@ORM\JoinColumn(name="genre_id", referencedColumnName="id")}
145
     *      )
146
     * @var ArrayCollection<Genre>
147
     * @Groups({"media-read"})
148
     **/
149
    protected $genres;
150
    /**
151
     * @ORM\ManyToMany(targetEntity="\AudioCoreEntity\Entity\Artist", inversedBy="medias", cascade={"persist", "detach", "refresh"}, fetch="EXTRA_LAZY")
152
     * @ORM\JoinTable(
153
     *      joinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id")},
154
     *      inverseJoinColumns={@ORM\JoinColumn(name="artist_id", referencedColumnName="id")}
155
     *      )
156
     * @var ArrayCollection<Artist>
157
     * @Groups({"media-read"})
158
     **/
159
    protected $artists;
160
    /**
161
     * @var integer
162
     *
163
     * @ORM\Column(type="integer", nullable=true)
164
     * @Groups({"media-read"})
165
     */
166
    protected $type;
167
    /**
168
     * @var integer
169
     *
170
     * @ORM\Column(type="integer", length=4, nullable=true)
171
     * @Groups({"media-read", "artist-read", "genre-read"})
172
     */
173
    protected $year;
174
    /**
175
     * @var integer
176
     *
177
     * ORM\Column(name="provider", type="integer")
178
     * @Groups({"media-read"})
179
     */
180
    //protected $provider;
181
182
    /**
183
     * @var integer
184
     * @todo change property name to externalId and update related method
185
     * @ORM\Column(type="string", length=30, nullable=true)
186
     * @Groups({"media-read"})
187
     */
188
    protected $providerId;
189
190
    /**
191
     *
192
     */
193 8
    public function __construct()
194
    {
195 8
        $this->genres = new ArrayCollection();
196 8
        $this->artists = new ArrayCollection();
197 8
    }
198
199 3
    public static function getTypes()
200
    {
201
        return [
202 3
            'audio'         => self::MEDIA_TYPE_AUDIO,
203 3
            'video'         => self::MEDIA_TYPE_VIDEO,
204 3
        ];
205
    }
206
207
    /**
208
     * Get id
209
     *
210
     * @return integer
211
     */
212 1
    public function getId()
213
    {
214 1
        return $this->id;
215
    }
216
217
    /**
218
     * Get artist.
219
     *
220
     * @return string
221
     */
222 1
    public function getArtist()
223
    {
224 1
        return $this->artist;
225
    }
226
227
    /**
228
     * Set artist.
229
     *
230
     * @param string $artist
231
     *
232
     * @return Media
233
     */
234 1
    public function setArtist($artist)
235
    {
236 1
        if ($artist) {
237 1
            $this->artist = substr(trim($artist), 0, 254);
238 1
        }
239
240 1
        return $this;
241
    }
242
243
    /**
244
     * @return int
245
     */
246 2
    public function getBpm()
247
    {
248 2
        return $this->bpm;
249
    }
250
251
    /**
252
     * @param int $bpm
253
     * @return Media
254
     */
255 2
    public function setBpm($bpm)
256
    {
257 2
        if (filter_var($bpm, FILTER_VALIDATE_INT) || filter_var($bpm, FILTER_VALIDATE_FLOAT)) {
258 2
            if ( $bpm <= 160 && $bpm >= 60) {
259 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...
260 2
            }
261 2
        }
262 2
        return $this;
263
    }
264
265
    /**
266
     * @return string
267
     */
268 1
    public function getExist()
269
    {
270 1
        return $this->exist;
271
    }
272
273
    /**
274
     * @param string $exist
275
     * @return $this
276
     */
277 1
    public function setExist($exist)
278
    {
279 1
        $this->exist = $exist;
280 1
        return $this;
281
    }
282
283
    /**
284
     * Get downloadlink.
285
     *
286
     * @return string
287
     */
288
    public function getProviderUrl()
289
    {
290
        return $this->providerUrl;
291
    }
292
293
    /**
294
     * Set downloadlink.
295
     *
296
     * @param string $providerUrl
297
     *
298
     * @return Media
299
     */
300
    public function setProviderUrl($providerUrl)
301
    {
302
        if (filter_var($providerUrl, FILTER_VALIDATE_URL)) {
303
            $this->providerUrl = $providerUrl;
304
        }
305
        return $this;
306
    }
307
308
    /**
309
     * Get fullPath.
310
     *
311
     * @return string
312
     */
313 1
    public function getFullPath()
314
    {
315 1
        return $this->fullPath;
316
    }
317
318
    /**
319
     * Set fullPath.
320
     *
321
     * @param string $fullPath
322
     *
323
     * @return Media
324
     */
325 1
    public function setFullPath($fullPath)
326
    {
327 1
        $pattern = '#('.DIRECTORY_SEPARATOR.')\1+#';
328 1
        $replacement = DIRECTORY_SEPARATOR;
329 1
        $fullPath = preg_replace($pattern, $replacement, $fullPath);
330
331 1
        $this->fullPath = $fullPath;
332
333 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...
334 1
        $splFile = new \SplFileInfo($this->fullPath);
335 1
        $this->setFileName($splFile->getBasename());
336 1
        $this->setFullFilePathMd5(md5($this->fullPath));
337 1
        $paths = explode('/', $splFile->getPath());
338 1
        $this->setDirName(end($paths));
339
340 1
        return $this;
341
    }
342
343
    /**
344
     * @return string
345
     */
346 1
    public function getFullFilePathMd5()
347
    {
348 1
        return $this->fullFilePathMd5;
349
    }
350
351
    /**
352
     * @param string $fullFilePathMd5
353
     * @return Media
354
     */
355 1
    public function setFullFilePathMd5($fullFilePathMd5)
356
    {
357 1
        if (strlen($fullFilePathMd5) == 32) {
358 1
            $this->fullFilePathMd5 = $fullFilePathMd5;
359 1
        }
360 1
        return $this;
361
    }
362
363
364
365
    /**
366
     * Get title.
367
     *
368
     * @return string
369
     */
370 1
    public function getTitle()
371
    {
372 1
        return $this->title;
373
    }
374
375
    /**
376
     * Set title.
377
     *
378
     * @param string $title
379
     *
380
     * @return Media
381
     */
382 1
    public function setTitle($title)
383
    {
384 1
        if ($title) {
385 1
            $this->title = trim($title);
386 1
        }
387
388 1
        return $this;
389
    }
390
391
    /**
392
     * Get type
393
     *
394
     * @return integer
395
     */
396 1
    public function getType()
397
    {
398 1
        return $this->type;
399
    }
400
401
    /**
402
     * Set type
403
     *
404
     * @param integer $type
405
     * @return Media
406
     */
407 2
    public function setType($type)
408
    {
409 2
        if (!in_array($type, self::getTypes())) {
410 1
            throw new \InvalidArgumentException(sprintf('%s is not a valid type. See %s', $type, self::class.'::getTypes()'));
411
        }
412
413 1
        $this->type = $type;
414
415 1
        return $this;
416
    }
417
418
    /**
419
     * @return int
420
     */
421
    public function getProviderId()
422
    {
423
        return $this->providerId;
424
    }
425
426
    /**
427
     * @param int $providerId
428
     * @return Media
429
     */
430 1
    public function setProviderId($providerId)
431
    {
432 1
        $this->providerId = $providerId;
433 1
        return $this;
434
    }
435
436
    /**
437
     * Get releaseDate.
438
     *
439
     * @return \DateTime
440
     */
441 1
    public function getReleaseDate()
442
    {
443 1
        return $this->releaseDate;
444
    }
445
446
    /**
447
     * Set releaseDate.
448
     *
449
     * @param \DateTime $releaseDate
450
     *
451
     * @return Media
452
     */
453 1
    public function setReleaseDate($releaseDate)
454
    {
455 1
        $this->releaseDate = $releaseDate;
456
457 1
        return $this;
458
    }
459
460
    /**
461
     * @param Genre $genre
462
     * @return $this
463
     */
464 1
    public function addGenre(Genre $genre)
465
    {
466 1
        if (!$this->genres->contains($genre)) {
467 1
            $this->genres->add($genre);
468 1
        }
469 1
        return $this;
470
    }
471
472
    /**
473
     * @param Genre $genre
474
     * @return $this
475
     */
476 1
    public function removeGenre(Genre $genre)
477
    {
478 1
        if ($this->genres->contains($genre)) {
479 1
            $this->genres->removeElement($genre);
480 1
        }
481 1
        return $this;
482
    }
483
484
    /**
485
     * @return ArrayCollection
486
     */
487 1
    public function getGenres()
488
    {
489 1
        return $this->genres;
490
    }
491
492
    /**
493
     * Set Genres.
494
     * @param ArrayCollection $genres
495
     * @return $this
496
     */
497 1
    public function setGenres(ArrayCollection $genres)
498
    {
499 1
        $this->genres = $genres;
500
501 1
        return $this;
502
    }
503
    /**
504
     * @param Artist $artist
505
     * @return $this
506
     */
507 1
    public function addArtist(Artist $artist)
508
    {
509 1
        if (!$this->artists->contains($artist)) {
510 1
            $this->artists->add($artist);
511 1
        }
512 1
        return $this;
513
    }
514
515
    /**
516
     * @param Genre $artist
517
     * @return $this
518
     */
519 1
    public function removeArtist(Artist $artist)
520
    {
521 1
        if ($this->artists->contains($artist)) {
522 1
            $this->artists->removeElement($artist);
523 1
        }
524 1
        return $this;
525
    }
526
527
    /**
528
     * @return ArrayCollection
529
     */
530 1
    public function getArtists()
531
    {
532 1
        return $this->artists;
533
    }
534
535
    /**
536
     * @param ArrayCollection $artists
537
     * @return $this
538
     */
539 1
    public function setArtists(ArrayCollection $artists)
540
    {
541 1
        $this->artists = $artists;
542
543 1
        return $this;
544
    }
545
546
    /**
547
     * Get version.
548
     *
549
     * @return string
550
     */
551 1
    public function getVersion()
552
    {
553 1
        return $this->version;
554
    }
555
556
    /**
557
     * Set version.
558
     *
559
     * @param string $version
560
     *
561
     * @return Media
562
     */
563 1
    public function setVersion($version)
564
    {
565 1
        $this->version = trim($version);
566
567 1
        return $this;
568
    }
569
570
    /**
571
     * @return string
572
     */
573 1
    public function getFileName()
574
    {
575 1
        return $this->fileName;
576
    }
577
578
    /**
579
     * @param string $fileName
580
     * @return $this
581
     */
582 1
    public function setFileName($fileName)
583
    {
584 1
        $this->fileName = $fileName;
585
586 1
        return $this;
587
    }
588
589
    /**
590
     * @return string
591
     */
592 1
    public function getScore()
593
    {
594 1
        return $this->score;
595
    }
596
597
    /**
598
     * @param string $score
599
     * @return Media
600
     */
601 1
    public function setScore($score)
602
    {
603 1
        if (filter_var($score, FILTER_VALIDATE_INT) || filter_var($score, FILTER_VALIDATE_FLOAT)) {
604 1
            $this->score = $score;
605 1
        }
606 1
        return $this;
607
    }
608
609
    /**
610
     * @return array
611
     * @Groups({"media-read"})
612
     */
613
    public function getProviderCode()
614
    {
615
        $key = array_search($this->getProvider(), $this->getProviders());
0 ignored issues
show
Bug introduced by
The method getProviders() does not exist on AudioCoreEntity\Entity\Media. Did you maybe mean getProvider()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
616
        return $key;
617
    }
618
619
    /**
620
     * Get provider
621
     *
622
     * @return integer
623
     */
624
    public function getProvider()
625
    {
626
        return $this->provider;
0 ignored issues
show
Bug introduced by
The property provider does not seem to exist. Did you mean providerUrl?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
627
    }
628
629
    /**
630
     * @return \DateTime
631
     */
632 1
    public function getDeletedAt()
633
    {
634 1
        return $this->deletedAt;
635
    }
636
637
    /**
638
     * @param \DateTime $deletedAt
639
     * @return Media
640
     */
641 1
    public function setDeletedAt($deletedAt)
642
    {
643 1
        $this->deletedAt = $deletedAt;
644 1
        return $this;
645
    }
646
647
    /**
648
     * @return string
649
     */
650 1
    public function getDirName()
651
    {
652 1
        return $this->dirName;
653
    }
654
655
    /**
656
     * @param string $dirName
657
     * @return Media
658
     */
659 1
    public function setDirName($dirName)
660
    {
661 1
        $this->dirName = $dirName;
662 1
        return $this;
663
    }
664
    public function isUntaged() {
665
        // @codeCoverageIgnoreStart
666
        return !$this->tagged;
667
        // @codeCoverageIgnoreEnd
668
    }
669
670
    /**
671
     * @return int
672
     */
673 1
    public function getYear()
674
    {
675 1
        return $this->year;
676
    }
677
678
    /**
679
     * @param int $year
680
     * @return Media
681
     */
682 2
    public function setYear($year)
683
    {
684 2
        if (preg_match('/\d{4}/', $year)) {
685 1
            $this->year = $year;
686 1
        }
687 2
        return $this;
688
    }
689
690
691
    /**
692
     * @todo Refactor. Implementation specific
693
     * @return $this
694
     */
695
    public function updateProviderId()
696
    {
697
        // @codeCoverageIgnoreStart
698
        $fileName   = $this->getFileName();
699
        $provider   = $this->getProvider();
700
        $patern     = '/^(?P<providerId>\d{1,9})\_/';
701
702
        if ($fileName && $provider && in_array($provider, $this->getProviders())) {
0 ignored issues
show
Bug introduced by
The method getProviders() does not exist on AudioCoreEntity\Entity\Media. Did you maybe mean getProvider()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
703
            if ($provider === Media::PROVIDER_SMASHVISION) {
704
                $patern = '/^(?P<providerId>\d{1,9}\_\d{1,9})\_/';
705
            }
706
707
            if (preg_match($patern, $fileName, $matches)){
708
                $this->setProviderId($matches['providerId']);
709
            }
710
        }
711
712
        return $this;
713
        // @codeCoverageIgnoreEnd
714
    }
715
716
    /**
717
     * @return string
718
     */
719
    public function getTagged()
720
    {
721
        // @codeCoverageIgnoreStart
722
        return $this->tagged;
723
        // @codeCoverageIgnoreEnd
724
    }
725
726
    /**
727
     * @param bool $tagged
728
     * @return Media
729
     */
730
    public function setTagged($tagged)
731
    {
732
        // @codeCoverageIgnoreStart
733
        $this->tagged = $tagged;
734
        return $this;
735
        // @codeCoverageIgnoreEnd
736
    }
737
738
}
739