BasicAclEntry   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 50
ccs 18
cts 18
cp 1
rs 10
c 2
b 0
f 0
wmc 9

4 Methods

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