Passed
Pull Request — master (#34)
by Igor
04:44
created

ImagesAwareTrait::hasImages()   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\Model;
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
    /** @var Collection|ImageInterface[] */
14
    protected $images;
15
16
    public function initializeImagesCollection(): void
17
    {
18
        $this->images = new ArrayCollection();
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getImages(): Collection
25
    {
26
        return $this->images;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getImagesByType(string $type): Collection
33
    {
34
        return $this->images->filter(function (ImageInterface $image) use ($type) {
35
            return $type === $image->getType();
36
        });
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function hasImages(): bool
43
    {
44
        return !$this->images->isEmpty();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function hasImage(ImageInterface $image): bool
51
    {
52
        return $this->images->contains($image);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function addImage(ImageInterface $image): void
59
    {
60
        if (false === $this->hasImage($image)) {
61
            $image->setOwner($this);
62
            $this->images->add($image);
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function removeImage(ImageInterface $image): void
70
    {
71
        if ($this->hasImage($image)) {
72
            $image->setOwner(null);
73
            $this->images->removeElement($image);
74
        }
75
    }
76
}
77