Completed
Pull Request — master (#46)
by Emanuele
06:31
created

FeatureSecurity   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 49
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B isGranted() 0 29 9
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 (empty($feature->getRole()) && empty($feature->getParentRole())) {
38 8
            return $feature->isEnabled();
39
        }
40
41 4
        if (null === $this->context) {
42
            return false;
43
        }
44
45 4
        if (!$feature->isEnabled()) {
46
            return false;
47
        }
48
49 4
        if ($feature->getRole()) {
50 4
            if (!$this->context->isGranted($feature->getRole())) {
51
                return false;
52
            }
53
        }
54
55 4
        if ('' !== trim($feature->getParentRole())) {
56 2
            if (!$this->context->isGranted($feature->getParentRole())) {
57 2
                return false;
58
            }
59
        }
60
61 2
        return true;
62
    }
63
}
64