IsOwnerVoter::vote()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 12
nc 6
nop 3
1
<?php
2
3
namespace Knp\RadBundle\Security\Voter;
4
5
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
6
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
7
use Symfony\Component\Security\Core\User\EquatableInterface;
8
use Knp\RadBundle\Security\OwnerInterface;
9
use Knp\RadBundle\Security\OwnableInterface;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
12
class IsOwnerVoter implements VoterInterface
13
{
14
    const IS_OWNER = 'IS_OWNER';
15
16
    public function supportsAttribute($attribute)
17
    {
18
        return self::IS_OWNER === $attribute;
19
    }
20
21
    public function supportsClass($class)
22
    {
23
        if (is_object($class)) {
24
            $refl = new \ReflectionObject($class);
25
26
            return $refl->implementsInterface('Knp\RadBundle\Security\OwnableInterface');
27
        }
28
29
        return false;
30
    }
31
32
    public function vote(TokenInterface $token, $object, array $attributes)
33
    {
34
        foreach ($attributes as $attribute) {
35
            if (!$this->supportsAttribute($attribute)) {
36
                continue;
37
            }
38
39
            if (!$this->supportsClass($object)) {
0 ignored issues
show
Documentation introduced by
$object is of type object|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
                return self::ACCESS_ABSTAIN;
41
            }
42
43
            if (!$token->getUser() instanceof OwnerInterface) {
44
                return self::ACCESS_ABSTAIN;
45
            }
46
47
            if ($this->isOwner($token->getUser(), $object)) {
0 ignored issues
show
Documentation introduced by
$object is of type object|null, but the function expects a object<Knp\RadBundle\Security\OwnableInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
                return self::ACCESS_GRANTED;
49
            }
50
51
            return self::ACCESS_DENIED;
52
        }
53
54
        return self::ACCESS_ABSTAIN;
55
    }
56
57
    private function isOwner(OwnerInterface $owner, OwnableInterface $ownable)
58
    {
59
        if ($ownable->getOwner() instanceof UserInterface && $owner instanceof EquatableInterface) {
60
            return $owner->isEqualTo($ownable->getOwner());
61
        }
62
63
        return $ownable->getOwner() === $owner;
64
    }
65
}
66