BasicPermission::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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