ImagesAwareTrait::hasImage()   A
last analyzed

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