Completed
Push — develop ( f05cec...11f198 )
by Daniel
07:33
created

ComponentGroup::getSortCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
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
    protected $parent;
37
38 6
    public function __construct(?AbstractComponent $parent = null)
39
    {
40 6
        if ($parent) {
41
            $this->setParent($parent);
42
        }
43 6
        $this->validComponents = new ArrayCollection;
44 6
        parent::__construct();
45 6
    }
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 3
    public function setParent(?AbstractComponent $parent, ?bool $cascadeValidComponent = null, ?bool $sortLast = true): void
62
    {
63 3
        $this->parent = $parent;
64 3
        if ($parent && $cascadeValidComponent !== false) {
65
            // convert to bool again for $force (null becomes false)
66 3
            $this->cascadeValidComponents($parent, (bool) $cascadeValidComponent);
67
        }
68 3
        if (null === $this->sort || $sortLast !== null) {
69 3
            $this->setSort($this->calculateSort($sortLast));
70
        }
71 3
    }
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 3
    public function getSortCollection(): Collection
84
    {
85 3
        return $this->parent ? $this->parent->getComponentGroups() : new ArrayCollection;
86
    }
87
}
88