Passed
Push — master ( 3ec415...9f2f47 )
by Daniel
16:25
created

AbstractComponent::setComponentPositions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
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\Entity\Utility\IdTrait;
20
use Silverback\ApiComponentsBundle\Entity\Utility\UiTrait;
21
22
/**
23
 * We must define this as an API resource, otherwise when serializing and the relation is to this class,
24
 * API Platform does not know that it will be a resource and will make it an object, not an IRI.
25
 *
26
 * @author Daniel West <[email protected]>
27
 * @ApiResource(collectionOperations={}, itemOperations={})
28
 */
29
abstract class AbstractComponent implements ComponentInterface
30
{
31
    use IdTrait;
32
    use UiTrait;
33
34
    /**
35
     * @var Collection|ComponentPosition[]
36
     */
37
    private Collection $componentPositions;
38
39 10
    public function __construct()
40
    {
41 10
        $this->initComponentCollections();
42 10
        $this->componentPositions = new ArrayCollection();
43 10
    }
44
45
    public function isPositionRestricted(): bool
46
    {
47
        return false;
48
    }
49
50
    public function getComponentPositions()
51
    {
52
        return $this->componentPositions;
53
    }
54
55
    public function setComponentPositions(iterable $componentPositions): self
56
    {
57
        $this->componentPositions = new ArrayCollection();
58
        foreach ($componentPositions as $componentPosition) {
59
            $this->addComponentPosition($componentPosition);
60
        }
61
62
        return $this;
63
    }
64
65
    public function addComponentPosition(ComponentPosition $componentPosition): self
66
    {
67
        $componentPosition->setComponent($this);
68
        $this->componentPositions->add($componentPosition);
69
70
        return $this;
71
    }
72
}
73