AuthorizationAware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthorizationChecker() 0 4 1
A setAuthorizationChecker() 0 4 1
A isGranted() 0 4 1
A denyAccessUnlessGranted() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Controller;
6
7
use LogicException;
8
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
9
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
10
11
trait AuthorizationAware
12
{
13
    /**
14
     * @var AuthorizationCheckerInterface
15
     */
16
    protected $authorizationChecker;
17
18
    /**
19
     * @return AuthorizationCheckerInterface
20
     */
21
    public function getAuthorizationChecker()
22
    {
23
        return $this->authorizationChecker;
24
    }
25
26
    /**
27
     * @param AuthorizationCheckerInterface $authorizationChecker
28
     */
29
    public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker)
30
    {
31
        $this->authorizationChecker = $authorizationChecker;
32
    }
33
34
    /**
35
     * Checks if the attributes are granted against the current authentication token and optionally supplied object.
36
     *
37
     * @param mixed $attributes The attributes
38
     * @param mixed $object     The object
39
     *
40
     * @throws LogicException
41
     *
42
     * @return bool
43
     */
44
    protected function isGranted($attributes, $object = null)
45
    {
46
        return $this->authorizationChecker->isGranted($attributes, $object);
47
    }
48
49
    /**
50
     * Throws an exception unless the attributes are granted against the current authentication token and optionally
51
     * supplied object.
52
     *
53
     * @param mixed  $attributes The attributes
54
     * @param mixed  $object     The object
55
     * @param string $message    The message passed to the exception
56
     *
57
     * @throws AccessDeniedException
58
     */
59
    protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
60
    {
61
        if (!$this->isGranted($attributes, $object)) {
62
            throw new AccessDeniedException($message);
63
        }
64
    }
65
}
66