Completed
Pull Request — master (#90)
by Arnaud
12:13 queued 03:07
created

AdminVoter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Security\Voter;
4
5
use LAG\AdminBundle\Admin\Request\RequestHandler;
6
use Symfony\Component\Form\RequestHandlerInterface;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
12
class AdminVoter extends Voter
13
{
14
    /**
15
     * @var RequestHandler
16
     */
17
    private $requestHandler;
18
    
19
    /**
20
     * @var RequestStack
21
     */
22
    private $requestStack;
23
    
24
    /**
25
     * AdminVoter constructor.
26
     *
27
     * @param RequestHandlerInterface $requestHandler
28
     * @param RequestStack $requestStack
29
     */
30
    public function __construct(
31
        RequestHandlerInterface $requestHandler,
32
        RequestStack $requestStack
33
    ) {
34
        $this->requestHandler = $requestHandler;
0 ignored issues
show
Documentation Bug introduced by
It seems like $requestHandler of type object<Symfony\Component...equestHandlerInterface> is incompatible with the declared type object<LAG\AdminBundle\A...Request\RequestHandler> of property $requestHandler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
        $this->requestStack = $requestStack;
36
    }
37
    
38
    /**
39
     * @param string $attribute
40
     * @param mixed $subject
41
     *
42
     * @return bool
43
     */
44
    protected function supports($attribute, $subject)
45
    {
46
        if (!$subject instanceof UserInterface) {
47
            return false;
48
        }
49
        $request = $this
50
            ->requestStack
51
            ->getCurrentRequest()
52
        ;
53
    
54
        if (!$this->requestHandler->supports($request)) {
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getCurrentRequest() on line 49 can be null; however, LAG\AdminBundle\Admin\Re...uestHandler::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...
55
            return false;
56
        }
57
        
58
        return true;
59
    }
60
    
61
    /**
62
     * @param string $attribute
63
     * @param mixed $subject
64
     * @param TokenInterface $token
65
     *
66
     * @return bool
67
     */
68
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
69
    {
70
        $request = $this
71
            ->requestStack
72
            ->getCurrentRequest()
73
        ;
74
        $admin = $this
75
            ->requestHandler
76
            ->handle($request)
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getCurrentRequest() on line 70 can be null; however, LAG\AdminBundle\Admin\Re...equestHandler::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...
77
        ;
78
        $roles = $admin
79
            ->getConfiguration()
80
            ->getParameter('permissions')
81
        ;
82
    
83
        if (!in_array($attribute, $roles)) {
84
            return false;
85
        }
86
        
87
        return true;
88
    }
89
}
90