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

BusinessEntityOwnerVoter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 21.62 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 3
dl 8
loc 37
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A supports() 0 6 3
A voteOnAttribute() 8 8 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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