Test Setup Failed
Push — release/2.5.x ( bedc9f...03b23e )
by
unknown
02:25
created

ScheduledContentVoter::hasCmsAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 1
cts 1
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
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
        $valid  = false;
31 6
        $valid |= (null === ($from = $object->isScheduledFrom()));
32
        $valid |= (null === ($till = $object->isScheduledTill()));
33 6
        $now = new \DateTimeImmutable();
34
        $vote = VoterInterface::ACCESS_ABSTAIN;
35 6
36 5
        if (!$object->isPublic() || false === (bool)$valid) {
37 5
            return $vote;
38
        }
39 3
40 5
        switch (true) {
41
            case is_null($from):
42 1
                $vote = $till >= $now ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED;
43 2
                break;
44
            case is_null($till):
45 1
                switch (true) {
46 1
                    case ($from <= $now):
47 5
                        $vote = VoterInterface::ACCESS_GRANTED;
48
                        break;
49 6
                    case ($from > $now && self::hasCmsAttribute($attributes)):
50
                        $vote = VoterInterface::ACCESS_GRANTED;
51
                        break;
52
                    default:
53
                        $vote = VoterInterface::ACCESS_DENIED;
54
                }
55
                break;
56
            default:
57
                switch (true) {
58
                    case ($from <= $now && $till >= $now):
59
                        $vote = VoterInterface::ACCESS_GRANTED;
60
                        break;
61 6
                    case (($from > $now && $till >= $now) && self::hasCmsAttribute($attributes)):
62
                        $vote = VoterInterface::ACCESS_GRANTED;
63 6
                        break;
64
                    default:
65
                        $vote = VoterInterface::ACCESS_DENIED;
66
                }
67
        }
68
69
        return $vote;
70
    }
71
72
    /**
73 7
     * Check if the given attributes contain cms roles/attributes
74
     *
75 7
     * @param array $attributes
76
     * @return bool
77
     */
78
    protected static function hasCmsAttribute(array $attributes = [])
79
    {
80
        return (in_array('ACTION_POST_UPDATE', $attributes) || in_array('ACTION_POST_PERSIST', $attributes));
81 7
    }
82
83
    /**
84 7
     * @{inheritDoc}
85
     */
86
    public function supportsAttribute($attribute)
87 7
    {
88 6
        return in_array($attribute, array('VIEW', 'ACTION_POST_UPDATE', 'ACTION_POST_PERSIST'));
89 6
    }
90
91
    /**
92
     * Checks if the voter supports the given class.
93 6
     *
94 6
     * @param string $class A class name
95 6
     *
96
     * @return Boolean true if this Voter can process the class
97 7
     */
98
    public function supportsClass($class)
99
    {
100
        return in_array('Zicht\Bundle\PageBundle\Model\ScheduledContentInterface', class_implements($class));
101
    }
102
103
    /**
104
     * @{inheritDoc}
105
     */
106
    public function vote(TokenInterface $token, $object, array $attributes)
107
    {
108
        // Abstract class checks if user is admin, if not so it will return VoterInterface::ACCESS_ABSTAIN
109
        $vote = parent::vote($token, $object, $attributes);
0 ignored issues
show
Bug introduced by
It seems like $object defined by parameter $object on line 106 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...
110
111
        /** @var ScheduledContentInterface $object */
112
        if ($vote === VoterInterface::ACCESS_ABSTAIN && $this->supportsClass(get_class($object))) {
113
            foreach ($attributes as $attribute) {
114
                if (!$this->supportsAttribute($attribute)) {
115
                    continue;
116
                }
117
118
                $vote = self::decide($object, $attributes);
119
            }
120
        }
121
122
        return $vote;
123
    }
124
}
125