Completed
Push — master ( 09ebf3...a2507a )
by Joachim
10s
created

ImagesAwareTrait::getImages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\SyliusBrandPlugin\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Sylius\Component\Core\Model\ImageInterface;
10
11
trait ImagesAwareTrait
12
{
13
14
    /** @var Collection|ImageInterface[] */
15
    protected $images;
16
17
    public function initializeImagesCollection(): void
18
    {
19
        $this->images = new ArrayCollection();
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getImages(): Collection
26
    {
27
        return $this->images;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getImagesByType(string $type): Collection
34
    {
35
        return $this->images->filter(function (ImageInterface $image) use ($type) {
36
            return $type === $image->getType();
37
        });
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function hasImages(): bool
44
    {
45
        return !$this->images->isEmpty();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function hasImage(ImageInterface $image): bool
52
    {
53
        return $this->images->contains($image);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function addImage(ImageInterface $image): void
60
    {
61
        if (false === $this->hasImage($image)) {
62
            $image->setOwner($this);
63
            $this->images->add($image);
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function removeImage(ImageInterface $image): void
71
    {
72
        if ($this->hasImage($image)) {
73
            $image->setOwner(null);
74
            $this->images->removeElement($image);
75
        }
76
    }
77
}
78