Test Setup Failed
Push — release/2.5.x ( 03b23e...561eb6 )
by
unknown
30:35
created

ScheduledContentVoter::decide()   C

Complexity

Conditions 14
Paths 9

Size

Total Lines 42
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 14

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
ccs 17
cts 17
cp 1
rs 5.0864
cc 14
eloc 33
nc 9
nop 2
crap 14

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Rik van der Kemp <[email protected]>
4
 * @copyright Zicht Online <http://www.zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\PageBundle\Security\Voter;
8
9
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
11
use Zicht\Bundle\PageBundle\Model\ScheduledContentInterface;
12
13
/**
14
 * Check content against the scheduled dates
15
 *
16
 * @package Zicht\Bundle\PageBundle\Security\Voter
17
 */
18
class ScheduledContentVoter extends AdminAwareVoterAbstract
19
{
20
    /**
21
     * Decide based on the current date and time what the vote should be. Static so it's strategy can easily be accessed
22
     * by other components as well, without the actual need for the voter instance.
23
     *
24
     * @param ScheduledContentInterface $object
25
     * @param array $attributes
26
     * @return int
27
     */
28 6
    public static function decide(ScheduledContentInterface $object, array $attributes = [])
29
    {
30 6
        $now = new \DateTimeImmutable();
31 6
        $vote = VoterInterface::ACCESS_ABSTAIN;
32
        $from = $object->isScheduledFrom();
33 6
        $till = $object->isScheduledTill();
34
35 6
        if (!$object->isPublic() || false === self::notEmpty($from, $till))  {
36 5
            return $vote;
37 5
        }
38
39 3
        switch (true) {
40 5
            case is_null($from):
41
                $vote = $till >= $now ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED;
42 1
                break;
43 2
            case is_null($till):
44
                switch (true) {
45 1
                    case ($from <= $now):
46 1
                        $vote = VoterInterface::ACCESS_GRANTED;
47 5
                        break;
48
                    case ($from > $now && self::hasCmsAttribute($attributes)):
49 6
                        $vote = VoterInterface::ACCESS_GRANTED;
50
                        break;
51
                    default:
52
                        $vote = VoterInterface::ACCESS_DENIED;
53
                }
54
                break;
55
            default:
56
                switch (true) {
57
                    case ($from <= $now && $till >= $now):
58
                        $vote = VoterInterface::ACCESS_GRANTED;
59
                        break;
60
                    case (($from > $now && $till >= $now) && self::hasCmsAttribute($attributes)):
61 6
                        $vote = VoterInterface::ACCESS_GRANTED;
62
                        break;
63 6
                    default:
64
                        $vote = VoterInterface::ACCESS_DENIED;
65
                }
66
        }
67
68
        return $vote;
69
    }
70
71
     /**
72
     * Check if one or more of the given items is not empty
73 7
     *
74
     * @param ...$value
75 7
     * @return bool
76
     */
77
    protected static function notEmpty(...$value)
78
    {
79
        return (bool)count(array_filter($value)) >= 1;
80
    }
81 7
    
82
    /**
83
     * Check if the given attributes contain cms roles/attributes
84 7
     *
85
     * @param array $attributes
86
     * @return bool
87 7
     */
88 6
    protected static function hasCmsAttribute(array $attributes = [])
89 6
    {
90
        return (in_array('ACTION_POST_UPDATE', $attributes) || in_array('ACTION_POST_PERSIST', $attributes));
91
    }
92
93 6
    /**
94 6
     * @{inheritDoc}
95 6
     */
96
    public function supportsAttribute($attribute)
97 7
    {
98
        return in_array($attribute, array('VIEW', 'ACTION_POST_UPDATE', 'ACTION_POST_PERSIST'));
99
    }
100
101
    /**
102
     * Checks if the voter supports the given class.
103
     *
104
     * @param string $class A class name
105
     *
106
     * @return Boolean true if this Voter can process the class
107
     */
108
    public function supportsClass($class)
109
    {
110
        return in_array('Zicht\Bundle\PageBundle\Model\ScheduledContentInterface', class_implements($class));
111
    }
112
113
    /**
114
     * @{inheritDoc}
115
     */
116
    public function vote(TokenInterface $token, $object, array $attributes)
117
    {
118
        // Abstract class checks if user is admin, if not so it will return VoterInterface::ACCESS_ABSTAIN
119
        $vote = parent::vote($token, $object, $attributes);
0 ignored issues
show
Bug introduced by
It seems like $object defined by parameter $object on line 116 can also be of type null; however, Zicht\Bundle\PageBundle\...reVoterAbstract::vote() does only seem to accept object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
120
121
        /** @var ScheduledContentInterface $object */
122
        if ($vote === VoterInterface::ACCESS_ABSTAIN && $this->supportsClass(get_class($object))) {
123
            foreach ($attributes as $attribute) {
124
                if (!$this->supportsAttribute($attribute)) {
125
                    continue;
126
                }
127
128
                $vote = self::decide($object, $attributes);
129
            }
130
        }
131
132
        return $vote;
133
    }
134
}
135