Passed
Pull Request — master (#46)
by Emanuele
02:37
created

FeatureSecurity::isGranted()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.1867

Importance

Changes 0
Metric Value
cc 8
eloc 13
nc 10
nop 1
dl 0
loc 29
ccs 12
cts 14
cp 0.8571
crap 8.1867
rs 8.4444
c 0
b 0
f 0
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