Passed
Pull Request — release/3.x (#24)
by
unknown
07:12
created

ScheduledContentVoter::decide()   C

Complexity

Conditions 14
Paths 9

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 16.1502

Importance

Changes 0
Metric Value
cc 14
eloc 32
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 41
ccs 28
cts 36
cp 0.7778
crap 16.1502
rs 5.0864

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 AbstractAdminAwareVoter
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 6
        $from = $object->isScheduledFrom();
33 6
        $till = $object->isScheduledTill();
34
35 6
        if (!$object->isPublic() || false === self::notEmpty($from, $till))  {
36 1
            return $vote;
37
        }
38
39 5
        switch (true) {
40 5
            case is_null($from):
41 1
                $vote = $till >= $now ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED;
42 1
                break;
43 4
            case is_null($till):
44 1
                switch (true) {
45 1
                    case ($from <= $now):
46 1
                        $vote = VoterInterface::ACCESS_GRANTED;
47 1
                        break;
48
                    case ($from > $now && self::hasCmsAttribute($attributes)):
49
                        $vote = VoterInterface::ACCESS_GRANTED;
50
                        break;
51
                    default:
52
                        $vote = VoterInterface::ACCESS_DENIED;
53
                }
54 1
                break;
55 3
            default:
56 3
                switch (true) {
57 3
                    case ($from <= $now && $till >= $now):
58 1
                        $vote = VoterInterface::ACCESS_GRANTED;
59 1
                        break;
60 2
                    case (($from > $now && $till >= $now) && self::hasCmsAttribute($attributes)):
61
                        $vote = VoterInterface::ACCESS_GRANTED;
62
                        break;
63 2
                    default:
64 2
                        $vote = VoterInterface::ACCESS_DENIED;
65 2
                }
66 3
        }
67
68 5
        return $vote;
69
    }
70
71
    /**
72
     * Checks if the voter supports the given class.
73
     *
74
     * @param string $class A class name
75
     *
76
     * @return Boolean true if this Voter can process the class
77
     */
78 7
    public function supportsClass($class)
79
    {
80 7
        return in_array('Zicht\Bundle\PageBundle\Model\ScheduledContentInterface', class_implements($class));
81
    }
82
83
    /**
84
     * @{inheritDoc}
85
     */
86 7
    public function vote(TokenInterface $token, $object, array $attributes)
87
    {
88 7
        if (null === $object) {
89
            return VoterInterface::ACCESS_ABSTAIN;
90
        }
91
92
        // Abstract class checks if user is admin, if not so it will return VoterInterface::ACCESS_ABSTAIN
93 7
        $vote = parent::vote($token, $object, $attributes);
94
95
        /** @var ScheduledContentInterface $object */
96 7
        if ($vote === VoterInterface::ACCESS_ABSTAIN && $this->supportsClass(get_class($object))) {
97 6
            foreach ($attributes as $attribute) {
98 6
                if (!$this->supportsAttribute($attribute)) {
99
                    continue;
100
                }
101
102 6
                $vote = self::decide($object, $attributes);
103 6
            }
104 6
        }
105
106 7
        return $vote;
107
    }
108
}
109