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

FeatureSecurity   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 45
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C isGranted() 0 24 7
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