Completed
Pull Request — master (#5)
by Christophe
02:50
created

Media::getProviderFromItem()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 8.8571
cc 6
eloc 12
nc 6
nop 1
crap 42

1 Method

Rating   Name   Duplication   Size   Complexity  
A Media::setDeletedAt() 0 5 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
 * @UniqueEntity(fields={"provider","fileName"})
26
 */
27
class Media
28
{
29
30
    const PROVIDER_DIGITAL_DJ_POOL  = 1;
31
    const PROVIDER_AV_DISTRICT      = 2;
32
    const PROVIDER_FRP_AUDIO        = 3;
33
    const PROVIDER_FRP_VIDEO        = 4;
34
    const PROVIDER_SMASHVISION      = 5;
35
36
    static public $providers = [
37
        'av_district'       => self::PROVIDER_AV_DISTRICT,
38
        'frp_video'         => self::PROVIDER_FRP_VIDEO,
39
        'frp_audio'         => self::PROVIDER_FRP_AUDIO,
40
        'ddp'               => self::PROVIDER_DIGITAL_DJ_POOL,
41
        'sv'                => self::PROVIDER_SMASHVISION,
42
    ];
43
44
    const MEDIA_TYPE_AUDIO          = 1;
45
    const MEDIA_TYPE_VIDEO          = 2;
46
    /**
47
     * @var string
48
     *
49
     * @ORM\Column(type="string", length=255, nullable=true)
50
     * @Groups({"media-read"})
51
     */
52
    protected $artist;
53
54
    /**
55
     * @var float
56
     *
57
     * @ORM\Column(type="float", nullable=true)
58
     * @Groups({"media-read"})
59
     */
60
    protected $bpm;
61
    /**
62
     * @var string
63
     *
64
     * @ORM\Column(type="text", nullable=true)
65
     * @Groups({"media-read"})
66
     *
67
     */
68
    protected $fullPath;
69
70
    /**
71
     * @var string
72
     *
73
     * @ORM\Column(type="string", length=32, nullable=false)
74
     * @Groups({"media-read"})
75
     *
76
     */
77
    protected $fullFilePathMd5;
78
79
    /**
80
     * @var string
81
     *
82
     * @ORM\Column(type="string", length=200, nullable=true)
83
     * @Groups({"media-read"})
84
     *
85
     */
86
    protected $dirName;
87
88
89
    /**
90
     * @var string
91
     *
92
     * @ORM\Column(type="string", length=255, nullable=true)
93
     * @Groups({"media-read", "artist-read", "genre-read"})
94
     */
95
    protected $title;
96
    /**
97
     * @var string
98
     *
99
     * @ORM\Column(type="text", nullable=true)
100
     * @Groups({"media-read"})
101
     */
102
    protected $providerUrl;
103
    /**
104
     * @var \DateTime
105
     *
106
     * @ORM\Column(type="datetime", nullable=true)
107
     * @Groups({"media-read"})
108
     */
109
    protected $releaseDate;
110
    /**
111
     * @var string
112
     *
113
     * @ORM\Column(type="string", length=40, nullable=true)
114
     * @Groups({"media-read"})
115
     */
116
    protected $version;
117
    /**
118
     * @var string
119
     *
120
     * @ORM\Column(type="string", length=255, nullable=true)
121
     * @Groups({"media-read"})
122
     */
123
    protected $fileName;
124
    /**
125
     * @var boolean
126
     *
127
     * @ORM\Column(type="boolean", nullable=false, options={"default":false})
128
     * @Groups({"media-read"})
129
     */
130
    protected $exist = false;
131
    /**
132
     * @var bool
133
     * @todo remove property and associated method
134
     * @ORM\Column(type="boolean", nullable=false, options={"default":false})
135
     * @Groups({"media-read", "genre-read", "artist-read"})
136
     */
137
    protected $tagged = false;
138
    /**
139
     * @var string
140
     *
141
     * @ORM\Column(type="integer", nullable=true)
142
     * @Groups({"media-read"})
143
     */
144
    protected $score = 0;
145
    /**
146
     * @var \DateTime
147
     *
148
     * @ORM\Column(type="datetime", nullable=true)
149
     * @Groups({"media-read"})
150
     */
151
    protected $deletedAt;
152
    /**
153
     * @var integer
154
     *
155
     * @ORM\Column(name="id", type="integer")
156
     * @ORM\Id
157
     * @ORM\GeneratedValue(strategy="AUTO")
158
     */
159
    protected $id;
160
    /**
161
     * @ORM\ManyToMany(targetEntity="Genre", inversedBy="medias", cascade={"persist", "detach", "refresh"}, fetch="EXTRA_LAZY")
162
     * @ORM\JoinTable(name="media_genre",
163
     *      joinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id")},
164
     *      inverseJoinColumns={@ORM\JoinColumn(name="genre_id", referencedColumnName="id")}
165
     *      )
166
     * @var ArrayCollection<Genre>
167
     * @Groups({"media-read"})
168
     **/
169
    protected $genres;
170
    /**
171
     * @ORM\ManyToMany(targetEntity="Artist", inversedBy="medias", cascade={"persist", "detach", "refresh"}, fetch="EXTRA_LAZY")
172
     * @ORM\JoinTable(
173
     *      joinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id")},
174
     *      inverseJoinColumns={@ORM\JoinColumn(name="artist_id", referencedColumnName="id")}
175
     *      )
176
     * @var ArrayCollection<Artist>
177
     * @Groups({"media-read"})
178
     **/
179
    protected $artists;
180
    /**
181
     * @var integer
182
     *
183
     * @ORM\Column(type="integer", nullable=true)
184
     * @Groups({"media-read"})
185
     */
186
    protected $type;
187
    /**
188
     * @var integer
189
     *
190
     * @ORM\Column(type="integer", length=4, nullable=true)
191
     * @Groups({"media-read", "artist-read", "genre-read"})
192
     */
193
    protected $year;
194
    /**
195
     * @var integer
196
     *
197
     * @ORM\Column(name="provider", type="integer")
198
     * @Groups({"media-read"})
199
     */
200
    protected $provider;
201
202
    /**
203
     * @var integer
204
     * @todo change property name to externalId and update related method
205
     * @ORM\Column(type="string", length=30, nullable=true)
206
     * @Groups({"media-read"})
207
     */
208
    protected $providerId;
209
210
    /**
211
     *
212
     */
213 11
    public function __construct()
214
    {
215 11
        $this->genres = new ArrayCollection();
216 11
        $this->artists = new ArrayCollection();
217 11
    }
218
219 3
    public static function getTypes()
220
    {
221
        return [
222 3
            'audio'         => self::MEDIA_TYPE_AUDIO,
223 3
            'video'         => self::MEDIA_TYPE_VIDEO,
224 3
        ];
225
    }
226
227
    /**
228
     * Get id
229
     *
230
     * @return integer
231
     */
232 1
    public function getId()
233
    {
234 1
        return $this->id;
235
    }
236
237
    /**
238
     * Get artist.
239
     *
240
     * @return string
241
     */
242 1
    public function getArtist()
243
    {
244 1
        return $this->artist;
245
    }
246
247
    /**
248
     * Set artist.
249
     *
250
     * @param string $artist
251
     *
252
     * @return Media
253
     */
254 1
    public function setArtist($artist)
255
    {
256 1
        if ($artist) {
257 1
            $this->artist = substr(trim($artist), 0, 254);
258 1
        }
259
260 1
        return $this;
261
    }
262
263
    /**
264
     * @return float
265
     */
266 2
    public function getBpm()
267
    {
268 2
        return $this->bpm;
269
    }
270
271
    /**
272
     * @param float $bpm
273
     * @return Media
274
     */
275 2
    public function setBpm($bpm)
276
    {
277 2
        if (filter_var($bpm, FILTER_VALIDATE_INT) || filter_var($bpm, FILTER_VALIDATE_FLOAT)) {
278 2
            if ( $bpm <= 160 && $bpm >= 60) {
279 2
                $this->bpm = abs($bpm);
0 ignored issues
show
Documentation Bug introduced by
It seems like abs($bpm) can also be of type integer. However, the property $bpm is declared as type double. 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...
280 2
            }
281 2
        }
282 2
        return $this;
283
    }
284
285
    /**
286
     * @return string
287
     */
288 1
    public function getExist()
289
    {
290 1
        return $this->exist;
291
    }
292
293
    /**
294
     * @param string $exist
295
     * @return $this
296
     */
297 1
    public function setExist($exist)
298
    {
299 1
        $this->exist = $exist;
0 ignored issues
show
Documentation Bug introduced by
The property $exist was declared of type boolean, but $exist is of type string. 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...
300 1
        return $this;
301
    }
302
303
    /**
304
     * Get downloadlink.
305
     *
306
     * @return string
307
     */
308 2
    public function getProviderUrl()
309
    {
310 2
        return $this->providerUrl;
311
    }
312
313
    /**
314
     * Set downloadlink.
315
     *
316
     * @param string $providerUrl
317
     *
318
     * @return Media
319
     */
320 2
    public function setProviderUrl($providerUrl)
321
    {
322 2
        if (filter_var($providerUrl, FILTER_VALIDATE_URL)) {
323 1
            $this->providerUrl = $providerUrl;
324 1
        }
325 2
        return $this;
326
    }
327
328
    /**
329
     * Get fullPath.
330
     *
331
     * @return string
332
     */
333 1
    public function getFullPath()
334
    {
335 1
        return $this->fullPath;
336
    }
337
338
    /**
339
     * Set fullPath.
340
     *
341
     * @param string $fullPath
342
     *
343
     * @return Media
344
     */
345 1
    public function setFullPath($fullPath)
346
    {
347 1
        $pattern = '#('.DIRECTORY_SEPARATOR.')\1+#';
348 1
        $replacement = DIRECTORY_SEPARATOR;
349 1
        $fullPath = preg_replace($pattern, $replacement, $fullPath);
350
351 1
        $this->fullPath = $fullPath;
352
353 1
        $this->exist = file_exists($fullPath) ? true : true;
354 1
        $splFile = new \SplFileInfo($this->fullPath);
355 1
        $this->setFileName($splFile->getBasename());
356 1
        $this->setFullFilePathMd5(md5($this->fullPath));
357 1
        $paths = explode('/', $splFile->getPath());
358 1
        $this->setDirName(end($paths));
359
360 1
        return $this;
361
    }
362
363
    /**
364
     * @return string
365
     */
366 1
    public function getFullFilePathMd5()
367
    {
368 1
        return $this->fullFilePathMd5;
369
    }
370
371
    /**
372
     * @param string $fullFilePathMd5
373
     * @return Media
374
     */
375 1
    public function setFullFilePathMd5($fullFilePathMd5)
376
    {
377 1
        if (strlen($fullFilePathMd5) == 32) {
378 1
            $this->fullFilePathMd5 = $fullFilePathMd5;
379 1
        }
380 1
        return $this;
381
    }
382
383
384
385
    /**
386
     * Get title.
387
     *
388
     * @return string
389
     */
390 1
    public function getTitle()
391
    {
392 1
        return $this->title;
393
    }
394
395
    /**
396
     * Set title.
397
     *
398
     * @param string $title
399
     *
400
     * @return Media
401
     */
402 1
    public function setTitle($title)
403
    {
404 1
        if ($title) {
405 1
            $this->title = trim($title);
406 1
        }
407
408 1
        return $this;
409
    }
410
411
    /**
412
     * Get type
413
     *
414
     * @return integer
415
     */
416 1
    public function getType()
417
    {
418 1
        return $this->type;
419
    }
420
421
    /**
422
     * Set type
423
     *
424
     * @param integer $type
425
     * @return Media
426
     */
427 2
    public function setType($type)
428
    {
429 2
        if (!in_array($type, self::getTypes())) {
430 1
            throw new \InvalidArgumentException(sprintf('%s is not a valid type. See %s', $type, self::class.'::getTypes()'));
431
        }
432
433 1
        $this->type = $type;
434
435 1
        return $this;
436
    }
437
438
    /**
439
     * @return int
440
     */
441 1
    public function getProviderId()
442
    {
443 1
        return $this->providerId;
444
    }
445
446
    /**
447
     * @param int $providerId
448
     * @return Media
449
     */
450 1
    public function setProviderId($providerId)
451
    {
452 1
        $this->providerId = $providerId;
453 1
        return $this;
454
    }
455
456
    /**
457
     * Get releaseDate.
458
     *
459
     * @return \DateTime
460
     */
461 1
    public function getReleaseDate()
462
    {
463 1
        return $this->releaseDate;
464
    }
465
466
    /**
467
     * Set releaseDate.
468
     *
469
     * @param \DateTime $releaseDate
470
     *
471
     * @return Media
472
     */
473 1
    public function setReleaseDate($releaseDate)
474
    {
475 1
        $this->releaseDate = $releaseDate;
476
477 1
        return $this;
478
    }
479
480
    /**
481
     * @param Genre $genre
482
     * @return $this
483
     */
484 1
    public function addGenre(Genre $genre)
485
    {
486 1
        if (!$this->genres->contains($genre)) {
487 1
            $this->genres->add($genre);
488 1
        }
489 1
        return $this;
490
    }
491
492
    /**
493
     * @param Genre $genre
494
     * @return $this
495
     */
496 1
    public function removeGenre(Genre $genre)
497
    {
498 1
        if ($this->genres->contains($genre)) {
499 1
            $this->genres->removeElement($genre);
500 1
        }
501 1
        return $this;
502
    }
503
504
    /**
505
     * @return ArrayCollection
506
     */
507 1
    public function getGenres()
508
    {
509 1
        return $this->genres;
510
    }
511
512
    /**
513
     * Set Genres.
514
     * @param ArrayCollection $genres
515
     * @return $this
516
     */
517 1
    public function setGenres(ArrayCollection $genres)
518
    {
519 1
        $this->genres = $genres;
520
521 1
        return $this;
522
    }
523
    /**
524
     * @param Artist $artist
525
     * @return $this
526
     */
527 1
    public function addArtist(Artist $artist)
528
    {
529 1
        if (!$this->artists->contains($artist)) {
530 1
            $this->artists->add($artist);
531 1
        }
532 1
        return $this;
533
    }
534
535
    /**
536
     * @param Genre $artist
537
     * @return $this
538
     */
539 1
    public function removeArtist(Artist $artist)
540
    {
541 1
        if ($this->artists->contains($artist)) {
542 1
            $this->artists->removeElement($artist);
543 1
        }
544 1
        return $this;
545
    }
546
547
    /**
548
     * @return ArrayCollection
549
     */
550 1
    public function getArtists()
551
    {
552 1
        return $this->artists;
553
    }
554
555
    /**
556
     * @param ArrayCollection $artists
557
     * @return $this
558
     */
559 1
    public function setArtists(ArrayCollection $artists)
560
    {
561 1
        $this->artists = $artists;
562
563 1
        return $this;
564
    }
565
566
    /**
567
     * Get version.
568
     *
569
     * @return string
570
     */
571 1
    public function getVersion()
572
    {
573 1
        return $this->version;
574
    }
575
576
    /**
577
     * Set version.
578
     *
579
     * @param string $version
580
     *
581
     * @return Media
582
     */
583 1
    public function setVersion($version)
584
    {
585 1
        $this->version = trim($version);
586
587 1
        return $this;
588
    }
589
590
    /**
591
     * @return string
592
     */
593 1
    public function getFileName()
594
    {
595 1
        return $this->fileName;
596
    }
597
598
    /**
599
     * @param string $fileName
600
     * @return $this
601
     */
602 1
    public function setFileName($fileName)
603
    {
604 1
        $this->fileName = $fileName;
605 1
        $this->updateProviderId();
606 1
        return $this;
607
    }
608
609
    /**
610
     * @return string
611
     */
612 1
    public function getScore()
613
    {
614 1
        return $this->score;
615
    }
616
617
    /**
618
     * @param string $score
619
     * @return Media
620
     */
621 1
    public function setScore($score)
622
    {
623 1
        if (filter_var($score, FILTER_VALIDATE_INT) || filter_var($score, FILTER_VALIDATE_FLOAT)) {
624 1
            $this->score = $score;
625 1
        }
626 1
        return $this;
627
    }
628
629
    /**
630
     * @return array
631
     * @Groups({"media-read"})
632
     */
633 1
    public function getProviderCode()
634
    {
635 1
        $key = array_search($this->getProvider(), $this->getProviders());
636 1
        return $key;
637
    }
638
639
    /**
640
     * Get provider
641
     *
642
     * @return integer
643
     */
644 1
    public function getProvider()
645
    {
646 1
        return $this->provider;
647
    }
648
649
    /**
650
     * Set provider
651
     *
652
     * @param integer $provider
653
     * @return Media
654
     */
655 2
    public function setProvider($provider)
656
    {
657 2
        if (!in_array($provider, self::getProviders())) {
658 1
            throw new \InvalidArgumentException(sprintf('%s is not a valid provider. See %s::%s', $provider, self::class, 'getProviders()'));
659
        }
660
661 1
        $this->provider = $provider;
662 1
        $this->updateProviderId();
663 1
        return $this;
664
    }
665
666 3
    public static function getProviders()
667
    {
668 3
        return self::$providers;
669
    }
670
671
    /**
672
     * @return \DateTime
673
     */
674 1
    public function getDeletedAt()
675
    {
676 1
        return $this->deletedAt;
677
    }
678
679
    /**
680
     * @param \DateTime $deletedAt
681
     * @return Media
682
     */
683 1
    public function setDeletedAt($deletedAt)
684
    {
685 1
        $this->deletedAt = $deletedAt;
686 1
        return $this;
687
    }
688
689
    /**
690
     * @return string
691
     */
692 1
    public function getDirName()
693
    {
694 1
        return $this->dirName;
695
    }
696
697
    /**
698
     * @param string $dirName
699
     * @return Media
700
     */
701 1
    public function setDirName($dirName)
702
    {
703 1
        $this->dirName = $dirName;
704 1
        return $this;
705
    }
706
    public function isUntaged() {
707
    // @codeCoverageIgnoreStart
708
        return !$this->tagged;
709
    // @codeCoverageIgnoreEnd
710
    }
711
712
    /**
713
     * @return int
714
     */
715 1
    public function getYear()
716
    {
717 1
        return $this->year;
718
    }
719
720
    /**
721
     * @param int $year
722
     * @return Media
723
     */
724 2
    public function setYear($year)
725
    {
726 2
        if (preg_match('/\d{4}/', $year)) {
727 1
            $this->year = $year;
728 1
        }
729 2
        return $this;
730
    }
731
732
733
    /**
734
     * @todo Refactor. Implementation specific
735
     * @return $this
736
     */
737
    public function updateProviderId()
738
    {
739
        // @codeCoverageIgnoreStart
740
        $fileName   = $this->getFileName();
741
        $provider   = $this->getProvider();
742
        $patern     = '/^(?P<providerId>\d{1,9})\_/';
743
744
        if ($fileName && $provider && in_array($provider, $this->getProviders())) {
745
            if ($provider === Media::PROVIDER_SMASHVISION) {
746
                $patern = '/^(?P<providerId>\d{1,9}\_\d{1,9})\_/';
747
            }
748
749
            if (preg_match($patern, $fileName, $matches)){
750
                $this->setProviderId($matches['providerId']);
751
            }
752
        }
753
754
        return $this;
755
        // @codeCoverageIgnoreEnd
756
    }
757
758
    /**
759
     * @return string
760
     */
761
    public function getTagged()
762
    {
763
        // @codeCoverageIgnoreStart
764
        return $this->tagged;
765
        // @codeCoverageIgnoreEnd
766
    }
767
768
    /**
769
     * @param bool $tagged
770
     * @return Media
771
     */
772
    public function setTagged($tagged)
773
    {
774
        // @codeCoverageIgnoreStart
775
        $this->tagged = $tagged;
776
        return $this;
777
        // @codeCoverageIgnoreEnd
778
    }
779
780
}
781