Completed
Pull Request — master (#90)
by Arnaud
02:00
created

AdminVoter::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 12
1
<?php
2
3
namespace LAG\AdminBundle\Security\Voter;
4
5
use LAG\AdminBundle\Admin\Request\RequestHandlerInterface;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
11
class AdminVoter extends Voter
12
{
13
    /**
14
     * @var RequestHandlerInterface
15
     */
16
    private $requestHandler;
17
    
18
    /**
19
     * @var RequestStack
20
     */
21
    private $requestStack;
22
    
23
    /**
24
     * AdminVoter constructor.
25
     *
26
     * @param RequestHandlerInterface $requestHandler
27
     * @param RequestStack $requestStack
28
     */
29
    public function __construct(
30
        RequestHandlerInterface $requestHandler,
31
        RequestStack $requestStack
32
    ) {
33
        $this->requestHandler = $requestHandler;
34
        $this->requestStack = $requestStack;
35
    }
36
    
37
    /**
38
     * @param string $attribute
39
     * @param mixed $subject
40
     *
41
     * @return bool
42
     */
43
    protected function supports($attribute, $subject)
44
    {
45
        if (!$subject instanceof UserInterface) {
46
            return false;
47
        }
48
        $request = $this
49
            ->requestStack
50
            ->getCurrentRequest()
51
        ;
52
    
53
        if (!$this->requestHandler->supports($request)) {
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getCurrentRequest() on line 48 can be null; however, LAG\AdminBundle\Admin\Re...erInterface::supports() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
54
            return false;
55
        }
56
        
57
        return true;
58
    }
59
    
60
    /**
61
     * @param string $attribute
62
     * @param mixed $subject
63
     * @param TokenInterface $token
64
     *
65
     * @return bool
66
     */
67
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
68
    {
69
        $request = $this
70
            ->requestStack
71
            ->getCurrentRequest()
72
        ;
73
        $admin = $this
74
            ->requestHandler
75
            ->handle($request)
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getCurrentRequest() on line 69 can be null; however, LAG\AdminBundle\Admin\Re...dlerInterface::handle() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
76
        ;
77
        $roles = $admin
78
            ->getConfiguration()
79
            ->getParameter('permissions')
80
        ;
81
    
82
        if (!in_array($attribute, $roles)) {
83
            return false;
84
        }
85
        
86
        return true;
87
    }
88
}
89