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

SecurityMiddleware::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

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