Completed
Push — master ( 9a342a...a162ae )
by Andrii
13:06
created

CheckPermissionsMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 10 3
1
<?php
2
3
namespace hiapi\Core\Endpoint\Middleware;
4
5
use hiapi\Core\Endpoint\Endpoint;
6
use hiapi\exceptions\InsufficientPermissionsException;
7
use hiapi\legacy\components\User;
8
use League\Tactician\Middleware;
9
10
/**
11
 * Class CheckPermissionsMiddleware
12
 *
13
 * @author Dmytro Naumenko <[email protected]>
14
 */
15
class CheckPermissionsMiddleware implements Middleware
16
{
17
    /**
18
     * @var User
19
     */
20
    private $user;
21
    /**
22
     * @var string[]
23
     */
24
    private $permissions;
25
26
    public function __construct(Endpoint $endpoint, User $user)
27
    {
28
        $this->permissions = $endpoint->getPermissions();
29
        $this->user = $user;
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function execute($command, callable $next)
36
    {
37
        foreach ($this->permissions as $permission) {
38
            if (! $this->user->can($permission)) {
39
                throw new InsufficientPermissionsException($permission);
40
            }
41
        }
42
43
        return $next($command);
44
    }
45
}
46