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

PageOwnerVoter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 25.71 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 7
lcom 1
cbo 2
dl 9
loc 35
rs 10
c 3
b 1
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 4 2
A voteOnAttribute() 9 9 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\PageBundle\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\PageBundle\Entity\Page;
8
9
/**
10
 * This class decides yes or no if the user is granted to do some action on a given page.
11
 */
12
class PageOwnerVoter extends Voter
13
{
14
    private $userClass;
15
16
    /**
17
     * PageOwnerVoter constructor.
18
     *
19
     * @param $userClass
20
     */
21
    public function __construct($userClass)
22
    {
23
        $this->userClass = $userClass;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function supports($attribute, $subject)
30
    {
31
        return $subject instanceof Page && 'PAGE_OWNER' === $attribute;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 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...
38
    {
39
        return $token->getUser() instanceof $this->userClass
40
            && (
41
                $token->getUser()->hasRole('ROLE_VICTOIRE')
42
             || $token->getUser()->hasRole('ROLE_VICTOIRE_DEVELOPER')
43
             || $subject->getAuthor() === $token->getUser()
44
            );
45
    }
46
}
47