Passed
Push — master ( 2e3594...a83f63 )
by Emanuele
03:00 queued 14s
created

FeatureSecurity::isGranted()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.2269

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 10
cts 12
cp 0.8333
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 12
nc 9
nop 1
crap 7.2269
1
<?php
2
3
namespace Ae\FeatureBundle\Security;
4
5
use Ae\FeatureBundle\Entity\Feature;
6
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
7
8
/**
9
 * Controls access to a Feature.
10
 *
11
 * @author Carlo Forghieri <[email protected]>
12
 */
13
class FeatureSecurity
14
{
15
    /**
16
     * @param AuthorizationCheckerInterface|null
17
     */
18
    protected $context;
19
20
    /**
21
     * @param AuthorizationCheckerInterface $context
22
     */
23 8
    public function __construct(AuthorizationCheckerInterface $context = null)
24
    {
25 8
        $this->context = $context;
26 8
    }
27
28
    /**
29
     * @param Feature $feature
30
     *
31
     * @return bool
32
     */
33 8
    public function isGranted(Feature $feature)
34
    {
35 8
        if (null === $this->context) {
36
            return false;
37
        }
38
39 8
        if (!$feature->isEnabled()) {
40 2
            return false;
41
        }
42
43 6
        if ($feature->getRole()) {
44 4
            if (!$this->context->isGranted($feature->getRole())) {
45
                return false;
46
            }
47
        }
48
49 6
        if ('' !== trim($feature->getParentRole())) {
50 2
            if (!$this->context->isGranted($feature->getParentRole())) {
51 2
                return false;
52
            }
53
        }
54
55 4
        return true;
56
    }
57
}
58