PageImageTrait::issetImage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
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(
13
     *     targetEntity="PiedWeb\CMSBundle\Entity\MediaInterface",
14
     *     cascade={"all"},
15
     *     inversedBy="mainImagePages"
16
     * )
17
     */
18
    protected $mainImage;
19
20
    /**
21
     * @var ArrayCollection
22
     */
23
    protected $images;
24
25
    /**
26
     * @ORM\OneToMany(
27
     *     targetEntity="PiedWeb\CMSBundle\Entity\PageHasMediaInterface",
28
     *     mappedBy="page",
29
     *     cascade={"all"},
30
     *     orphanRemoval=true
31
     * )
32
     * @ORM\OrderBy({"position": "ASC"})
33
     */
34
    protected $pageHasMedias;
35
36 3
    public function __constructImage()
37
    {
38 3
        $this->pageHasMedias = new ArrayCollection();
39 3
    }
40
41
    public function setPageHasMedias($pageHasMedias)
42
    {
43
        $this->pageHasMedias = new ArrayCollection();
44
        foreach ($pageHasMedias as $pageHasMedia) {
45
            $this->addPageHasMedia($pageHasMedia);
46
        }
47
    }
48
49
    public function getPageHasMedias()
50
    {
51
        return $this->pageHasMedias;
52
    }
53
54
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
55
    {
56
        $pageHasMedia->setPage($this);
57
        $this->pageHasMedias[] = $pageHasMedia;
58
    }
59
60
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
61
    {
62
        $this->pageHasMedias->removeElement($pageHasMedia);
63
    }
64
65
    public function getMainImage(): ?MediaInterface
66
    {
67
        return $this->mainImage;
68
    }
69
70
    public function setMainImage(?MediaInterface $mainImage): self
71
    {
72
        // TODO: Déplacer en Assert pour éviter une erreur dégueu ?!
73
        if (null !== $mainImage && null === $mainImage->getWidth()) {
74
            throw new \Exception('mainImage must be an Image. Media imported is not an image');
75
        }
76
77
        $this->mainImage = $mainImage;
78
79
        return $this;
80
    }
81
82
    public function getImages(): Collection
83
    {
84
        if (! $this->images) {
85
            $this->images = new ArrayCollection();
86
            foreach ($this->pageHasMedias as $p) {
87
                if (null !== $p->getMedia()) {
88
                    $this->images[] = $p->getMedia();
89
                }
90
            }
91
        }
92
93
        return $this->images;
94
    }
95
96
    public function issetImage()
97
    {
98
        if ($this->getImages()->count() > 0) {
99
            return true;
100
        }
101
102
        return false;
103
    }
104
}
105