Feature::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
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 5
    public function __construct(
27
        FeatureManager $manager,
28
        FeatureSecurity $security
29
    ) {
30 5
        $this->manager = $manager;
31 5
        $this->security = $security;
32 5
    }
33
34
    /**
35
     * Check if a feature (defined by name/parent) is granted to the logged user.
36
     *
37
     * @param string $name
38
     * @param string $parent
39
     *
40
     * @return bool
41
     */
42 2
    public function isGranted($name, $parent)
43
    {
44
        try {
45 2
            return $this->security->isGranted(
46 2
                $this->manager->find($name, $parent)
47
            );
48
        } catch (Exception $exception) {
49
            return false;
50
        }
51
    }
52
53 3
    public function isGrantedForUser(string $name, string $parent, UserInterface $user): bool
54
    {
55 3
        $feature = $this->manager->find($name, $parent);
56
57 3
        if (!$feature instanceof FeatureEntity) {
0 ignored issues
show
introduced by
$feature is always a sub-type of Ae\FeatureBundle\Entity\Feature.
Loading history...
58 1
            return false;
59
        }
60
61 2
        return $this->security->isGrantedForUser($feature, $user);
62
    }
63
}
64