Passed
Push — master ( 01499e...3408ae )
by Emanuele
42s
created

Feature::requiresRoleCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ae\FeatureBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
8
/**
9
 * Feature entity.
10
 *
11
 * @ORM\Table(name="application_feature")
12
 * @ORM\Entity
13
 */
14
class Feature
15
{
16
    /**
17
     * @var int
18
     *
19
     * @ORM\Column(type="integer")
20
     * @ORM\Id
21
     * @ORM\GeneratedValue(strategy="AUTO")
22
     */
23
    private $id;
24
25
    /**
26
     * @var string
27
     *
28
     * @ORM\Column(length=250)
29
     */
30
    private $name;
31
32
    /**
33
     * @var bool
34
     *
35
     * @ORM\Column(type="boolean")
36
     */
37
    private $enabled = false;
38
39
    /**
40
     * @var string
41
     *
42
     * @ORM\Column(type="text", nullable=true)
43
     */
44
    private $role;
45
46
    /**
47
     * @var ArrayCollection
48
     *
49
     * @ORM\OneToMany(targetEntity="Feature", mappedBy="parent")
50
     */
51
    private $children;
52
53
    /**
54
     * @var Feature
55
     *
56
     * @ORM\ManyToOne(targetEntity="Feature", inversedBy="children", cascade={"persist", "remove"})
57
     */
58
    private $parent;
59
60
    /**
61
     * @var null|string
62
     *
63
     * @ORM\Column(nullable=true)
64
     */
65
    private $description;
66
67 12
    public function __construct()
68
    {
69 12
        $this->children = new ArrayCollection();
70 12
    }
71
72
    public function __toString()
73
    {
74
        return $this->getName();
75
    }
76
77
    /**
78
     * Get id.
79
     *
80
     * @return int
81
     */
82
    public function getId()
83
    {
84
        return $this->id;
85
    }
86
87
    /**
88
     * Set parent.
89
     *
90
     * @param Feature $parent
91
     */
92 6
    public function setParent(Feature $parent)
93
    {
94 6
        $this->parent = $parent;
95 6
    }
96
97
    /**
98
     * Get parent.
99
     *
100
     * @return Feature
101
     */
102 6
    public function getParent()
103
    {
104 6
        return $this->parent;
105
    }
106
107
    /**
108
     * Set name.
109
     *
110
     * @param string $name
111
     */
112
    public function setName($name)
113
    {
114
        $this->name = $name;
115
    }
116
117
    /**
118
     * Get name.
119
     *
120
     * @return string
121
     */
122
    public function getName()
123
    {
124
        return $this->name;
125
    }
126
127
    /**
128
     * Set enabled.
129
     *
130
     * @param bool $enabled
131
     */
132 3
    public function setEnabled($enabled)
133
    {
134 3
        $this->enabled = $enabled;
135 3
    }
136
137
    /**
138
     * Get enabled.
139
     *
140
     * @return bool
141
     */
142 4
    public function getEnabled()
143
    {
144 4
        return $this->enabled;
145
    }
146
147
    /**
148
     * Set role.
149
     *
150
     * @param string $role
151
     */
152 4
    public function setRole($role)
153
    {
154 4
        $this->role = $role;
155 4
    }
156
157
    /**
158
     * Get role.
159
     *
160
     * @return string
161
     */
162 1
    public function getRole()
163
    {
164 1
        return $this->role;
165
    }
166
167 3
    public function hasRole(): bool
168
    {
169 3
        return !empty($this->role);
170
    }
171
172 2
    public function hasParentRole(): bool
173
    {
174 2
        return $this->parent instanceof self && $this->parent->hasRole();
175
    }
176
177 1
    public function requiresRoleCheck(): bool
178
    {
179 1
        return $this->hasRole() || $this->hasParentRole();
180
    }
181
182 2
    public function getParentRole()
183
    {
184 2
        return $this->getParent() ? $this->getParent()->getRole() : null;
185
    }
186
187
    /**
188
     * Add children.
189
     *
190
     * @param Feature $children
191
     */
192 1
    public function addFeature(Feature $children)
193
    {
194 1
        $this->children[] = $children;
195 1
    }
196
197
    /**
198
     * Get children.
199
     *
200
     * @return ArrayCollection
201
     */
202 1
    public function getChildren()
203
    {
204 1
        return $this->children;
205
    }
206
207 4
    public function isEnabled()
208
    {
209 4
        return $this->getEnabled() && ($this->getParent() ? $this->getParent()->getEnabled() : true);
210
    }
211
212
    /**
213
     * Get description.
214
     *
215
     * @return null|string
216
     */
217 1
    public function getDescription()
218
    {
219 1
        return $this->description;
220
    }
221
222
    /**
223
     * Set description.
224
     *
225
     * @param null|string $description
226
     */
227 1
    public function setDescription($description)
228
    {
229 1
        $this->description = $description;
230 1
    }
231
}
232