Passed
Push — master ( 01829a...af608e )
by Dev
03:30
created

PageImageTrait::getImages()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 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
use PiedWeb\CMSBundle\Entity\MediaInterface as Media;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PiedWeb\CMSBundle\Entity\Media. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
10
trait PageImageTrait
11
{
12
    /**
13
     * @ORM\ManyToOne(targetEntity="PiedWeb\CMSBundle\Entity\MediaInterface")
14
     */
15
    private $mainImage;
16
17
    /**
18
     * @var ArrayCollection
19
     */
20
    private $images;
21
22
    /**
23
     * @ORM\OneToMany(targetEntity="PiedWeb\CMSBundle\Entity\PageHasMedia", mappedBy="page",cascade={"all"}, orphanRemoval=true)
24
     * @ORM\OrderBy({"position":"ASC"})
25
     */
26
    private $pageHasMedias;
27
28
    public function __construct_image()
29
    {
30
    }
31
32
    public function setPageHasMedias($pageHasMedias)
33
    {
34
        $this->pageHasMedias = new ArrayCollection();
35
        foreach ($pageHasMedias as $pageHasMedia) {
36
            $this->addPageHasMedia($pageHasMedia);
37
        }
38
    }
39
40
    public function getPageHasMedias()
41
    {
42
        return $this->pageHasMedias;
43
    }
44
45
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
46
    {
47
        $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

47
        $pageHasMedia->setPage(/** @scrutinizer ignore-type */ $this);
Loading history...
48
        $this->pageHasMedias[] = $pageHasMedia;
49
    }
50
51
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
52
    {
53
        $this->pageHasMedias->removeElement($pageHasMedia);
54
    }
55
56
    public function getMainImage(): ?Media
57
    {
58
        return $this->mainImage;
59
    }
60
61
    public function setMainImage(?Media $mainImage): self
62
    {
63
        // TODO: Déplacer en Assert pour éviter une erreur dégueu ?!
64
        if (null === $mainImage->getWidth()) {
0 ignored issues
show
Bug introduced by
The method getWidth() does not exist on null. ( Ignorable by Annotation )

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

64
        if (null === $mainImage->/** @scrutinizer ignore-call */ getWidth()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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

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