Test Failed
Push — develop ( b13210...c32797 )
by Daniel
04:26
created

ComponentGroup::setParent()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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