Completed
Push — master ( 83a099...10158f )
by Dev
12:50 queued 08:16
created

PageImageTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 8.57%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 97
ccs 3
cts 35
cp 0.0857
rs 10
c 0
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addPageHasMedia() 0 4 1
A setPageHasMedias() 0 5 2
A __constructImage() 0 3 1
A removePageHasMedia() 0 3 1
A issetImage() 0 7 2
A setMainImage() 0 10 3
A getMainImage() 0 3 1
A getImages() 0 12 4
A getPageHasMedias() 0 3 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(
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
    /**
83
     * @return Collection|Image[]
84
     */
85
    public function getImages(): Collection
86
    {
87
        if (!$this->images) {
88
            $this->images = new ArrayCollection();
89
            foreach ($this->pageHasMedias as $p) {
90
                if (null !== $p->getMedia()) {
91
                    $this->images[] = $p->getMedia();
92
                }
93
            }
94
        }
95
96
        return $this->images;
97
    }
98
99
    public function issetImage()
100
    {
101
        if ($this->getImages()->count() > 0) {
102
            return true;
103
        }
104
105
        return false;
106
    }
107
}
108