Passed
Push — develop ( 53a66f...07e04a )
by Daniel
05:35
created

ValidComponentTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removeValidComponent() 0 4 1
A cascadeValidComponents() 0 11 3
A getValidComponents() 0 3 1
A addValidComponent() 0 6 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\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
    /**
20
     * @return ArrayCollection
21
     */
22 5
    public function getValidComponents(): ArrayCollection
23
    {
24 5
        return $this->validComponents;
25
    }
26
27
    /**
28
     * @param string $component
29
     * @return ValidComponentTrait
30
     */
31 5
    public function addValidComponent(string $component): self
32
    {
33 5
        if (!$this->validComponents->contains($component)) {
34 5
            $this->validComponents->add($component);
35
        }
36 5
        return $this;
37
    }
38
39
    /**
40
     * @param string $component
41
     * @return ValidComponentTrait
42
     */
43
    public function removeValidComponent(string $component): self
44
    {
45
        $this->validComponents->removeElement($component);
46
        return $this;
47
    }
48
49
    /**
50
     * @param ValidComponentInterface $entity
51
     * @param bool $force
52
     */
53 5
    protected function cascadeValidComponents(ValidComponentInterface $entity, bool $force = false): void
54
    {
55 5
        if ($force) {
56
            // Set to true so force the cascade whether empty or not
57
            $this->validComponents = $entity->getValidComponents();
58
            return;
59
        }
60
        // Default behaviour - cascade if set on parent
61 5
        $parentValidComponents = $entity->getValidComponents();
62 5
        if ($parentValidComponents->count()) {
63 5
            $this->validComponents = $entity->getValidComponents();
64
        }
65 5
    }
66
}
67