Completed
Push — master ( 3f17e5...ec676e )
by Ross
9s
created

SecurityMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 3
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 10 2
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
10
class SecurityMiddleware implements Middleware
11
{
12
    /**
13
     * @var AuthorizationCheckerInterface
14
     */
15
    private $authorizationChecker;
16
17
    /**
18
     * @param AuthorizationCheckerInterface $authorizationChecker
19
     */
20 6
    public function __construct(AuthorizationCheckerInterface $authorizationChecker) {
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 ($this->authorizationChecker->isGranted('handle', $command)) {
32 3
            return $next($command);
33
        } else {
34 3
            throw new AccessDeniedException(
35 3
                sprintf('The current user is not allowed to handle command of type \'%s\'', get_class($command))
36 3
            );
37
        }
38
    }
39
}
40
41