BasicPermission   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 34
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A matchesAction() 0 6 2
A matchesResource() 0 8 3
1
<?php declare(strict_types=1);
2
3
namespace jschreuder\MiddleAuth\Rbac;
4
5
use jschreuder\MiddleAuth\AuthorizationEntityInterface;
6
7
final class BasicPermission implements PermissionInterface
8
{
9 8
    public function __construct(
10
        private string $resourceMatcher,
11
        private string $actionMatcher
12
    )
13
    {
14 8
    }
15
16
    /**
17
     * Can match in 3 ways: a single '*' matches everything, ending on '::*'
18
     * means it will only have to match the type, otherwise it needs to be a
19
     * full match.
20
     */
21 5
    public function matchesResource(AuthorizationEntityInterface $resource): bool
22
    {
23 5
        if ($this->resourceMatcher === '*') {
24 1
            return true;
25 4
        } elseif (substr($this->resourceMatcher, -3, 3) === '::*') {
26 2
            return $resource->getType() === substr($this->resourceMatcher, 0, -3);
27
        }
28 2
        return ($resource->getType() . '::' . $resource->getId()) === $this->resourceMatcher;
29
    }
30
31
    /**
32
     * Can match in 2 ways: either a single '*' matches everything, or it needs
33
     * to be a full match.
34
     */
35 4
    public function matchesAction(string $action): bool
36
    {
37 4
        if ($this->actionMatcher === '*') {
38 1
            return true;
39
        }
40 3
        return $action === $this->actionMatcher;
41
    }
42
}
43