Passed
Pull Request — master (#64)
by Emanuele
03:28
created

Feature::setExpiration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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