Completed
Push — master ( 59b92d...81b834 )
by Dev
04:01
created

PageImageTrait::addPageHasMedia()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
9
trait PageImageTrait
10
{
11
    /**
12
     * @ORM\ManyToOne(targetEntity="PiedWeb\CMSBundle\Entity\MediaInterface")
13
     */
14
    protected $mainImage;
15
16
    /**
17
     * @var ArrayCollection
18
     */
19
    protected $images;
20
21
    /**
22
     * @ORM\OneToMany(
23
     *     targetEntity="PiedWeb\CMSBundle\Entity\PageHasMediaInterface",
24
     *     mappedBy="page",
25
     *     cascade={"all"},
26
     *     orphanRemoval=true
27
     * )
28
     * @ORM\OrderBy({"position":"ASC"})
29
     */
30
    protected $pageHasMedias;
31
32
    public function __constructImage()
33
    {
34
        $this->pageHasMedias = new ArrayCollection();
35
    }
36
37
    public function setPageHasMedias($pageHasMedias)
38
    {
39
        $this->pageHasMedias = new ArrayCollection();
40
        foreach ($pageHasMedias as $pageHasMedia) {
41
            $this->addPageHasMedia($pageHasMedia);
42
        }
43
    }
44
45
    public function getPageHasMedias()
46
    {
47
        return $this->pageHasMedias;
48
    }
49
50
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
51
    {
52
        $pageHasMedia->setPage($this);
1 ignored issue
show
Bug introduced by
$this of type PiedWeb\CMSBundle\Entity\PageImageTrait is incompatible with the type PiedWeb\CMSBundle\Entity\PageInterface|null expected by parameter $page of PiedWeb\CMSBundle\Entity\PageHasMedia::setPage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $pageHasMedia->setPage(/** @scrutinizer ignore-type */ $this);
Loading history...
53
        $this->pageHasMedias[] = $pageHasMedia;
54
    }
55
56
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
57
    {
58
        $this->pageHasMedias->removeElement($pageHasMedia);
59
    }
60
61
    public function getMainImage(): ?MediaInterface
62
    {
63
        return $this->mainImage;
64
    }
65
66
    public function setMainImage(?MediaInterface $mainImage): self
67
    {
68
        // TODO: Déplacer en Assert pour éviter une erreur dégueu ?!
69
        if (null !== $mainImage && null === $mainImage->getWidth()) {
0 ignored issues
show
Bug introduced by
The method getWidth() does not exist on PiedWeb\CMSBundle\Entity\MediaInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to PiedWeb\CMSBundle\Entity\MediaInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        if (null !== $mainImage && null === $mainImage->/** @scrutinizer ignore-call */ getWidth()) {
Loading history...
70
            throw new \Exception('mainImage must be an Image. Media imported is not an image');
71
        }
72
73
        $this->mainImage = $mainImage;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return Collection|Image[]
80
     */
81
    public function getImages(): Collection
82
    {
83
        if (!$this->images) {
84
            $this->images = new ArrayCollection();
85
            foreach ($this->pageHasMedias as $p) {
86
                if (null !== $p->getMedia()) {
87
                    $this->images[] = $p->getMedia();
88
                }
89
            }
90
        }
91
92
        return $this->images;
93
    }
94
95
    public function issetImage()
96
    {
97
        if ($this->getImages()->count() > 0) {
98
            return true;
99
        }
100
101
        return false;
102
    }
103
}
104