PageVoter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 42
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsClass() 0 3 2
A isPublic() 0 3 1
A vote() 0 17 5
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