Completed
Push — 0.4.26 ( 70c06b...d0497d )
by Peter
18:23
created

Item   F

Complexity

Total Complexity 103

Size/Duplication

Total Lines 914
Duplicated Lines 10.5 %

Coupling/Cohesion

Components 12
Dependencies 12

Test Coverage

Coverage 83.54%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 103
c 1
b 0
f 0
lcom 12
cbo 12
dl 96
loc 914
ccs 198
cts 237
cp 0.8354
rs 1.0434

62 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setName() 0 5 1
A setDatePremiere() 0 5 2
A getDatePremiere() 0 4 2
A setDuration() 0 5 1
A getDuration() 0 4 1
A setSummary() 0 5 1
A getPath() 0 12 4
A getEpisodes() 0 4 1
A setTranslate() 0 5 1
A getTranslate() 0 4 1
A getFileInfo() 0 4 1
A getLabels() 0 4 1
A getCover() 0 4 1
A getEpisodesNumber() 0 4 1
A setDateAdd() 0 5 1
A getDateAdd() 0 4 1
A setDateUpdate() 0 5 1
A getDateUpdate() 0 4 1
A getRating() 0 4 1
A getUrlName() 0 4 1
A __toString() 0 4 1
A getId() 0 4 1
A getName() 0 4 1
A setDateEnd() 0 5 2
A getDateEnd() 0 4 2
A getSummary() 0 4 1
A getRealPath() 0 4 1
A setEpisodes() 0 5 1
A setFileInfo() 0 5 1
A getNames() 0 4 1
A getType() 0 4 1
A getGenres() 0 4 1
A getCountry() 0 4 1
A getStorage() 0 4 1
A setCover() 0 5 1
A getFilename() 0 4 2
A setFilename() 0 6 1
A getSources() 0 4 1
A getImages() 0 4 1
A setEpisodesNumber() 0 5 1
A setRating() 0 5 1
A getStudio() 0 4 1
A doChangeDateUpdate() 0 4 1
A setPath() 0 10 2
A addName() 9 9 2
A removeName() 0 8 2
A setType() 17 17 4
A addGenre() 0 8 2
A removeGenre() 0 8 2
A addLabel() 0 8 2
A removeLabel() 0 8 2
A setCountry() 17 17 4
A setStorage() 18 18 4
A addSource() 9 9 2
A removeSource() 0 8 2
A addImage() 9 9 2
A removeImage() 0 8 2
A setStudio() 17 17 4
A isPathValid() 0 6 4
A freez() 0 16 4
B doClearPath() 0 12 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Item often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Item, and based on these observations, apply Extract Interface, too.

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\Collections\ArrayCollection;
16
use Symfony\Component\Validator\ExecutionContextInterface;
17
use Doctrine\Bundle\DoctrineBundle\Registry;
18
use AnimeDb\Bundle\AppBundle\Service\Downloader\Entity\BaseEntity;
19
use AnimeDb\Bundle\AppBundle\Service\Downloader\Entity\ImageInterface;
20
21
/**
22
 * Item
23
 *
24
 * @ORM\Entity
25
 * @ORM\Table(name="item")
26
 * @ORM\HasLifecycleCallbacks
27
 * @ORM\Entity(repositoryClass="AnimeDb\Bundle\CatalogBundle\Repository\Item")
28
 * @Assert\Callback(methods={"isPathValid"})
29
 *
30
 * @package AnimeDb\Bundle\CatalogBundle\Entity
31
 * @author  Peter Gribanov <[email protected]>
32
 */
33
class Item extends BaseEntity implements ImageInterface
34
{
35
    /**
36
     * @ORM\Id
37
     * @ORM\GeneratedValue
38
     * @ORM\Column(type="integer")
39
     *
40
     * @var int
41
     */
42
    protected $id = 0;
43
44
    /**
45
     * @ORM\Column(type="string", length=256)
46
     * @Assert\NotBlank()
47
     *
48
     * @var string
49
     */
50
    protected $name = '';
51
52
    /**
53
     * @ORM\OneToMany(targetEntity="Name", mappedBy="item", cascade={"persist", "remove"}, orphanRemoval=true)
54
     *
55
     * @var ArrayCollection
56
     */
57
    protected $names;
58
59
    /**
60
     * @ORM\ManyToOne(targetEntity="Type", inversedBy="items", cascade={"persist"})
61
     * @ORM\JoinColumn(name="type", referencedColumnName="id")
62
     *
63
     * @var Type
64
     */
65
    protected $type;
66
67
    /**
68
     * @ORM\Column(type="date")
69
     * @Assert\Date()
70
     *
71
     * @var \DateTime
72
     */
73
    protected $date_premiere;
74
75
    /**
76
     * @ORM\Column(type="date", nullable=true)
77
     * @Assert\Date()
78
     *
79
     * @var \DateTime|null
80
     */
81
    protected $date_end;
82
83
    /**
84
     * @ORM\ManyToMany(targetEntity="Genre", inversedBy="items", cascade={"persist"})
85
     * @ORM\JoinTable(name="items_genres")
86
     *
87
     * @var ArrayCollection
88
     */
89
    protected $genres;
90
91
    /**
92
     * @ORM\ManyToMany(targetEntity="Label", inversedBy="items", cascade={"persist"})
93
     * @ORM\JoinTable(name="items_labels")
94
     *
95
     * @var ArrayCollection
96
     */
97
    protected $labels;
98
99
    /**
100
     * @ORM\ManyToOne(targetEntity="Country", inversedBy="items", cascade={"persist"})
101
     * @ORM\JoinColumn(name="country", referencedColumnName="id")
102
     *
103
     * @var Country
104
     */
105
    protected $country;
106
107
    /**
108
     * @ORM\Column(type="integer", nullable=true)
109
     * @Assert\Type(type="integer", message="The value {{ value }} is not a valid {{ type }}.")
110
     *
111
     * @var int
112
     */
113
    protected $duration = 0;
114
115
    /**
116
     * @ORM\Column(type="text", nullable=true)
117
     *
118
     * @var string
119
     */
120
    protected $summary = '';
121
122
    /**
123
     * @ORM\Column(type="string", length=256, nullable=true)
124
     *
125
     * @var string
126
     */
127
    protected $path = '';
128
129
    /**
130
     * @ORM\ManyToOne(targetEntity="Storage", inversedBy="items", cascade={"persist"})
131
     * @ORM\JoinColumn(name="storage", referencedColumnName="id")
132
     *
133
     * @var Storage
134
     */
135
    protected $storage;
136
137
    /**
138
     * @ORM\Column(type="text", nullable=true)
139
     *
140
     * @var string
141
     */
142
    protected $episodes = '';
143
144
    /**
145
     * Translate (subtitles and voice)
146
     *
147
     * @ORM\Column(type="string", length=256, nullable=true)
148
     *
149
     * @var string
150
     */
151
    protected $translate = '';
152
153
    /**
154
     * @ORM\Column(type="text", nullable=true)
155
     *
156
     * @var string
157
     */
158
    protected $file_info = '';
159
160
    /**
161
     * @ORM\OneToMany(targetEntity="Source", mappedBy="item", cascade={"persist", "remove"}, orphanRemoval=true)
162
     *
163
     * @var ArrayCollection
164
     */
165
    protected $sources;
166
167
    /**
168
     * @ORM\Column(type="string", length=256, nullable=true)
169
     *
170
     * @var string
171
     */
172
    protected $cover = '';
173
174
    /**
175
     * Number of episodes
176
     *
177
     * @ORM\Column(type="string", length=5, nullable=true)
178
     * @Assert\Regex(
179
     *     pattern="/^(\d{1,4}\+?)$/",
180
     *     message="The number of episodes should be a number and can contain a '+' to denote the continuation of production"
181
     * )
182
     *
183
     * @var string
184
     */
185
    protected $episodes_number = '';
186
187
    /**
188
     * @ORM\Column(type="datetime")
189
     *
190
     * @var \DateTime
191
     */
192
    protected $date_add;
193
194
    /**
195
     * @ORM\Column(type="datetime")
196
     *
197
     * @var \DateTime
198
     */
199
    protected $date_update;
200
201
    /**
202
     * @ORM\OneToMany(targetEntity="Image", mappedBy="item", cascade={"persist", "remove"}, orphanRemoval=true)
203
     *
204
     * @var ArrayCollection
205
     */
206
    protected $images;
207
208
    /**
209
     * @ORM\Column(type="integer", nullable=true)
210
     * @Assert\Type(type="integer", message="The value {{ value }} is not a valid {{ type }}.")
211
     *
212
     * @var int
213
     */
214
    protected $rating = 0;
215
216
    /**
217
     * @ORM\ManyToOne(targetEntity="Studio", inversedBy="items", cascade={"persist"})
218
     * @ORM\JoinColumn(name="studio", referencedColumnName="id")
219
     *
220
     * @var Studio
221
     */
222
    protected $studio;
223
224
    /**
225
     * @var string
226
     */
227
    protected $not_cleared_path = '';
228
229 116
    public function __construct() {
230 116
        $this->genres = new ArrayCollection();
231 116
        $this->labels = new ArrayCollection();
232 116
        $this->names = new ArrayCollection();
233 116
        $this->sources = new ArrayCollection();
234 116
        $this->images = new ArrayCollection();
235 116
        $this->date_add = new \DateTime();
236 116
        $this->date_update = new \DateTime();
237 116
    }
238
239
    /**
240
     * @return int
241
     */
242 1
    public function getId()
243
    {
244 1
        return $this->id;
245
    }
246
247
    /**
248
     * @param string $name
249
     *
250
     * @return Item
251
     */
252 5
    public function setName($name)
253
    {
254 5
        $this->name = $name;
255 5
        return $this;
256
    }
257
258
    /**
259
     * @return string
260
     */
261 2
    public function getName()
262
    {
263 2
        return $this->name;
264
    }
265
266
    /**
267
     * @param \DateTime|null $date_premiere
268
     *
269
     * @return Item
270
     */
271 1
    public function setDatePremiere(\DateTime $date_premiere = null)
272
    {
273 1
        $this->date_premiere = $date_premiere ? clone $date_premiere : $date_premiere;
274 1
        return $this;
275
    }
276
277
    /**
278
     * @return \DateTime
279
     */
280 1
    public function getDatePremiere()
281
    {
282 1
        return $this->date_premiere ? clone $this->date_premiere : null;
283
    }
284
285
    /**
286
     * @param \DateTime|null $date_end
287
     *
288
     * @return Item
289
     */
290 1
    public function setDateEnd(\DateTime $date_end = null)
291
    {
292 1
        $this->date_end = $date_end ? clone $date_end : null;
293 1
        return $this;
294
    }
295
296
    /**
297
     * @return \DateTime|null
298
     */
299 1
    public function getDateEnd()
300
    {
301 1
        return $this->date_end ? clone $this->date_end : null;
302
    }
303
304
    /**
305
     * @param int $duration
306
     *
307
     * @return Item
308
     */
309 1
    public function setDuration($duration)
310
    {
311 1
        $this->duration = $duration;
312 1
        return $this;
313
    }
314
315
    /**
316
     * @return int
317
     */
318 1
    public function getDuration()
319
    {
320 1
        return $this->duration;
321
    }
322
323
    /**
324
     * @param string $summary
325
     *
326
     * @return Item
327
     */
328 1
    public function setSummary($summary)
329
    {
330 1
        $this->summary = $summary;
331 1
        return $this;
332
    }
333
334
    /**
335
     * @return string
336
     */
337 1
    public function getSummary()
338
    {
339 1
        return $this->summary;
340
    }
341
342
    /**
343
     * @param string $path
344
     *
345
     * @return Item
346
     */
347 8
    public function setPath($path)
348
    {
349 8
        if ($path) {
350 4
            $this->not_cleared_path = $path;
351 4
            $this->doClearPath();
352
        } else {
353 4
            $this->path = '';
354
        }
355 8
        return $this;
356
    }
357
358
    /**
359
     * @return string
360
     */
361 6
    public function getPath()
362
    {
363
        // path not cleared
364 6
        if ($this->not_cleared_path) {
365 3
            return $this->not_cleared_path;
366
        }
367
        // use storage path as prefix
368 3
        if ($this->getStorage() instanceof Storage && $this->getStorage()->getPath()) {
369 1
            return $this->getStorage()->getPath().$this->path;
370
        }
371 2
        return $this->path;
372
    }
373
374
    /**
375
     * Get real path
376
     *
377
     * Need for tests
378
     *
379
     * @return string
380
     */
381 4
    public function getRealPath()
382
    {
383 4
        return $this->path;
384
    }
385
386
    /**
387
     * @param string $episodes
388
     *
389
     * @return Item
390
     */
391 1
    public function setEpisodes($episodes)
392
    {
393 1
        $this->episodes = $episodes;
394 1
        return $this;
395
    }
396
397
    /**
398
     * @return string
399
     */
400 1
    public function getEpisodes()
401
    {
402 1
        return $this->episodes;
403
    }
404
405
    /**
406
     * @param string $translate
407
     *
408
     * @return Item
409
     */
410 1
    public function setTranslate($translate)
411
    {
412 1
        $this->translate = $translate;
413 1
        return $this;
414
    }
415
416
    /**
417
     * @return string
418
     */
419 1
    public function getTranslate()
420
    {
421 1
        return $this->translate;
422
    }
423
424
    /**
425
     * @param string $fileInfo
426
     *
427
     * @return Item
428
     */
429 1
    public function setFileInfo($fileInfo)
430
    {
431 1
        $this->file_info = $fileInfo;
432 1
        return $this;
433
    }
434
435
    /**
436
     * @return string
437
     */
438 1
    public function getFileInfo()
439
    {
440 1
        return $this->file_info;
441
    }
442
443
    /**
444
     * @param Name $name
445
     *
446
     * @return Item
447
     */
448 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...
449
    {
450
        $names = array_map('strval', $this->names->toArray());
451
        if (!in_array($name->getName(), $names)) {
452
            $this->names->add($name);
453
            $name->setItem($this);
454
        }
455
        return $this;
456
    }
457
458
    /**
459
     * @param Name $name
460
     *
461
     * @return Item
462
     */
463
    public function removeName(Name $name)
464
    {
465
        if ($this->names->contains($name)) {
466
            $this->names->removeElement($name);
467
            $name->setItem(null);
468
        }
469
        return $this;
470
    }
471
472
    /**
473
     * @return ArrayCollection
474
     */
475
    public function getNames()
476
    {
477
        return $this->names;
478
    }
479
480
    /**
481
     * @param Type $type
482
     *
483
     * @return Item
484
     */
485 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...
486
    {
487 2
        if ($this->type !== $type) {
488
            // romove link on this item for old type
489 2
            if ($this->type instanceof Type) {
490 1
                $tmp = $this->type;
491 1
                $this->type = null;
492 1
                $tmp->removeItem($this);
493
            }
494 2
            $this->type = $type;
495
            // add link on this item
496 2
            if ($this->type instanceof Type) {
497 2
                $this->type->addItem($this);
498
            }
499
        }
500 2
        return $this;
501
    }
502
503
    /**
504
     * @return Type
505
     */
506 2
    public function getType()
507
    {
508 2
        return $this->type;
509
    }
510
511
    /**
512
     * @param Genre $genre
513
     *
514
     * @return Item
515
     */
516 2
    public function addGenre(Genre $genre)
517
    {
518 2
        if (!$this->genres->contains($genre)) {
519 2
            $this->genres->add($genre);
520 2
            $genre->addItem($this);
521
        }
522 2
        return $this;
523
    }
524
525
    /**
526
     * @param Genre $genre
527
     *
528
     * @return Item
529
     */
530 1
    public function removeGenre(Genre $genre)
531
    {
532 1
        if ($this->genres->contains($genre)) {
533 1
            $this->genres->removeElement($genre);
534 1
            $genre->removeItem($this);
535
        }
536 1
        return $this;
537
    }
538
539
    /**
540
     * @return ArrayCollection
541
     */
542 2
    public function getGenres()
543
    {
544 2
        return $this->genres;
545
    }
546
547
    /**
548
     * @param Label $label
549
     *
550
     * @return Item
551
     */
552 1
    public function addLabel(Label $label)
553
    {
554 1
        if (!$this->labels->contains($label)) {
555 1
            $this->labels->add($label);
556 1
            $label->addItem($this);
557
        }
558 1
        return $this;
559
    }
560
561
    /**
562
     * @param Label $label
563
     *
564
     * @return Item
565
     */
566 1
    public function removeLabel(Label $label)
567
    {
568 1
        if ($this->labels->contains($label)) {
569 1
            $this->labels->removeElement($label);
570 1
            $label->removeItem($this);
571
        }
572 1
        return $this;
573
    }
574
575
    /**
576
     * @return ArrayCollection
577
     */
578 1
    public function getLabels()
579
    {
580 1
        return $this->labels;
581
    }
582
583
    /**
584
     * @param Country $country
585
     *
586
     * @return Item
587
     */
588 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...
589
    {
590 2
        if ($this->country !== $country) {
591
            // romove link on this item for old country
592 2
            if ($this->country instanceof Country) {
593 1
                $tmp = $this->country;
594 1
                $this->country = null;
595 1
                $tmp->removeItem($this);
596
            }
597 2
            $this->country = $country;
598
            // add link on this item
599 2
            if ($this->country instanceof Country) {
600 2
                $this->country->addItem($this);
601
            }
602
        }
603 2
        return $this;
604
    }
605
606
    /**
607
     * @return Country
608
     */
609 2
    public function getCountry()
610
    {
611 2
        return $this->country;
612
    }
613
614
    /**
615
     * @param Storage $storage
616
     *
617
     * @return Item
618
     */
619 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...
620
    {
621 7
        if ($this->storage !== $storage) {
622
            // romove link on this item for old storage
623 7
            if ($this->storage instanceof Storage) {
624 1
                $tmp = $this->storage;
625 1
                $this->storage = null;
626 1
                $tmp->removeItem($this);
627
            }
628 7
            $this->storage = $storage;
629
            // add link on this item
630 7
            if ($this->storage instanceof Storage) {
631 7
                $this->storage->addItem($this);
632
            }
633
        }
634 7
        $this->doClearPath();
635 7
        return $this;
636
    }
637
638
    /**
639
     * @return Storage
640
     */
641 10
    public function getStorage()
642
    {
643 10
        return $this->storage;
644
    }
645
646
    /**
647
     * @param string $cover
648
     *
649
     * @return Item
650
     */
651 1
    public function setCover($cover)
652
    {
653 1
        $this->setFilename($cover);
654 1
        return $this;
655
    }
656
657
    /**
658
     * @return string
659
     */
660 1
    public function getCover()
661
    {
662 1
        return $this->getFilename();
663
    }
664
665
    /**
666
     * @return string
667
     */
668 2
    public function getFilename()
669
    {
670 2
        return $this->cover ?: parent::getFilename();
671
    }
672
673
    /**
674
     * @param string $filename
675
     *
676
     * @return Item
677
     */
678 2
    public function setFilename($filename)
679
    {
680 2
        $this->cover = $filename;
681 2
        parent::setFilename($filename);
682 2
        return $this;
683
    }
684
685
    /**
686
     * @param Source $source
687
     *
688
     * @return Item
689
     */
690 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...
691
    {
692
        $sources = array_map('strval', $this->sources->toArray());
693
        if (!in_array($source->getUrl(), $sources)) {
694
            $this->sources->add($source);
695
            $source->setItem($this);
696
        }
697
        return $this;
698
    }
699
700
    /**
701
     * @param Source $source
702
     *
703
     * @return Item
704
     */
705
    public function removeSource(Source $source)
706
    {
707
        if ($this->sources->contains($source)) {
708
            $this->sources->removeElement($source);
709
            $source->setItem(null);
710
        }
711
        return $this;
712
    }
713
714
    /**
715
     * @return ArrayCollection
716
     */
717
    public function getSources()
718
    {
719
        return $this->sources;
720
    }
721
722
    /**
723
     * @param Image $image
724
     *
725
     * @return Item
726
     */
727 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...
728
    {
729
        $images = array_map('strval', $this->images->toArray());
730
        if (!in_array($image->getSource(), $images)) {
731
            $this->images->add($image);
732
            $image->setItem($this);
733
        }
734
        return $this;
735
    }
736
737
    /**
738
     * @param Image $image
739
     *
740
     * @return Item
741
     */
742
    public function removeImage(Image $image)
743
    {
744
        if ($this->images->contains($image)) {
745
            $this->images->removeElement($image);
746
            $image->setItem(null);
747
        }
748
        return $this;
749
    }
750
751
    /**
752
     * @return ArrayCollection
753
     */
754
    public function getImages()
755
    {
756
        return $this->images;
757
    }
758
759
    /**
760
     * @param string $episodes_number
761
     *
762
     * @return Item
763
     */
764 1
    public function setEpisodesNumber($episodes_number)
765
    {
766 1
        $this->episodes_number = $episodes_number;
767 1
        return $this;
768
    }
769
770
    /**
771
     * @return string
772
     */
773 1
    public function getEpisodesNumber()
774
    {
775 1
        return $this->episodes_number;
776
    }
777
778
    /**
779
     * @param \DateTime $date_add
780
     *
781
     * @return Item
782
     */
783 1
    public function setDateAdd(\DateTime $date_add)
784
    {
785 1
        $this->date_add = clone $date_add;
786 1
        return $this;
787
    }
788
789
    /**
790
     * @return \DateTime
791
     */
792 1
    public function getDateAdd()
793
    {
794 1
        return clone $this->date_add;
795
    }
796
797
    /**
798
     * @param \DateTime $date_update
799
     *
800
     * @return Item
801
     */
802 2
    public function setDateUpdate(\DateTime $date_update)
803
    {
804 2
        $this->date_update = clone $date_update;
805 2
        return $this;
806
    }
807
808
    /**
809
     * @return \DateTime
810
     */
811 2
    public function getDateUpdate()
812
    {
813 2
        return clone $this->date_update;
814
    }
815
816
    /**
817
     * @param int $rating
818
     *
819
     * @return Item
820
     */
821 1
    public function setRating($rating)
822
    {
823 1
        $this->rating = $rating;
824 1
        return $this;
825
    }
826
827
    /**
828
     * @return int
829
     */
830 1
    public function getRating()
831
    {
832 1
        return $this->rating;
833
    }
834
835
    /**
836
     * @param Studio $studio
837
     *
838
     * @return Item
839
     */
840 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...
841
    {
842 1
        if ($this->studio !== $studio) {
843
            // romove link on this item for old studio
844 1
            if ($this->studio instanceof Studio) {
845 1
                $tmp = $this->studio;
846 1
                $this->studio = null;
847 1
                $tmp->removeItem($this);
848
            }
849 1
            $this->studio = $studio;
850
            // add link on this item
851 1
            if ($this->studio instanceof Studio) {
852 1
                $this->studio->addItem($this);
853
            }
854
        }
855 1
        return $this;
856
    }
857
858
    /**
859
     * @return Studio
860
     */
861 1
    public function getStudio()
862
    {
863 1
        return $this->studio;
864
    }
865
866
    /**
867
     * @ORM\PreUpdate
868
     */
869 1
    public function doChangeDateUpdate()
870
    {
871 1
        $this->date_update = new \DateTime();
872 1
    }
873
874
    /**
875
     * Is valid path for current type
876
     *
877
     * @param ExecutionContextInterface $context
878
     */
879 4
    public function isPathValid(ExecutionContextInterface $context)
880
    {
881 4
        if ($this->getStorage() instanceof Storage && $this->getStorage()->isPathRequired() && !$this->getPath()) {
882 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: since version 2.5, to be removed in 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...
883
        }
884 4
    }
885
886
    /**
887
     * Freeze item
888
     *
889
     * @param Registry $doctrine
890
     *
891
     * @return Item
892
     */
893 1
    public function freez(Registry $doctrine)
894
    {
895 1
        $em = $doctrine->getManager();
896
        // create reference to existing entity
897 1
        if ($this->country) {
898 1
            $this->country = $em->getReference(get_class($this->country), $this->country->getId());
899
        }
900 1
        if ($this->storage) {
901 1
            $this->storage = $em->getReference(get_class($this->storage), $this->storage->getId());
902
        }
903 1
        $this->type = $em->getReference(get_class($this->type), $this->type->getId());
904 1
        foreach ($this->genres as $key => $genre) {
905 1
            $this->genres[$key] = $em->getReference(get_class($genre), $genre->getId());
906
        }
907 1
        return $this;
908
    }
909
910
    /**
911
     * Remove storage path in item path
912
     *
913
     * @ORM\PrePersist
914
     * @ORM\PreUpdate
915
     */
916 8
    public function doClearPath()
917
    {
918
        if (
919 8
            $this->not_cleared_path &&
920 8
            $this->getStorage() instanceof Storage &&
921 8
            $this->getStorage()->getPath() &&
922 8
            strpos($this->not_cleared_path, $this->getStorage()->getPath()) === 0
923
        ) {
924 1
            $this->path = substr($this->not_cleared_path, strlen($this->getStorage()->getPath()));
925 1
            $this->not_cleared_path = '';
926
        }
927 8
    }
928
929
    /**
930
     * Get item name for url
931
     *
932
     * @return string
933
     */
934 3
    public function getUrlName()
935
    {
936 3
        return trim(preg_replace('/\s+/', '_', $this->name), '_');
937
    }
938
939
    /**
940
     * @return string
941
     */
942 1
    public function __toString()
943
    {
944 1
        return $this->getName();
945
    }
946
}
947