Completed
Push — master ( 32f082...893399 )
by Dmitry
12:23
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
1
<?php
2
3
namespace hiapi\Core\Endpoint\Middleware;
4
5
use hiapi\Core\Endpoint\Endpoint;
6
use hiapi\exceptions\InsufficientPermissionsException;
7
use yii\web\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
    private User $user;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
18
    private Endpoint $endpoint;
19
20
    public function __construct(Endpoint $endpoint, User $user)
21
    {
22
        $this->endpoint = $endpoint;
23
        $this->user = $user;
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function execute($command, callable $next)
30
    {
31
        foreach ($this->endpoint->permissions as $permission) {
32
            if (! $this->user->can($permission)) {
33
                throw new InsufficientPermissionsException($permission);
34
            }
35
        }
36
37
        return $next($command);
38
    }
39
}
40