Completed
Push — master ( d7020d...a4700d )
by Daniel
06:50
created

AbstractContent::addSecurityRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\Entity\Content;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping as ORM;
10
use Ramsey\Uuid\Uuid;
11
use Silverback\ApiComponentBundle\Entity\Component\ComponentLocation;
12
use Silverback\ApiComponentBundle\Entity\RestrictedResourceInterface;
13
use Silverback\ApiComponentBundle\Entity\TimestampedEntityTrait;
14
15
/**
16
 * @package Silverback\ApiComponentBundle\Entity
17
 * @author Daniel West <[email protected]>
18
 * @ORM\Entity(repositoryClass="Silverback\ApiComponentBundle\Repository\Content\AbstractContentRepository")
19
 * @ORM\Table(name="content")
20
 * @ORM\InheritanceType("SINGLE_TABLE")
21
 * @ORM\DiscriminatorColumn(name="type", type="string")
22
 * @ORM\DiscriminatorMap({
23
 *     "abstract_page" = "Silverback\ApiComponentBundle\Entity\Content\Page\AbstractPage",
24
 *     "static_page" = "Silverback\ApiComponentBundle\Entity\Content\Page\StaticPage",
25
 *     "dynamic_page" = "Silverback\ApiComponentBundle\Entity\Content\Page\DynamicPage",
26
 *     "component_group" = "Silverback\ApiComponentBundle\Entity\Content\ComponentGroup\ComponentGroup"
27
 * })
28
 */
29
abstract class AbstractContent implements ContentInterface, RestrictedResourceInterface
30
{
31
    use TimestampedEntityTrait;
32
33
    /**
34
     * @ORM\Id()
35
     * @ORM\Column(type="string", length=36)
36
     * @var string
37
     */
38
    protected $id;
39
40
    /**
41
     * @ORM\OneToMany(targetEntity="Silverback\ApiComponentBundle\Entity\Component\ComponentLocation", mappedBy="content", cascade={"persist", "remove"})
42
     * @ORM\OrderBy({"sort"="ASC"})
43
     * @var Collection|ComponentLocation[]
44
     */
45
    protected $componentLocations;
46
47 7
    /**
48
     * @ORM\Column(type="json_array", nullable=true)
49 7
     * @var array|null
50 7
     */
51 7
    protected $securityRoles;
52
53
    public function __construct()
54
    {
55
        $this->id = Uuid::uuid4()->getHex();
56
        $this->componentLocations = new ArrayCollection;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getId(): string
63
    {
64
        return $this->id;
65
    }
66
67
    /**
68
     * @return Collection|ComponentLocation[]
69
     */
70
    public function getComponentLocations(): Collection
71
    {
72
        return $this->componentLocations;
73
    }
74
75
    /**
76
     * @param ComponentLocation[]|iterable $componentLocations
77
     * @return AbstractContent
78
     */
79
    public function setComponentLocations(iterable $componentLocations): AbstractContent
80
    {
81
        $this->componentLocations = new ArrayCollection;
82
        /** @var ComponentLocation $componentLocation */
83
        foreach ($componentLocations as $componentLocation) {
84
            $this->addComponentLocation($componentLocation);
85
        }
86
        return $this;
87
    }
88
89
    /**
90
     * @param ComponentLocation $componentLocation
91
     * @return AbstractContent
92
     */
93
    public function addComponentLocation(ComponentLocation $componentLocation): AbstractContent
94
    {
95
        if (!$this->componentLocations->contains($componentLocation)) {
96
            $this->componentLocations->add($componentLocation);
97
            $componentLocation->setContent($this);
98
        }
99
        return $this;
100
    }
101
102
    /**
103
     * @param ComponentLocation $componentLocation
104
     * @return AbstractContent
105
     */
106
    public function removeComponentLocation(ComponentLocation $componentLocation): AbstractContent
107
    {
108
        if ($this->componentLocations->contains($componentLocation)) {
109
            $this->componentLocations->removeElement($componentLocation);
110
            if ($componentLocation->getContent() === $this) {
111
                $componentLocation->setContent(null);
112
            }
113
        }
114
        return $this;
115
    }
116
117
    public function getSecurityRoles(): ?array
118
    {
119
        return $this->securityRoles;
120
    }
121
122
    public function addSecurityRole(string $securityRole): self
123
    {
124
        $this->securityRoles[] = $securityRole;
125
        return $this;
126
    }
127
128
    public function setSecurityRoles(?array $securityRoles): self
129
    {
130
        if (!$securityRoles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $securityRoles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
131
            $this->securityRoles = $securityRoles;
132
            return $this;
133
        }
134
        $this->securityRoles = [];
135
        foreach ($securityRoles as $securityRole) {
136
            $this->addSecurityRole($securityRole);
137
        }
138
        return $this;
139
    }
140
}
141