Completed
Push — master ( 26fb58...617f4f )
by Peter
03:53
created

Item::removeSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * AnimeDb package
4
 *
5
 * @package   AnimeDb
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\CatalogBundle\Entity;
12
13
use Doctrine\ORM\Mapping as ORM;
14
use Symfony\Component\Validator\Constraints as Assert;
15
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
16
use Doctrine\Common\Collections\ArrayCollection;
17
use AnimeDb\Bundle\CatalogBundle\Entity\Genre;
18
use AnimeDb\Bundle\CatalogBundle\Entity\Country;
19
use AnimeDb\Bundle\CatalogBundle\Entity\Storage;
20
use AnimeDb\Bundle\CatalogBundle\Entity\Type;
21
use AnimeDb\Bundle\CatalogBundle\Entity\Label;
22
use Symfony\Component\Validator\ExecutionContextInterface;
23
use AnimeDb\Bundle\CatalogBundle\Entity\Studio;
24
use Doctrine\Bundle\DoctrineBundle\Registry;
25
use AnimeDb\Bundle\AppBundle\Service\Downloader\Entity\BaseEntity;
26
use AnimeDb\Bundle\AppBundle\Service\Downloader\Entity\ImageInterface;
27
28
/**
29
 * Item
30
 *
31
 * @ORM\Entity
32
 * @ORM\Table(name="item")
33
 * @ORM\HasLifecycleCallbacks
34
 * @ORM\Entity(repositoryClass="AnimeDb\Bundle\CatalogBundle\Repository\Item")
35
 * @Assert\Callback(methods={"isPathValid"})
36
 * @IgnoreAnnotation("ORM")
37
 *
38
 * @package AnimeDb\Bundle\CatalogBundle\Entity
39
 * @author  Peter Gribanov <[email protected]>
40
 */
41
class Item extends BaseEntity implements ImageInterface
42
{
43
    /**
44
     * Id
45
     *
46
     * @ORM\Id
47
     * @ORM\GeneratedValue
48
     * @ORM\Column(type="integer")
49
     *
50
     * @var integer
51
     */
52
    protected $id = 0;
53
54
    /**
55
     * Main name
56
     *
57
     * @ORM\Column(type="string", length=256)
58
     * @Assert\NotBlank()
59
     *
60
     * @var string
61
     */
62
    protected $name = '';
63
64
    /**
65
     * Main name
66
     *
67
     * @ORM\OneToMany(targetEntity="Name", mappedBy="item", cascade={"persist", "remove"}, orphanRemoval=true)
68
     *
69
     * @var \Doctrine\Common\Collections\ArrayCollection
70
     */
71
    protected $names;
72
73
    /**
74
     * Type
75
     *
76
     * @ORM\ManyToOne(targetEntity="Type", inversedBy="items", cascade={"persist"})
77
     * @ORM\JoinColumn(name="type", referencedColumnName="id")
78
     *
79
     * @var \AnimeDb\Bundle\CatalogBundle\Entity\Type
80
     */
81
    protected $type;
82
83
    /**
84
     * Date premiere
85
     *
86
     * @ORM\Column(type="date")
87
     * @Assert\Date()
88
     *
89
     * @var \DateTime
90
     */
91
    protected $date_premiere;
92
93
    /**
94
     * Date end release
95
     *
96
     * @ORM\Column(type="date", nullable=true)
97
     * @Assert\Date()
98
     *
99
     * @var \DateTime|null
100
     */
101
    protected $date_end;
102
103
    /**
104
     * Genre list
105
     *
106
     * @ORM\ManyToMany(targetEntity="Genre", inversedBy="items", cascade={"persist"})
107
     * @ORM\JoinTable(name="items_genres")
108
     *
109
     * @var \Doctrine\Common\Collections\ArrayCollection
110
     */
111
    protected $genres;
112
113
    /**
114
     * Label list
115
     *
116
     * @ORM\ManyToMany(targetEntity="Label", inversedBy="items", cascade={"persist"})
117
     * @ORM\JoinTable(name="items_labels")
118
     *
119
     * @var \Doctrine\Common\Collections\ArrayCollection
120
     */
121
    protected $labels;
122
123
    /**
124
     * Country
125
     *
126
     * @ORM\ManyToOne(targetEntity="Country", inversedBy="items", cascade={"persist"})
127
     * @ORM\JoinColumn(name="country", referencedColumnName="id")
128
     *
129
     * @var \AnimeDb\Bundle\CatalogBundle\Entity\Country
130
     */
131
    protected $country;
132
133
    /**
134
     * Duration
135
     *
136
     * @ORM\Column(type="integer", nullable=true)
137
     * @Assert\Type(type="integer", message="The value {{ value }} is not a valid {{ type }}.")
138
     *
139
     * @var integer
140
     */
141
    protected $duration = 0;
142
143
    /**
144
     * Summary
145
     *
146
     * @ORM\Column(type="text", nullable=true)
147
     *
148
     * @var string
149
     */
150
    protected $summary = '';
151
152
    /**
153
     * Disk path
154
     *
155
     * @ORM\Column(type="string", length=256, nullable=true)
156
     *
157
     * @var string
158
     */
159
    protected $path = '';
160
161
    /**
162
     * Storage
163
     *
164
     * @ORM\ManyToOne(targetEntity="Storage", inversedBy="items", cascade={"persist"})
165
     * @ORM\JoinColumn(name="storage", referencedColumnName="id")
166
     *
167
     * @var \AnimeDb\Bundle\CatalogBundle\Entity\Storage
168
     */
169
    protected $storage;
170
171
    /**
172
     * Episodes list
173
     *
174
     * @ORM\Column(type="text", nullable=true)
175
     *
176
     * @var string
177
     */
178
    protected $episodes = '';
179
180
    /**
181
     * Translate (subtitles and voice)
182
     *
183
     * @ORM\Column(type="string", length=256, nullable=true)
184
     *
185
     * @var string
186
     */
187
    protected $translate = '';
188
189
    /**
190
     * File info
191
     *
192
     * @ORM\Column(type="text", nullable=true)
193
     *
194
     * @var string
195
     */
196
    protected $file_info = '';
197
198
    /**
199
     * Source list
200
     *
201
     * @ORM\OneToMany(targetEntity="Source", mappedBy="item", cascade={"persist", "remove"}, orphanRemoval=true)
202
     *
203
     * @var \Doctrine\Common\Collections\ArrayCollection
204
     */
205
    protected $sources;
206
207
    /**
208
     * Cover
209
     *
210
     * @ORM\Column(type="string", length=256, nullable=true)
211
     *
212
     * @var string
213
     */
214
    protected $cover = '';
215
216
    /**
217
     * Number of episodes
218
     *
219
     * @ORM\Column(type="string", length=5, nullable=true)
220
     * @Assert\Regex(
221
     *     pattern="/^(\d{1,4}\+?)$/",
222
     *     message="The number of episodes should be a number and can contain a '+' to denote the continuation of production"
223
     * )
224
     *
225
     * @var string
226
     */
227
    protected $episodes_number = '';
228
229
    /**
230
     * Date add item
231
     *
232
     * @ORM\Column(type="datetime")
233
     *
234
     * @var \DateTime
235
     */
236
    protected $date_add;
237
238
    /**
239
     * Date last update item
240
     *
241
     * @ORM\Column(type="datetime")
242
     *
243
     * @var \DateTime
244
     */
245
    protected $date_update;
246
247
    /**
248
     * Image list
249
     *
250
     * @ORM\OneToMany(targetEntity="Image", mappedBy="item", cascade={"persist", "remove"}, orphanRemoval=true)
251
     *
252
     * @var \Doctrine\Common\Collections\ArrayCollection
253
     */
254
    protected $images;
255
256
    /**
257
     * Rating
258
     *
259
     * @ORM\Column(type="integer", nullable=true)
260
     * @Assert\Type(type="integer", message="The value {{ value }} is not a valid {{ type }}.")
261
     *
262
     * @var integer
263
     */
264
    protected $rating = 0;
265
266
    /**
267
     * Studio
268
     *
269
     * @ORM\ManyToOne(targetEntity="Studio", inversedBy="items", cascade={"persist"})
270
     * @ORM\JoinColumn(name="studio", referencedColumnName="id")
271
     *
272
     * @var \AnimeDb\Bundle\CatalogBundle\Entity\Studio
273
     */
274
    protected $studio;
275
276
    /**
277
     * Not cleared path
278
     *
279
     * @var string
280
     */
281
    protected $not_cleared_path = '';
282
283
    /**
284
     * Construct
285
     */
286 116
    public function __construct() {
287 116
        $this->genres = new ArrayCollection();
288 116
        $this->labels = new ArrayCollection();
289 116
        $this->names = new ArrayCollection();
290 116
        $this->sources = new ArrayCollection();
291 116
        $this->images = new ArrayCollection();
292 116
        $this->date_add = new \DateTime();
293 116
        $this->date_update = new \DateTime();
294 116
    }
295
296
    /**
297
     * Get id
298
     *
299
     * @return integer
300
     */
301 1
    public function getId()
302
    {
303 1
        return $this->id;
304
    }
305
306
    /**
307
     * Set name
308
     *
309
     * @param string $name
310
     *
311
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
312
     */
313 5
    public function setName($name)
314
    {
315 5
        $this->name = $name;
316 5
        return $this;
317
    }
318
319
    /**
320
     * Get name
321
     *
322
     * @return string
323
     */
324 2
    public function getName()
325
    {
326 2
        return $this->name;
327
    }
328
329
    /**
330
     * Set date premiere
331
     *
332
     * @param \DateTime|null $date_premiere
333
     *
334
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
335
     */
336 1
    public function setDatePremiere(\DateTime $date_premiere = null)
337
    {
338 1
        $this->date_premiere = $date_premiere ? clone $date_premiere : $date_premiere;
339 1
        return $this;
340
    }
341
342
    /**
343
     * Get date premiere
344
     *
345
     * @return \DateTime
346
     */
347 1
    public function getDatePremiere()
348
    {
349 1
        return $this->date_premiere ? clone $this->date_premiere : null;
350
    }
351
352
    /**
353
     * Set date end
354
     *
355
     * @param \DateTime|null $date_end
356
     *
357
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
358
     */
359 1
    public function setDateEnd(\DateTime $date_end = null)
360
    {
361 1
        $this->date_end = $date_end ? clone $date_end : null;
362 1
        return $this;
363
    }
364
365
    /**
366
     * Get date end
367
     *
368
     * @return \DateTime|null
369
     */
370 1
    public function getDateEnd()
371
    {
372 1
        return $this->date_end ? clone $this->date_end : null;
373
    }
374
375
    /**
376
     * Set duration
377
     *
378
     * @param integer $duration
379
     *
380
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
381
     */
382 1
    public function setDuration($duration)
383
    {
384 1
        $this->duration = $duration;
385 1
        return $this;
386
    }
387
388
    /**
389
     * Get duration
390
     *
391
     * @return integer
392
     */
393 1
    public function getDuration()
394
    {
395 1
        return $this->duration;
396
    }
397
398
    /**
399
     * Set summary
400
     *
401
     * @param string $summary
402
     *
403
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
404
     */
405 1
    public function setSummary($summary)
406
    {
407 1
        $this->summary = $summary;
408 1
        return $this;
409
    }
410
411
    /**
412
     * Get summary
413
     *
414
     * @return string
415
     */
416 1
    public function getSummary()
417
    {
418 1
        return $this->summary;
419
    }
420
421
    /**
422
     * Set path
423
     *
424
     * @param string $path
425
     *
426
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
427
     */
428 8
    public function setPath($path)
429
    {
430 8
        if ($path) {
431 4
            $this->not_cleared_path = $path;
432 4
            $this->doClearPath();
433 4
        } else {
434 4
            $this->path = '';
435
        }
436 8
        return $this;
437
    }
438
439
    /**
440
     * Get path
441
     *
442
     * @return string
443
     */
444 6
    public function getPath()
445
    {
446
        // path not cleared
447 6
        if ($this->not_cleared_path) {
448 3
            return $this->not_cleared_path;
449
        }
450
        // use storage path as prefix
451 3
        if ($this->getStorage() instanceof Storage && $this->getStorage()->getPath()) {
452 1
            return $this->getStorage()->getPath().$this->path;
453
        }
454 2
        return $this->path;
455
    }
456
457
    /**
458
     * Get real path
459
     *
460
     * Need for tests
461
     *
462
     * @return string
463
     */
464 4
    public function getRealPath()
465
    {
466 4
        return $this->path;
467
    }
468
469
    /**
470
     * Set episodes
471
     *
472
     * @param string $episodes
473
     *
474
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
475
     */
476 1
    public function setEpisodes($episodes)
477
    {
478 1
        $this->episodes = $episodes;
479 1
        return $this;
480
    }
481
482
    /**
483
     * Get episodes
484
     *
485
     * @return string
486
     */
487 1
    public function getEpisodes()
488
    {
489 1
        return $this->episodes;
490
    }
491
492
    /**
493
     * Set translate
494
     *
495
     * @param string $translate
496
     *
497
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
498
     */
499 1
    public function setTranslate($translate)
500
    {
501 1
        $this->translate = $translate;
502 1
        return $this;
503
    }
504
505
    /**
506
     * Get translate
507
     *
508
     * @return string
509
     */
510 1
    public function getTranslate()
511
    {
512 1
        return $this->translate;
513
    }
514
515
    /**
516
     * Set file info
517
     *
518
     * @param string $fileInfo
519
     *
520
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
521
     */
522 1
    public function setFileInfo($fileInfo)
523
    {
524 1
        $this->file_info = $fileInfo;
525 1
        return $this;
526
    }
527
528
    /**
529
     * Get file_info
530
     *
531
     * @return string
532
     */
533 1
    public function getFileInfo()
534
    {
535 1
        return $this->file_info;
536
    }
537
538
    /**
539
     * Add name
540
     *
541
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Name $name
542
     *
543
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
544
     */
545 View Code Duplication
    public function addName(Name $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
546
    {
547
        $names = array_map('strval', $this->names->toArray());
548
        if (!in_array($name->getName(), $names)) {
549
            $this->names->add($name);
550
            $name->setItem($this);
551
        }
552
        return $this;
553
    }
554
555
    /**
556
     * Remove name
557
     *
558
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Name $name
559
     *
560
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
561
     */
562
    public function removeName(Name $name)
563
    {
564
        if ($this->names->contains($name)) {
565
            $this->names->removeElement($name);
566
            $name->setItem(null);
567
        }
568
        return $this;
569
    }
570
571
    /**
572
     * Get names
573
     *
574
     * @return \Doctrine\Common\Collections\Collection
575
     */
576
    public function getNames()
577
    {
578
        return $this->names;
579
    }
580
581
    /**
582
     * Set type
583
     *
584
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Type $type
585
     *
586
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
587
     */
588 2 View Code Duplication
    public function setType(Type $type = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
589
    {
590 2
        if ($this->type !== $type) {
591
            // romove link on this item for old type
592 2
            if ($this->type instanceof Type) {
593 1
                $tmp = $this->type;
594 1
                $this->type = null;
595 1
                $tmp->removeItem($this);
596 1
            }
597 2
            $this->type = $type;
598
            // add link on this item
599 2
            if ($this->type instanceof Type) {
600 2
                $this->type->addItem($this);
601 2
            }
602 2
        }
603 2
        return $this;
604
    }
605
606
    /**
607
     * Get type
608
     *
609
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Type
610
     */
611 2
    public function getType()
612
    {
613 2
        return $this->type;
614
    }
615
616
    /**
617
     * Add genre
618
     *
619
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Genre $genre
620
     *
621
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
622
     */
623 2
    public function addGenre(Genre $genre)
624
    {
625 2
        if (!$this->genres->contains($genre)) {
626 2
            $this->genres->add($genre);
627 2
            $genre->addItem($this);
628 2
        }
629 2
        return $this;
630
    }
631
632
    /**
633
     * Remove genre
634
     *
635
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Genre $genre
636
     *
637
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
638
     */
639 1
    public function removeGenre(Genre $genre)
640
    {
641 1
        if ($this->genres->contains($genre)) {
642 1
            $this->genres->removeElement($genre);
643 1
            $genre->removeItem($this);
644 1
        }
645 1
        return $this;
646
    }
647
648
    /**
649
     * Get genres
650
     *
651
     * @return \Doctrine\Common\Collections\Collection
652
     */
653 2
    public function getGenres()
654
    {
655 2
        return $this->genres;
656
    }
657
658
    /**
659
     * Add label
660
     *
661
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Label $label
662
     *
663
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
664
     */
665 1
    public function addLabel(Label $label)
666
    {
667 1
        if (!$this->labels->contains($label)) {
668 1
            $this->labels->add($label);
669 1
            $label->addItem($this);
670 1
        }
671 1
        return $this;
672
    }
673
674
    /**
675
     * Remove label
676
     *
677
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Label $label
678
     *
679
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
680
     */
681 1
    public function removeLabel(Label $label)
682
    {
683 1
        if ($this->labels->contains($label)) {
684 1
            $this->labels->removeElement($label);
685 1
            $label->removeItem($this);
686 1
        }
687 1
        return $this;
688
    }
689
690
    /**
691
     * Get labels
692
     *
693
     * @return \Doctrine\Common\Collections\Collection
694
     */
695 1
    public function getLabels()
696
    {
697 1
        return $this->labels;
698
    }
699
700
    /**
701
     * Set country
702
     *
703
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Country $country
704
     *
705
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
706
     */
707 2 View Code Duplication
    public function setCountry(Country $country = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
708
    {
709 2
        if ($this->country !== $country) {
710
            // romove link on this item for old country
711 2
            if ($this->country instanceof Country) {
712 1
                $tmp = $this->country;
713 1
                $this->country = null;
714 1
                $tmp->removeItem($this);
715 1
            }
716 2
            $this->country = $country;
717
            // add link on this item
718 2
            if ($this->country instanceof Country) {
719 2
                $this->country->addItem($this);
720 2
            }
721 2
        }
722 2
        return $this;
723
    }
724
725
    /**
726
     * Get country
727
     *
728
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Country
729
     */
730 2
    public function getCountry()
731
    {
732 2
        return $this->country;
733
    }
734
735
    /**
736
     * Set storage
737
     *
738
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Storage $storage
739
     *
740
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
741
     */
742 7 View Code Duplication
    public function setStorage(Storage $storage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
743
    {
744 7
        if ($this->storage !== $storage) {
745
            // romove link on this item for old storage
746 7
            if ($this->storage instanceof Storage) {
747 1
                $tmp = $this->storage;
748 1
                $this->storage = null;
749 1
                $tmp->removeItem($this);
750 1
            }
751 7
            $this->storage = $storage;
752
            // add link on this item
753 7
            if ($this->storage instanceof Storage) {
754 7
                $this->storage->addItem($this);
755 7
            }
756 7
        }
757 7
        $this->doClearPath();
758 7
        return $this;
759
    }
760
761
    /**
762
     * Get storage
763
     *
764
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Storage
765
     */
766 10
    public function getStorage()
767
    {
768 10
        return $this->storage;
769
    }
770
771
    /**
772
     * Set cover
773
     *
774
     * @param string $cover
775
     *
776
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
777
     */
778 1
    public function setCover($cover)
779
    {
780 1
        $this->setFilename($cover);
781 1
        return $this;
782
    }
783
784
    /**
785
     * Get cover
786
     *
787
     * @return string
788
     */
789 1
    public function getCover()
790
    {
791 1
        return $this->getFilename();
792
    }
793
794
    /**
795
     * (non-PHPdoc)
796
     * @see \AnimeDb\Bundle\AppBundle\Service\Downloader\Entity\BaseEntity::getFilename()
797
     */
798 2
    public function getFilename()
799
    {
800 2
        return $this->cover ?: parent::getFilename();
801
    }
802
803
    /**
804
     * (non-PHPdoc)
805
     * @see \AnimeDb\Bundle\AppBundle\Service\Downloader\Entity\BaseEntity::setFilename()
806
     */
807 2
    public function setFilename($filename)
808
    {
809 2
        $this->cover = $filename;
810 2
        parent::setFilename($filename);
811 2
        return $this;
812
    }
813
814
    /**
815
     * Add source
816
     *
817
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Source $source
818
     *
819
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
820
     */
821 View Code Duplication
    public function addSource(Source $source)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
822
    {
823
        $sources = array_map('strval', $this->sources->toArray());
824
        if (!in_array($source->getUrl(), $sources)) {
825
            $this->sources->add($source);
826
            $source->setItem($this);
827
        }
828
        return $this;
829
    }
830
831
    /**
832
     * Remove source
833
     *
834
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Source $source
835
     *
836
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
837
     */
838
    public function removeSource(Source $source)
839
    {
840
        if ($this->sources->contains($source)) {
841
            $this->sources->removeElement($source);
842
            $source->setItem(null);
843
        }
844
        return $this;
845
    }
846
847
    /**
848
     * Get sources
849
     *
850
     * @return \Doctrine\Common\Collections\Collection 
851
     */
852
    public function getSources()
853
    {
854
        return $this->sources;
855
    }
856
857
    /**
858
     * Add image
859
     *
860
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Image $image
861
     *
862
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
863
     */
864 View Code Duplication
    public function addImage(Image $image)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
865
    {
866
        $images = array_map('strval', $this->images->toArray());
867
        if (!in_array($image->getSource(), $images)) {
868
            $this->images->add($image);
869
            $image->setItem($this);
870
        }
871
        return $this;
872
    }
873
874
    /**
875
     * Remove image
876
     *
877
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Image $image
878
     *
879
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
880
     */
881
    public function removeImage(Image $image)
882
    {
883
        if ($this->images->contains($image)) {
884
            $this->images->removeElement($image);
885
            $image->setItem(null);
886
        }
887
        return $this;
888
    }
889
890
    /**
891
     * Get images
892
     *
893
     * @return \Doctrine\Common\Collections\Collection 
894
     */
895
    public function getImages()
896
    {
897
        return $this->images;
898
    }
899
900
    /**
901
     * Set number of episodes
902
     *
903
     * @param string $episodes_number
904
     *
905
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
906
     */
907 1
    public function setEpisodesNumber($episodes_number)
908
    {
909 1
        $this->episodes_number = $episodes_number;
910 1
        return $this;
911
    }
912
913
    /**
914
     * Get number of episodes
915
     *
916
     * @return string 
917
     */
918 1
    public function getEpisodesNumber()
919
    {
920 1
        return $this->episodes_number;
921
    }
922
923
    /**
924
     * Set date add item
925
     *
926
     * @param \DateTime $date_add
927
     *
928
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
929
     */
930 1
    public function setDateAdd(\DateTime $date_add)
931
    {
932 1
        $this->date_add = clone $date_add;
933 1
        return $this;
934
    }
935
936
    /**
937
     * Get date add item
938
     *
939
     * @return \DateTime
940
     */
941 1
    public function getDateAdd()
942
    {
943 1
        return clone $this->date_add;
944
    }
945
946
    /**
947
     * Set date last update item
948
     *
949
     * @param \DateTime $date_update
950
     *
951
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
952
     */
953 2
    public function setDateUpdate(\DateTime $date_update)
954
    {
955 2
        $this->date_update = clone $date_update;
956 2
        return $this;
957
    }
958
959
    /**
960
     * Get date last update item
961
     *
962
     * @return \DateTime
963
     */
964 2
    public function getDateUpdate()
965
    {
966 2
        return clone $this->date_update;
967
    }
968
969
    /**
970
     * Set rating
971
     *
972
     * @param integer $rating
973
     *
974
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
975
     */
976 1
    public function setRating($rating)
977
    {
978 1
        $this->rating = $rating;
979 1
        return $this;
980
    }
981
982
    /**
983
     * Get rating
984
     *
985
     * @return integer
986
     */
987 1
    public function getRating()
988
    {
989 1
        return $this->rating;
990
    }
991
992
    /**
993
     * Set studio
994
     *
995
     * @param \AnimeDb\Bundle\CatalogBundle\Entity\Studio $studio
996
     *
997
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
998
     */
999 1 View Code Duplication
    public function setStudio(Studio $studio = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1000
    {
1001 1
        if ($this->studio !== $studio) {
1002
            // romove link on this item for old studio
1003 1
            if ($this->studio instanceof Studio) {
1004 1
                $tmp = $this->studio;
1005 1
                $this->studio = null;
1006 1
                $tmp->removeItem($this);
1007 1
            }
1008 1
            $this->studio = $studio;
1009
            // add link on this item
1010 1
            if ($this->studio instanceof Studio) {
1011 1
                $this->studio->addItem($this);
1012 1
            }
1013 1
        }
1014 1
        return $this;
1015
    }
1016
1017
    /**
1018
     * Get studio
1019
     *
1020
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Studio
1021
     */
1022 1
    public function getStudio()
1023
    {
1024 1
        return $this->studio;
1025
    }
1026
1027
    /**
1028
     * Change item date update
1029
     *
1030
     * @ORM\PreUpdate
1031
     */
1032 1
    public function doChangeDateUpdate()
1033
    {
1034 1
        $this->date_update = new \DateTime();
1035 1
    }
1036
1037
    /**
1038
     * Is valid path for current type
1039
     *
1040
     * @param \Symfony\Component\Validator\ExecutionContextInterface $context
1041
     */
1042 4
    public function isPathValid(ExecutionContextInterface $context)
1043
    {
1044 4
        if ($this->getStorage() instanceof Storage && $this->getStorage()->isPathRequired() && !$this->getPath()) {
1045 1
            $context->addViolationAt('path', 'Path is required to fill for current type of storage');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Valida...rface::addViolationAt() has been deprecated with message: Deprecated since version 2.5, to be removed in Symfony 3.0. Use {@link Context\ExecutionContextInterface::buildViolation()} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1046 1
        }
1047 4
    }
1048
1049
    /**
1050
     * Freeze item
1051
     *
1052
     * @param \Doctrine\Bundle\DoctrineBundle\Registry $doctrine
1053
     *
1054
     * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
1055
     */
1056 1
    public function freez(Registry $doctrine)
1057
    {
1058 1
        $em = $doctrine->getManager();
1059
        // create reference to existing entity
1060 1
        if ($this->country) {
1061 1
            $this->country = $em->getReference(get_class($this->country), $this->country->getId());
1062 1
        }
1063 1
        if ($this->storage) {
1064 1
            $this->storage = $em->getReference(get_class($this->storage), $this->storage->getId());
1065 1
        }
1066 1
        $this->type = $em->getReference(get_class($this->type), $this->type->getId());
1067 1
        foreach ($this->genres as $key => $genre) {
1068 1
            $this->genres[$key] = $em->getReference(get_class($genre), $genre->getId());
1069 1
        }
1070 1
        return $this;
1071
    }
1072
1073
    /**
1074
     * Remove storage path in item path
1075
     *
1076
     * @ORM\PrePersist
1077
     * @ORM\PreUpdate
1078
     */
1079 8
    public function doClearPath()
1080
    {
1081
        if (
1082 8
            $this->not_cleared_path &&
1083 8
            $this->getStorage() instanceof Storage &&
1084 8
            $this->getStorage()->getPath() &&
1085 2
            strpos($this->not_cleared_path, $this->getStorage()->getPath()) === 0
1086 8
        ) {
1087 1
            $this->path = substr($this->not_cleared_path, strlen($this->getStorage()->getPath()));
1088 1
            $this->not_cleared_path = '';
1089 1
        }
1090 8
    }
1091
1092
    /**
1093
     * Get item name for url
1094
     *
1095
     * @return string
1096
     */
1097 3
    public function getUrlName()
1098
    {
1099 3
        return trim(preg_replace('/\s+/', '_', $this->name), '_');
1100
    }
1101
1102
    /**
1103
     * To string
1104
     *
1105
     * @return string
1106
     */
1107 1
    public function __toString()
1108
    {
1109 1
        return $this->getName();
1110
    }
1111
}
1112