Test Failed
Branch develop (ac2838)
by Daniel
09:09
created

ValidComponentTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

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