PageVoter::supportsClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * @copyright Zicht Online <http://www.zicht.nl>
4
 */
5
namespace Zicht\Bundle\PageBundle\Security\Voter;
6
7
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
9
use Zicht\Bundle\PageBundle\Entity\Page;
10
use Zicht\Bundle\PageBundle\Model\PageInterface;
11
12
/**
13
 * Votes for pages to be public for anyone.
14
 */
15
class PageVoter extends AbstractVoter
16
{
17
    /**
18
     * @{inheritDoc}
19
     */
20
    public function supportsClass($class)
21
    {
22
        return Page::class === $class || is_subclass_of($class, Page::class);
23
    }
24
25
    /**
26
     * @{inheritDoc}
27
     */
28
    public function vote(TokenInterface $token, $object, array $attributes)
29
    {
30
        // check if class of this object is supported by this voter
31
        if (!$this->supportsClass(get_class($object))) {
32
            return VoterInterface::ACCESS_ABSTAIN;
33
        }
34
35
        foreach ($attributes as $attribute) {
36
            if (!$this->supportsAttribute($attribute)) {
37
                continue;
38
            }
39
40
            if ($this->isPublic($object)) {
41
                return VoterInterface::ACCESS_GRANTED;
42
            }
43
        }
44
        return VoterInterface::ACCESS_ABSTAIN;
45
    }
46
47
48
    /**
49
     * Checks whether the given page is public. If not, it abstains from voting
50
     *
51
     * @param PageInterface $page
52
     * @return bool
53
     */
54
    public function isPublic(PageInterface $page)
55
    {
56
        return $page->isPublic();
57
    }
58
}
59