Passed
Push — develop ( c0a645...f538e8 )
by Daniel
05:28
created

ComponentGroup::getParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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