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

AbstractComponent   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 23.53%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 42
ccs 4
cts 17
cp 0.2353
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setComponentPositions() 0 8 2
A isPositionRestricted() 0 3 1
A getComponentPositions() 0 3 1
A addComponentPosition() 0 6 1
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