Completed
Push — master ( ed6979...760c22 )
by Joachim
08:32
created

Brand::removeImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 5
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
use Sylius\Component\Core\Model\ImagesAwareInterface;
11
12
class Brand implements BrandInterface, ImagesAwareInterface
13
{
14
    /**
15
     * @var int
16
     */
17
    protected $id;
18
19
    /**
20
     * @var string
21
     */
22
    protected $slug;
23
24
    /**
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * @var Collection|ImageInterface[]
31
     */
32
    protected $images;
33
34
    public function __construct()
35
    {
36
        $this->images = new ArrayCollection();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function __toString(): string
43
    {
44
        return (string) $this->getName();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getId(): ?int
51
    {
52
        return $this->id;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function setId(int $id): BrandInterface
59
    {
60
        $this->id = $id;
61
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getSlug(): ?string
69
    {
70
        return $this->slug;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function setSlug(string $slug): BrandInterface
77
    {
78
        $this->slug = $slug;
79
80
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getName(): ?string
87
    {
88
        return $this->name;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function setName(string $name): BrandInterface
95
    {
96
        $this->name = $name;
97
98
        return $this;
99
    }
100
101
    /********************************
102
     * ImagesAwareInterface methods *
103
     *******************************/
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getImages(): Collection
109
    {
110
        return $this->images;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getImagesByType(string $type): Collection
117
    {
118
        return $this->images->filter(function (ImageInterface $image) use ($type) {
119
            return $type === $image->getType();
120
        });
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function hasImages(): bool
127
    {
128
        return !$this->images->isEmpty();
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function hasImage(ImageInterface $image): bool
135
    {
136
        return $this->images->contains($image);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function addImage(ImageInterface $image): void
143
    {
144
        $image->setOwner($this);
145
        $this->images->add($image);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function removeImage(ImageInterface $image): void
152
    {
153
        if ($this->hasImage($image)) {
154
            $image->setOwner(null);
155
            $this->images->removeElement($image);
156
        }
157
    }
158
}
159