PageViewValidation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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