PageViewValidation   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 30%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 37
ccs 3
cts 10
cp 0.3
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 4 2
A isAllowed() 0 6 2
1
<?php
2
/**
3
 * @author Philip Bergman <[email protected]>
4
 * @copyright Zicht online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\PageBundle\Security;
7
8
use Zicht\Bundle\PageBundle\Model\PageInterface;
9
use Zicht\Bundle\PageBundle\Model\ViewValidationInterface;
10
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
11
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
12
13
/**
14
 * Class PageViewValidation
15
 *
16
 * @package Zicht\Bundle\PageBundle\Security
17
 */
18
class PageViewValidation implements ViewValidationInterface
19
{
20
    /** @var null|AuthorizationCheckerInterface */
21
    protected $auth;
22
23
    /**
24
     * PageViewValidation constructor.
25
     *
26
     * @param AuthorizationCheckerInterface|null $auth
27
     */
28 3
    public function __construct(AuthorizationCheckerInterface $auth = null)
29
    {
30 3
        $this->auth = $auth;
31 3
    }
32
33
    /**
34
     * Similar to PageController it will return true when auth === null
35
     *
36
     * @param PageInterface $page
37
     * @return bool
38
     */
39
    protected function isAllowed(PageInterface $page)
40
    {
41
        if (null !== $this->auth) {
42
            return $this->auth->isGranted('VIEW', $page);
43
        }
44
        return true;
45
    }
46
47
48
    /**
49
     * @{inheritDoc}
50
     */
51
    public function validate(PageInterface $page)
52
    {
53
        if (false === $this->isAllowed($page)) {
54
            throw new AccessDeniedException("Page {$page->getId()} is not accessible to the current user");
55
        }
56
    }
57
}
58