Passed
Push — master ( e7ebae...ecc5ac )
by Valentin
04:51 queued 11s
created

GuardInterceptor::makePermission()   B

Complexity

Conditions 9
Paths 21

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 28
nc 21
nop 3
dl 0
loc 43
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Domain;
13
14
use Spiral\Core\CoreInterceptorInterface;
15
use Spiral\Core\CoreInterface;
16
use Spiral\Core\Exception\ControllerException;
17
use Spiral\Security\GuardInterface;
18
19
/**
20
 * Interceptor provides the ability to check the access to the controllers and controller methods using security
21
 * component and annotations "Guarded" and "GuardNamespace".
22
 */
23
final class GuardInterceptor implements CoreInterceptorInterface
24
{
25
    /** @var GuardInterface */
26
    private $guard;
27
28
    /** @var PermissionsProviderInterface */
29
    private $permissions;
30
31
    public function __construct(GuardInterface $guard, PermissionsProviderInterface $permissions)
32
    {
33
        $this->guard = $guard;
34
        $this->permissions = $permissions;
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function process(string $controller, string $action, array $parameters, CoreInterface $core)
41
    {
42
        $permission = $this->permissions->getPermission($controller, $action);
43
44
        if ($permission !== null && !$this->guard->allows($permission[0], $parameters)) {
45
            throw new ControllerException($permission[2], $permission[1]);
46
        }
47
48
        return $core->callAction($controller, $action, $parameters);
49
    }
50
}
51