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

Feature   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 46.67%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 52
ccs 7
cts 15
cp 0.4667
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isGrantedForUser() 0 9 2
A isGranted() 0 8 2
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