Test Setup Failed
Push — release/2.5.x ( 561eb6...9a29e9 )
by
unknown
06:54 queued 03:10
created

ScheduledContentVoter   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 87
ccs 28
cts 29
cp 0.9655
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C decide() 0 42 14
A supportsClass() 0 4 1
B vote() 0 18 5
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
        $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
     * Checks if the voter supports the given class.
73 7
     *
74
     * @param string $class A class name
75 7
     *
76
     * @return Boolean true if this Voter can process the class
77
     */
78
    public function supportsClass($class)
79
    {
80
        return in_array('Zicht\Bundle\PageBundle\Model\ScheduledContentInterface', class_implements($class));
81 7
    }
82
83
    /**
84 7
     * @{inheritDoc}
85
     */
86
    public function vote(TokenInterface $token, $object, array $attributes)
87 7
    {
88 6
        // Abstract class checks if user is admin, if not so it will return VoterInterface::ACCESS_ABSTAIN
89 6
        $vote = parent::vote($token, $object, $attributes);
0 ignored issues
show
Bug introduced by
It seems like $object defined by parameter $object on line 86 can also be of type null; however, Zicht\Bundle\PageBundle\...AdminAwareVoter::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...
90
91
        /** @var ScheduledContentInterface $object */
92
        if ($vote === VoterInterface::ACCESS_ABSTAIN && $this->supportsClass(get_class($object))) {
93 6
            foreach ($attributes as $attribute) {
94 6
                if (!$this->supportsAttribute($attribute)) {
95 6
                    continue;
96
                }
97 7
98
                $vote = self::decide($object, $attributes);
99
            }
100
        }
101
102
        return $vote;
103
    }
104
}
105