PermissionService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 31
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A canEditNode() 0 4 1
A canViewNode() 0 4 1
A denyAccessUnlessGranted() 0 10 2
1
<?php
2
namespace AppBundle\Service;
3
4
use AppBundle\Entity\Node;
5
use AppBundle\Security\NodeVoter;
6
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
7
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
8
9
class PermissionService
10
{
11
    /** @var AuthorizationChecker */
12
    private $authorizationChecker;
13
14
    public function __construct(AuthorizationChecker $authorizationChecker)
15
    {
16
        $this->authorizationChecker = $authorizationChecker;
17
    }
18
19
    public function canEditNode(Node $node)
20
    {
21
        $this->denyAccessUnlessGranted(NodeVoter::EDIT, $node, 'you don\'t have permissions to edit this datanode');
22
    }
23
24
    public function canViewNode(Node $node)
25
    {
26
        $this->denyAccessUnlessGranted(NodeVoter::VIEW, $node, 'you don\'t have permissions to view this datanode');
27
    }
28
29
    protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
30
    {
31
        if (!$this->authorizationChecker->isGranted($attributes, $object)) {
32
            $exception = new AccessDeniedException($message);
33
            $exception->setAttributes($attributes);
34
            $exception->setSubject($object);
35
36
            throw $exception;
37
        }
38
    }
39
}
40