Passed
Push — master ( 3408ae...8f5960 )
by Emanuele
37s queued 10s
created

Feature::isGrantedForUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ae\FeatureBundle\Service;
4
5
use Ae\FeatureBundle\Entity\Feature as FeatureEntity;
6
use Ae\FeatureBundle\Entity\FeatureManager;
7
use Ae\FeatureBundle\Security\FeatureSecurity;
8
use Exception;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
11
/**
12
 * @author Carlo Forghieri <[email protected]>
13
 */
14
class Feature implements FeatureInterface
15
{
16
    /**
17
     * @var FeatureManager
18
     */
19
    protected $manager;
20
21
    /**
22
     * @var FeatureSecurity
23
     */
24
    protected $security;
25
26
    /**
27
     * @param FeatureManager  $manager
28
     * @param FeatureSecurity $security
29
     */
30 5
    public function __construct(
31
        FeatureManager $manager,
32
        FeatureSecurity $security
33
    ) {
34 5
        $this->manager = $manager;
35 5
        $this->security = $security;
36 5
    }
37
38
    /**
39
     * Check if a feature (defined by name/parent) is granted to the logged user.
40
     *
41
     * @param string $name
42
     * @param string $parent
43
     *
44
     * @return bool
45
     */
46 2
    public function isGranted($name, $parent)
47
    {
48
        try {
49 2
            return $this->security->isGranted(
50 2
                $this->manager->find($name, $parent)
51
            );
52
        } catch (Exception $exception) {
53
            return false;
54
        }
55
    }
56
57 3
    public function isGrantedForUser(string $name, string $parent, UserInterface $user): bool
58
    {
59 3
        $feature = $this->manager->find($name, $parent);
60
61 3
        if (!$feature instanceof FeatureEntity) {
0 ignored issues
show
introduced by
$feature is always a sub-type of Ae\FeatureBundle\Entity\Feature.
Loading history...
62 1
            return false;
63
        }
64
65 2
        return $this->security->isGrantedForUser($feature, $user);
66
    }
67
}
68