Passed
Push — master ( 5558e1...346da5 )
by Daniel
12:03 queued 06:11
created

ComponentCollection::setLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Entity\Core;
15
16
use ApiPlatform\Core\Annotation\ApiResource;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Doctrine\Common\Collections\Collection;
19
use Silverback\ApiComponentsBundle\Annotation as Silverback;
20
use Silverback\ApiComponentsBundle\Entity\Utility\IdTrait;
21
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
22
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
23
use Symfony\Component\Serializer\Annotation\Groups;
24
use Symfony\Component\Validator\Constraints as Assert;
25
26
/**
27
 * @author Daniel West <[email protected]>
28
 *
29
 * @Silverback\Timestamped
30
 * @ApiResource(
31
 *     mercure=true,
32
 *     normalizationContext={ "groups"={"ComponentCollection:read"} },
33
 *     denormalizationContext={ "groups"={"ComponentCollection:write"} }
34
 * )
35
 * @UniqueEntity(fields={"reference"}, message="There is already a ComponentCollection resource with that reference.")
36
 */
37
class ComponentCollection
38
{
39
    use IdTrait;
40
    use TimestampedTrait;
41
42
    /**
43
     * @Assert\NotBlank(message="The reference cannot be blank.")
44
     * @Groups({"ComponentCollection:read", "ComponentCollection:write"})
45
     */
46
    public string $reference;
47
48
    /**
49
     * @Assert\NotBlank(message="The location cannot be blank.")
50
     * @Groups({"ComponentCollection:read", "ComponentCollection:write"})
51
     */
52
    public string $location;
53
54
    /**
55
     * @var Collection|Layout[]
56
     * @Groups({"ComponentCollection:read", "ComponentCollection:write"})
57
     */
58
    public $layouts;
59
60
    /**
61
     * @var Collection|Page[]
62
     * @Groups({"ComponentCollection:read", "ComponentCollection:write"})
63
     */
64
    public Collection $pages;
65
66
    /**
67
     * @var Collection|AbstractComponent[]
68
     * @Groups({"ComponentCollection:read", "ComponentCollection:write"})
69
     */
70
    public Collection $components;
71
72
    /**
73
     * @var Collection|ComponentPosition[]
74
     * @Groups({"ComponentCollection:read", "ComponentCollection:write"})
75
     */
76
    public Collection $componentPositions;
77
78
    /**
79
     * @Groups({"ComponentCollection:read", "ComponentCollection:write"})
80
     */
81
    public ?Collection $allowedComponents = null;
82
83
    public function __construct()
84
    {
85
        $this->layouts = new ArrayCollection();
86
        $this->pages = new ArrayCollection();
87
        $this->components = new ArrayCollection();
88
        $this->componentPositions = new ArrayCollection();
89
    }
90
91
    public function setReference(string $reference): self
92
    {
93
        $this->reference = $reference;
94
95
        return $this;
96
    }
97
98
    public function setLocation(string $location): self
99
    {
100
        $this->location = $location;
101
102
        return $this;
103
    }
104
105
    public function setLayouts(iterable $layouts): self
106
    {
107
        $this->layouts = new ArrayCollection();
108
        foreach ($layouts as $layout) {
109
            $this->addLayout($layout);
110
        }
111
112
        return $this;
113
    }
114
115
    public function addLayout(Layout $layout): self
116
    {
117
        if (!$this->layouts->contains($layout)) {
118
            $this->layouts->add($layout);
119
            $layout->addComponentCollection($this);
120
        }
121
122
        return $this;
123
    }
124
125
    public function setPages(iterable $pages): self
126
    {
127
        $this->pages = new ArrayCollection();
128
        foreach ($pages as $page) {
129
            $this->addPage($page);
130
        }
131
132
        return $this;
133
    }
134
135
    public function addPage(Page $page): self
136
    {
137
        if (!$this->pages->contains($page)) {
138
            $this->pages->add($page);
139
            $page->addComponentCollection($this);
140
        }
141
142
        return $this;
143
    }
144
145
    public function setComponents(iterable $components): self
146
    {
147
        $this->components = new ArrayCollection();
148
        foreach ($components as $component) {
149
            $this->addComponent($component);
150
        }
151
152
        return $this;
153
    }
154
155
    public function addComponent(AbstractComponent $component): self
156
    {
157
        if (!$this->components->contains($component)) {
158
            $this->components->add($component);
159
            $component->addComponentCollection($this);
160
        }
161
162
        return $this;
163
    }
164
165
    public function setComponentPositions(iterable $componentPositions): self
166
    {
167
        $this->componentPositions = new ArrayCollection();
168
        foreach ($componentPositions as $componentPosition) {
169
            $this->addComponentPosition($componentPosition);
170
        }
171
172
        return $this;
173
    }
174
175
    public function addComponentPosition(ComponentPosition $componentPosition): self
176
    {
177
        if (!$this->componentPositions->contains($componentPosition)) {
178
            $this->componentPositions->add($componentPosition);
179
        }
180
181
        return $this;
182
    }
183
184
    public function setAllowedComponents(iterable $allowedComponents): self
185
    {
186
        $this->allowedComponents = new ArrayCollection();
187
        foreach ($allowedComponents as $componentIri) {
188
            $this->addAllowedComponent($componentIri);
189
        }
190
191
        return $this;
192
    }
193
194
    public function addAllowedComponent(string $allowedComponent): self
195
    {
196
        $this->allowedComponents->add($allowedComponent);
0 ignored issues
show
Bug introduced by
The method add() 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

196
        $this->allowedComponents->/** @scrutinizer ignore-call */ 
197
                                  add($allowedComponent);

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...
197
198
        return $this;
199
    }
200
}
201