Test Setup Failed
Push — develop ( 47a494...4bd721 )
by Daniel
04:03
created

ComponentGroup::hasComponent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
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 18
     * @var AbstractComponent
37
     */
38 18
    protected $parent;
39 18
40 18
    public function __construct(?AbstractComponent $parent = null)
41
    {
42
        if ($parent) {
43
            $this->setParent($parent);
44
        }
45
        $this->validComponents = new ArrayCollection;
46 1
        parent::__construct();
47
    }
48 1
49
50
    /**
51
     * @return AbstractComponent|null
52
     */
53
    public function getParent(): ?AbstractComponent
54
    {
55 13
        return $this->parent;
56
    }
57 13
58 13
    /**
59
     * @param AbstractComponent|null $parent
60 13
     * @param bool|null $cascadeValidComponent
61
     * @param bool|null $sortLast
62 13
     */
63
    public function setParent(?AbstractComponent $parent, ?bool $cascadeValidComponent = null, ?bool $sortLast = true): void
64 1
    {
65
        $this->parent = $parent;
66 1
        if ($parent && $cascadeValidComponent !== false) {
67
            // convert to bool again for $force (null becomes false)
68
            $this->cascadeValidComponents($parent, (bool) $cascadeValidComponent);
69
        }
70
        if (null === $this->sort || $sortLast !== null) {
71 1
            $this->setSort($this->calculateSort($sortLast));
72
        }
73
    }
74
75
    public function hasComponent(AbstractComponent $component)
76
    {
77
        foreach ($this->getComponentLocations() as $componentLocation) {
78
            if ($component === $componentLocation->getComponent()) {
79
                return true;
80
            }
81
        }
82
        return false;
83
    }
84
85
    public function getSortCollection(): Collection
86
    {
87
        return $this->parent ? $this->parent->getComponentGroups() : new ArrayCollection;
88
    }
89
}
90