Completed
Pull Request — master (#47)
by Emanuele
06:35
created

Feature::isGrantedForUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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