BasicAclEntry::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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