Passed
Push — master ( a023d2...6d474d )
by Daniel
06:11
created

ValidComponentTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A cascadeValidComponents() 0 5 3
A removeValidComponent() 0 5 1
A getValidComponents() 0 4 1
A initValidComponents() 0 6 2
A addValidComponent() 0 7 2
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\Content\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 16
    private function initValidComponents(): self
20
    {
21 16
        if (!($this->validComponents instanceof ArrayCollection)) {
22 3
            $this->validComponents = new ArrayCollection();
23
        }
24 16
        return $this;
25
    }
26
27
    /**
28
     * @return ArrayCollection
29
     */
30 16
    public function getValidComponents(): ArrayCollection
31
    {
32 16
        $this->initValidComponents();
33 16
        return $this->validComponents;
34
    }
35
36
    /**
37
     * @param string $component
38
     * @return ValidComponentTrait
39
     */
40 15
    public function addValidComponent(string $component): self
41
    {
42 15
        $this->initValidComponents();
43 15
        if (!$this->validComponents->contains($component)) {
44 15
            $this->validComponents->add($component);
45
        }
46 15
        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 15
    protected function cascadeValidComponents(ValidComponentInterface $entity, bool $force = false): void
65
    {
66 15
        $entityValidComponents = $entity->getValidComponents();
67 15
        if ($force || $entityValidComponents->count()) {
68 14
            $this->validComponents = $entityValidComponents;
69
        }
70 15
    }
71
}
72