UiTrait::setComponentGroups()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Entity\Utility;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Doctrine\ORM\Mapping as ORM;
19
use Silverback\ApiComponentsBundle\Entity\Core\ComponentGroup;
20
use Symfony\Component\Serializer\Annotation\Groups;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 *
25
 * @internal
26
 */
27
trait UiTrait
28
{
29
    #[ORM\Column(nullable: true)]
30
    public ?string $uiComponent = null;
31
32
    #[ORM\Column(type: 'json', nullable: true)]
33
    public ?array $uiClassNames = null;
34
35
    /**
36
     * @var Collection|ComponentGroup[]
37
     */
38
    #[ORM\ManyToMany(targetEntity: ComponentGroup::class)]
39
    #[Groups(['Route:manifest:read'])]
40
    private Collection $componentGroups;
41
42
    public function __construct()
43
    {
44
        $this->initComponentGroups();
45
    }
46
47 9
    private function initComponentGroups(): void
48
    {
49 9
        $this->componentGroups = new ArrayCollection();
50
    }
51
52
    /**
53
     * @return Collection|ComponentGroup[]
54
     */
55 1
    public function getComponentGroups()
56
    {
57 1
        return $this->componentGroups;
58
    }
59
60
    /**
61
     * @return static
62
     */
63
    public function setComponentGroups(iterable $componentGroups)
64
    {
65
        $this->componentGroups = new ArrayCollection();
66
        foreach ($componentGroups as $componentGroup) {
67
            $this->addComponentGroup($componentGroup);
68
        }
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return static
75
     */
76
    public function addComponentGroup(ComponentGroup $componentGroup)
77
    {
78
        if (!$this->componentGroups->contains($componentGroup)) {
79
            $this->componentGroups->add($componentGroup);
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return static
87
     */
88
    public function removeComponentGroup(ComponentGroup $componentGroup)
89
    {
90
        if ($this->componentGroups->contains($componentGroup)) {
91
            $this->componentGroups->remove($componentGroup);
0 ignored issues
show
Bug introduced by
$componentGroup of type Silverback\ApiComponents...ity\Core\ComponentGroup is incompatible with the type integer|string expected by parameter $key of Doctrine\Common\Collections\Collection::remove(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            $this->componentGroups->remove(/** @scrutinizer ignore-type */ $componentGroup);
Loading history...
92
        }
93
94
        return $this;
95
    }
96
}
97