Passed
Push — develop ( 605c3e...4f08b4 )
by Daniel
06:09
created

AbstractComponent::getComponentName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Entity\Content\Component;
4
5
use ApiPlatform\Core\Annotation\ApiProperty;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Ramsey\Uuid\Uuid;
10
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent;
11
use Silverback\ApiComponentBundle\Entity\Content\ComponentGroup;
12
use Silverback\ApiComponentBundle\Entity\Content\ContentInterface;
13
use Silverback\ApiComponentBundle\Entity\DeleteCascadeInterface;
14
use Silverback\ApiComponentBundle\Entity\ValidComponentTrait;
15
use Symfony\Component\Serializer\Annotation\Groups;
16
17
/**
18
 * Class AbstractComponent
19
 * @package Silverback\ApiComponentBundle\Entity\Content\Component
20
 * @author Daniel West <[email protected]>
21
 * @ORM\Entity()
22
 * @ORM\Table(name="component")
23
 * @ORM\InheritanceType("SINGLE_TABLE")
24
 * @ORM\DiscriminatorColumn(name="type", type="string")
25
 * @ORM\DiscriminatorMap({
26
 *     "content" = "Silverback\ApiComponentBundle\Entity\Content\Component\Content\Content",
27
 *     "form" = "Silverback\ApiComponentBundle\Entity\Content\Component\Form\Form",
28
 *     "gallery" = "Silverback\ApiComponentBundle\Entity\Content\Component\Gallery\Gallery",
29
 *     "gallery_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Gallery\GalleryItem",
30
 *     "hero" = "Silverback\ApiComponentBundle\Entity\Content\Component\Hero\Hero",
31
 *     "feature_columns" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\Columns\FeatureColumns",
32
 *     "feature_columns_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\Columns\FeatureColumnsItem",
33
 *     "feature_stacked" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\Stacked\FeatureStacked",
34
 *     "feature_stacked_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\Stacked\FeatureStackedItem",
35
 *     "feature_text_list" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\TextList\FeatureTextList",
36
 *     "feature_text_list_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Feature\TextList\FeatureTextListItem",
37
 *     "article" = "Silverback\ApiComponentBundle\Entity\Content\Component\Article\Article",
38
 *     "nav_bar" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\NavBar\NavBar",
39
 *     "nav_bar_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\NavBar\NavBarItem",
40
 *     "tabs" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\Tabs\Tabs",
41
 *     "tabs_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\Tabs\TabsItem",
42
 *     "menu" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\Menu\Menu",
43
 *     "menu_item" = "Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\Menu\MenuItem"
44
 * })
45
 * @ORM\EntityListeners({"Silverback\ApiComponentBundle\EntityListener\ComponentListener"})
46
 */
47
abstract class AbstractComponent implements ComponentInterface, DeleteCascadeInterface
48
{
49
    use ValidComponentTrait;
50
51
    /**
52
     * @ORM\Id()
53
     * @ORM\Column(type="string")
54
     * @var string
55
     */
56
    private $id;
57
58
    /**
59
     * @ORM\Column(nullable=true)
60
     * @Groups({"component", "content"})
61
     * @var null|string
62
     */
63
    private $className;
64
65
    /**
66
     * @ORM\OneToMany(targetEntity="Silverback\ApiComponentBundle\Entity\Content\Component\ComponentLocation", mappedBy="component")
67
     * @var ArrayCollection|ComponentLocation[]
68
     */
69
    protected $locations;
70
71
    /**
72
     * @ORM\OneToMany(targetEntity="Silverback\ApiComponentBundle\Entity\Content\ComponentGroup", mappedBy="parent", cascade={"persist"})
73
     * @ApiProperty(attributes={"fetchEager": false})
74
     * @Groups({"component", "content"})
75
     * @var ArrayCollection|ComponentGroup[]
76
     */
77
    protected $componentGroups;
78
79
    /**
80
     * AbstractComponent constructor.
81
     */
82 43
    public function __construct()
83
    {
84 43
        $this->id = Uuid::uuid4()->getHex();
85 43
        $this->locations = new ArrayCollection;
86 43
        $this->componentGroups = new ArrayCollection;
87 43
        $this->validComponents = new ArrayCollection;
88 43
    }
89
90
    /**
91
     * @return string
92
     */
93 2
    public function getId(): string
94
    {
95 2
        return $this->id;
96
    }
97
98
    /**
99
     * @return null|string
100
     */
101 1
    public function getClassName(): ?string
102
    {
103 1
        return $this->className;
104
    }
105
106
    /**
107
     * @param null|string $className
108
     * @return AbstractComponent
109
     */
110 2
    public function setClassName(?string $className): AbstractComponent
111
    {
112 2
        $this->className = $className;
113 2
        return $this;
114
    }
115
116
    /**
117
     * @param ContentInterface $content
118
     * @return AbstractComponent
119
     */
120
    public function addLocation(ContentInterface $content): AbstractComponent
121
    {
122
        $this->locations->add($content);
123
        return $this;
124
    }
125
126
    /**
127
     * @param ContentInterface $content
128
     * @return AbstractComponent
129
     */
130
    public function removeLocation(ContentInterface $content): AbstractComponent
131
    {
132
        $this->locations->removeElement($content);
133
        return $this;
134
    }
135
136
    /**
137
     * @param array $componentGroups
138
     * @return AbstractComponent
139
     */
140
    public function setComponentGroups(array $componentGroups): AbstractComponent
141
    {
142
        $this->componentGroups = new ArrayCollection;
143
        foreach ($componentGroups as $componentGroup) {
144
            $this->addComponentGroup($componentGroup);
145
        }
146
        return $this;
147
    }
148
149
    /**
150
     * @param ComponentGroup $componentGroup
151
     * @return AbstractComponent
152
     */
153 12
    public function addComponentGroup(ComponentGroup $componentGroup): AbstractComponent
154
    {
155 12
        $componentGroup->setParent($this);
156 12
        $this->componentGroups->add($componentGroup);
157 12
        return $this;
158
    }
159
160
    /**
161
     * @param ComponentGroup $componentGroup
162
     * @return AbstractComponent
163
     */
164
    public function removeComponentGroup(ComponentGroup $componentGroup): AbstractComponent
165
    {
166
        $this->componentGroups->removeElement($componentGroup);
167
        return $this;
168
    }
169
170
    /**
171
     * @return ArrayCollection|ComponentGroup[]
172
     */
173 2
    public function getComponentGroups(): Collection
174
    {
175 2
        return $this->componentGroups;
176
    }
177
178
    /**
179
     * Return the component name for front-end to decipher
180
     * @Groups({"content", "component"})
181
     * @return string
182
     */
183
    public static function getComponentName(): string
184
    {
185
        $explodedClass = explode('\\', static::class);
186
        return array_pop($explodedClass);
187
    }
188
189
    /**
190
     * @return bool
191
     */
192
    public function onDeleteCascade(): bool
193
    {
194
        return false;
195
    }
196
197
    /**
198
     * @param AbstractComponent $component
199
     * @param int $componentGroupOffset
200
     * @return ComponentGroup
201
     * @throws \InvalidArgumentException
202
     */
203 1
    private function getComponentComponentGroup(AbstractComponent $component, int $componentGroupOffset = 0): ComponentGroup
204
    {
205
        /** @var ComponentGroup $componentGroup */
206 1
        $componentGroup = $component->getComponentGroups()->get($componentGroupOffset);
207 1
        if (null === $componentGroup) {
208
            throw new \InvalidArgumentException(sprintf('There is no component group child of this component with the offset %d', $componentGroupOffset));
209
        }
210 1
        return $componentGroup;
211
    }
212
213
    /**
214
     * @Groups({"component_write"})
215
     * @param AbstractComponent $parent
216
     * @param int $componentGroupOffset
217
     * @return AbstractComponent
218
     * @throws \InvalidArgumentException
219
     */
220 1
    public function setParent(AbstractComponent $parent, int $componentGroupOffset = 0): AbstractComponent
221
    {
222 1
        if (!\in_array($parent, $this->getParentComponents(), true)) {
223 1
            $componentGroup = $this->getComponentComponentGroup($parent, $componentGroupOffset);
224 1
            if (!$componentGroup->hasComponent($this)) {
225 1
                $componentGroup->addComponent(new ComponentLocation($componentGroup, $this));
226
            }
227
        }
228 1
        return $this;
229
    }
230
231
    /**
232
     * @return AbstractComponent[]
233
     */
234 1
    private function getParentComponents(): array
235
    {
236 1
        $parentContent = $this->getParentContent();
237 1
        return array_unique(
238 1
            array_filter(
239 1
                array_map(
240 1
                    function ($content) {
241
                        if ($content instanceof ComponentGroup) {
242
                            return $content->getParent();
243
                        }
244 1
                    },
245 1
                    $parentContent
246
                )
247
            )
248
        );
249
    }
250
251
    /**
252
     * @return AbstractContent[]
253
     */
254 1
    private function getParentContent(): array
255
    {
256 1
        return array_unique(
257 1
            array_filter(
258 1
                array_map(
259 1
                    function (ComponentLocation $loc) {
260
                        return $loc->getContent();
261 1
                    },
262 1
                    $this->locations->toArray()
263
                )
264
            )
265
        );
266
    }
267
}
268