Passed
Push — develop ( 185343...eb895f )
by Daniel
06:30
created

ComponentGroup::getParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Entity\Content;
4
5
use ApiPlatform\Core\Annotation\ApiResource;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Silverback\ApiComponentBundle\Entity\Content\Component\AbstractComponent;
10
use Silverback\ApiComponentBundle\Entity\SortableInterface;
11
use Silverback\ApiComponentBundle\Entity\SortableTrait;
12
use Silverback\ApiComponentBundle\Entity\ValidComponentInterface;
13
use Silverback\ApiComponentBundle\Entity\ValidComponentTrait;
14
use Symfony\Component\Serializer\Annotation\Groups;
15
16
/**
17
 * Class ComponentGroup
18
 * @package Silverback\ApiComponentBundle\Entity\Content\Component
19
 * @author Daniel West <[email protected]>
20
 * @ApiResource()
21
 * @ORM\Entity()
22
 */
23
class ComponentGroup extends AbstractContent implements ValidComponentInterface, SortableInterface
24
{
25
    use SortableTrait;
26
    use ValidComponentTrait;
27
28
    /**
29
     * @Groups({"default"})
30
     */
31
    protected $componentLocations;
32
33
    /**
34
     * @ORM\ManyToOne(targetEntity="Silverback\ApiComponentBundle\Entity\Content\Component\AbstractComponent", inversedBy="componentGroups")
35
     * @ORM\JoinColumn(onDelete="SET NULL")
36
     * @var AbstractComponent
37
     */
38
    protected $parent;
39
40 18
    public function __construct(?AbstractComponent $parent = null)
41
    {
42 18
        if ($parent) {
43 2
            $this->setParent($parent);
44
        }
45 18
        $this->validComponents = new ArrayCollection;
46 18
        parent::__construct();
47 18
    }
48
49
50
    /**
51
     * @return AbstractComponent|null
52
     */
53 1
    public function getParent(): ?AbstractComponent
54
    {
55 1
        return $this->parent;
56
    }
57
58
    /**
59
     * @param AbstractComponent|null $parent
60
     * @param bool|null $cascadeValidComponent
61
     * @param bool|null $sortLast
62
     */
63 13
    public function setParent(?AbstractComponent $parent, ?bool $cascadeValidComponent = null, ?bool $sortLast = true): void
64
    {
65 13
        $this->parent = $parent;
66 13
        if ($parent && $cascadeValidComponent !== false) {
67
            // convert to bool again for $force (null becomes false)
68 13
            $this->cascadeValidComponents($parent, (bool) $cascadeValidComponent);
69
        }
70 13
        if (null === $this->sort || $sortLast !== null) {
71 13
            $this->setSort($this->calculateSort($sortLast));
72
        }
73 13
    }
74
75 1
    public function hasComponent(AbstractComponent $component)
76
    {
77 1
        foreach ($this->getComponentLocations() as $componentLocation) {
78
            if ($component === $componentLocation->getComponent()) {
79
                return true;
80
            }
81
        }
82 1
        return false;
83
    }
84
85 13
    public function getSortCollection(): Collection
86
    {
87 13
        return $this->parent ? $this->parent->getComponentGroups() : new ArrayCollection;
88
    }
89
}
90