Passed
Pull Request — main (#146)
by Daniel
07:02 queued 02:35
created

ComponentGroup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
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\Metadata\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
    normalizationContext: ['groups' => ['ComponentGroup:read']],
32
    denormalizationContext: ['groups' => ['ComponentGroup:write']],
33
    mercure: true
34
)]
35
#[UniqueEntity(fields: ['reference'], message: 'There is already a ComponentGroup resource with that reference.')]
36
class ComponentGroup
37
{
38
    use IdTrait;
39
    use TimestampedTrait;
40
41
    #[Groups(['ComponentGroup:read', 'ComponentGroup:write'])]
42
    #[Assert\NotBlank(message: 'The reference cannot be blank.')]
43
    public ?string $reference = null;
44
45
    #[Assert\NotBlank(message: 'The location cannot be blank.')]
46
    #[Groups(['ComponentGroup:read', 'ComponentGroup:write'])]
47
    public ?string $location = null;
48
49
    /**
50
     * @var Collection|Layout[]
51
     */
52
    #[Groups(['ComponentGroup:read', 'ComponentGroup:write'])]
53
    public Collection $layouts;
54
55
    /**
56
     * @var Collection|Page[]
57
     */
58
    #[Groups(['ComponentGroup:read', 'ComponentGroup:write'])]
59
    public Collection $pages;
60
61
    /**
62
     * @var Collection|AbstractComponent[]
63
     */
64
    #[Groups(['ComponentGroup:read', 'ComponentGroup:write'])]
65
    public Collection $components;
66
67
    /**
68
     * @var Collection|ComponentPosition[]
69
     */
70
    #[Groups(['ComponentGroup:read', 'ComponentGroup:write'])]
71
    public Collection $componentPositions;
72
73
    /**
74
     * @var string[]|null
75
     */
76
    #[Groups(['ComponentGroup:read', 'ComponentGroup:write'])]
77
    public ?array $allowedComponents = null;
78
79
    public function __construct()
80
    {
81
        $this->layouts = new ArrayCollection();
82
        $this->pages = new ArrayCollection();
83
        $this->components = new ArrayCollection();
84
        $this->componentPositions = new ArrayCollection();
85
    }
86
87
    public function setReference(string $reference): self
88
    {
89
        $this->reference = $reference;
90
91
        return $this;
92
    }
93
94
    public function setLocation(string $location): self
95
    {
96
        $this->location = $location;
97
98
        return $this;
99
    }
100
101
    public function setLayouts(iterable $layouts): self
102
    {
103
        $this->layouts = new ArrayCollection();
104
        foreach ($layouts as $layout) {
105
            $this->addLayout($layout);
106
        }
107
108
        return $this;
109
    }
110
111
    public function addLayout(Layout $layout): self
112
    {
113
        if (!$this->layouts->contains($layout)) {
114
            $this->layouts->add($layout);
115
            $layout->addComponentGroup($this);
116
        }
117
118
        return $this;
119
    }
120
121
    public function setPages(iterable $pages): self
122
    {
123
        $this->pages = new ArrayCollection();
124
        foreach ($pages as $page) {
125
            $this->addPage($page);
126
        }
127
128
        return $this;
129
    }
130
131
    public function addPage(Page $page): self
132
    {
133
        if (!$this->pages->contains($page)) {
134
            $this->pages->add($page);
135
            $page->addComponentGroup($this);
136
        }
137
138
        return $this;
139
    }
140
141
    public function setComponents(iterable $components): self
142
    {
143
        $this->components = new ArrayCollection();
144
        foreach ($components as $component) {
145
            $this->addComponent($component);
146
        }
147
148
        return $this;
149
    }
150
151
    public function addComponent(AbstractComponent $component): self
152
    {
153
        if (!$this->components->contains($component)) {
154
            $this->components->add($component);
155
            $component->addComponentGroup($this);
156
        }
157
158
        return $this;
159
    }
160
161
    public function setComponentPositions(iterable $componentPositions): self
162
    {
163
        $this->componentPositions = new ArrayCollection();
164
        foreach ($componentPositions as $componentPosition) {
165
            $this->addComponentPosition($componentPosition);
166
        }
167
168
        return $this;
169
    }
170
171
    public function addComponentPosition(ComponentPosition $componentPosition): self
172
    {
173
        if (!$this->componentPositions->contains($componentPosition)) {
174
            $this->componentPositions->add($componentPosition);
175
        }
176
177
        return $this;
178
    }
179
180
    public function setAllowedComponents(?iterable $allowedComponents): self
181
    {
182
        if (!$allowedComponents) {
0 ignored issues
show
introduced by
$allowedComponents is of type iterable|null, thus it always evaluated to false.
Loading history...
183
            $this->allowedComponents = null;
184
185
            return $this;
186
        }
187
        $this->allowedComponents = [];
188
        foreach ($allowedComponents as $componentIri) {
189
            $this->addAllowedComponent($componentIri);
190
        }
191
192
        return $this;
193
    }
194
195
    public function addAllowedComponent(string $allowedComponent): self
196
    {
197
        if (null === $this->allowedComponents) {
198
            $this->allowedComponents = [];
199
        }
200
        $this->allowedComponents[] = $allowedComponent;
201
202
        return $this;
203
    }
204
}
205