Passed
Push — develop ( 5d2ce0...8a9894 )
by Daniel
04:55
created

ValidComponentTrait::cascadeValidComponents()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Silverback\ApiComponentBundle\Entity\Component\AbstractComponent;
8
use Silverback\ApiComponentBundle\Validator\Constraints as ACBAssert;
9
10
trait ValidComponentTrait
11
{
12
    /**
13
     * @ORM\Column(type="array")
14
     * @ACBAssert\ComponentTypeClasses()
15
     * @var ArrayCollection|AbstractComponent[]
16
     */
17
    protected $validComponents;
18
19 8
    private function initValidComponents(): self
20
    {
21 8
        if (!($this->validComponents instanceof ArrayCollection)) {
22 3
            $this->validComponents = new ArrayCollection();
23
        }
24 8
        return $this;
25
    }
26
27
    /**
28
     * @return ArrayCollection
29
     */
30 8
    public function getValidComponents(): ArrayCollection
31
    {
32 8
        $this->initValidComponents();
33 8
        return $this->validComponents;
34
    }
35
36
    /**
37
     * @param string $component
38
     * @return ValidComponentTrait
39
     */
40 8
    public function addValidComponent(string $component): self
41
    {
42 8
        $this->initValidComponents();
43 8
        if (!$this->validComponents->contains($component)) {
44 8
            $this->validComponents->add($component);
45
        }
46 8
        return $this;
47
    }
48
49
    /**
50
     * @param string $component
51
     * @return ValidComponentTrait
52
     */
53 1
    public function removeValidComponent(string $component): self
54
    {
55 1
        $this->initValidComponents();
56 1
        $this->validComponents->removeElement($component);
57 1
        return $this;
58
    }
59
60
    /**
61
     * @param ValidComponentInterface $entity
62
     * @param bool $force
63
     */
64 7
    protected function cascadeValidComponents(ValidComponentInterface $entity, bool $force = false): void
65
    {
66 7
        $entityValidComponents = $entity->getValidComponents();
67 7
        if ($force || $entityValidComponents->count()) {
68 7
            $this->validComponents = $entityValidComponents;
69
        }
70 7
    }
71
}
72