Completed
Push — master ( 2840be...7a1aba )
by Leny
19:07 queued 09:47
created

BusinessEntityOwnerVoter::vote()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %
Metric Value
dl 17
loc 17
rs 8.2222
cc 7
eloc 10
nc 4
nop 3
1
<?php
2
3
namespace Victoire\Bundle\BusinessEntityBundle\Security\Voter;
4
5
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
7
use Victoire\Bundle\BusinessEntityBundle\Helper\BusinessEntityHelper;
8
9
/**
10
 * This class decides yes or no if the user is granted to do some action on a given entity.
11
 */
12
class BusinessEntityOwnerVoter extends Voter
13
{
14
    private $userClass;
15
    private $businessEntityHelper;
16
17
    /**
18
     * @param $userClass
19
     * @param BusinessEntityHelper $businessEntityHelper
20
     */
21
    public function __construct($userClass, BusinessEntityHelper $businessEntityHelper)
22
    {
23
        $this->userClass = $userClass;
24
        $this->businessEntityHelper = $businessEntityHelper;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function supports($attribute, $subject)
31
    {
32
        return method_exists($subject, 'getAuthor')
33
            && $this->businessEntityHelper->findByEntityInstance($subject)
34
            && 'BUSINESS_ENTITY_OWNER' === $attribute;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 View Code Duplication
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        return $token->getUser() instanceof $this->userClass
43
            && ($token->getUser()->hasRole('ROLE_VICTOIRE')
44
                || $token->getUser()->hasRole('ROLE_VICTOIRE_DEVELOPER')
45
                || $subject->getAuthor() === $token->getUser()
46
            );
47
    }
48
}
49