Completed
Push — master ( 32c2ac...90b7d3 )
by olivier
12s
created

TagVoter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 52.73 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 29
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 12 12 3
A voteOnAttribute() 17 17 3
A canView() 0 4 1

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 Badger\Bundle\GameBundle\Security;
4
5
use Badger\Component\Game\Model\TagInterface;
6
use Badger\Component\Game\Taggable\TaggableInterface;
7
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
9
10
/**
11
 * Voter for Tag entities.
12
 * Is able to tell if a User can view a given Tag.
13
 *
14
 * @author  Adrien Pétremann <[email protected]>
15
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
16
 */
17
class TagVoter extends Voter
18
{
19
    const VIEW = 'view';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 View Code Duplication
    protected function supports($attribute, $subject)
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...
25
    {
26
        if (!in_array($attribute, [self::VIEW])) {
27
            return false;
28
        }
29
30
        if (!$subject instanceof TagInterface) {
31
            return false;
32
        }
33
34
        return true;
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
        $user = $token->getUser();
43
44
        if (!$user instanceof TaggableInterface) {
45
            return false;
46
        }
47
48
        $tag = $subject;
49
50
        switch ($attribute) {
51
            case self::VIEW:
52
                return $this->canView($tag, $user);
53
        }
54
55
        throw new \LogicException('This code should not be reached!');
56
    }
57
58
    /**
59
     * Return true if the given $user can view the given $tag.
60
     * The $entity can be viewed by the $user if the user is linked to that $tag.
61
     *
62
     * @param TagInterface      $tag
63
     * @param TaggableInterface $user
64
     *
65
     * @return bool
66
     */
67
    private function canView(TagInterface $tag, TaggableInterface $user)
68
    {
69
        return in_array($tag, $user->getTags()->toArray());
70
    }
71
}
72