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

FeatureSecurity   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 49
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B isGranted() 0 29 8
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 12
    public function __construct(AuthorizationCheckerInterface $context = null)
24
    {
25 12
        $this->context = $context;
26 12
    }
27
28
    /**
29
     * @param Feature $feature
30
     *
31
     * @return bool
32
     */
33 12
    public function isGranted(Feature $feature)
34
    {
35
        // feature is enabled without required roles
36
        // there's no need to check on user roles
37 12
        if (!$feature->requiresRoleCheck()) {
38 4
            return $feature->isEnabled();
39
        }
40
41 8
        if (null === $this->context) {
42
            return false;
43
        }
44
45 8
        if (!$feature->isEnabled()) {
46 2
            return false;
47
        }
48
49 6
        if ($feature->getRole()) {
50 4
            if (!$this->context->isGranted($feature->getRole())) {
51
                return false;
52
            }
53
        }
54
55 6
        if ('' !== trim($feature->getParentRole())) {
56 2
            if (!$this->context->isGranted($feature->getParentRole())) {
57 2
                return false;
58
            }
59
        }
60
61 4
        return true;
62
    }
63
}
64