Completed
Pull Request — master (#32)
by Timothée
02:28
created

SecurityMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 38
ccs 10
cts 13
cp 0.7692
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 17 3
1
<?php
2
3
namespace League\Tactician\Bundle\Middleware;
4
5
use League\Tactician\Middleware;
6
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
7
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
8
9
class SecurityMiddleware implements Middleware
10
{
11
    /**
12
     * @var AuthorizationCheckerInterface
13
     */
14
    private $authorizationChecker;
15
16
    /**
17
     * @param AuthorizationCheckerInterface $authorizationChecker
18
     */
19 6
    public function __construct(AuthorizationCheckerInterface $authorizationChecker = null)
20
    {
21 6
        $this->authorizationChecker = $authorizationChecker;
22 6
    }
23
24
    /**
25
     * @param object $command
26
     * @param callable $next
27
     * @return mixed
28
     */
29 6
    public function execute($command, callable $next)
30
    {
31 6
        if (null === $this->authorizationChecker) {
32
            throw new \Exception(
33
                "The Security Middleware requires the authorization checker service (@security.authorization_checker) to be present and configured." .
34
                "Please active security extension in your config."
35
            );
36
        }
37
38 6
        if ($this->authorizationChecker->isGranted('handle', $command)) {
39 3
            return $next($command);
40
        } else {
41 3
            throw new AccessDeniedException(
42 3
                sprintf('The current user is not allowed to handle command of type \'%s\'', get_class($command))
43 2
            );
44
        }
45
    }
46
}
47
48