AccessController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 21
c 3
b 2
f 0
dl 0
loc 63
ccs 26
cts 26
cp 1
rs 10
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isPublic() 0 7 2
A matchesPublicConditions() 0 15 5
A matchesPrivateConditions() 0 3 2
A categoryIsUnderOneOf() 0 9 3
A atLeastOneTagIsIn() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service;
6
7
class AccessController
8
{
9 25
    public function __construct(
10
        private ?array $publicCategories,
11
        private ?array $publicTags,
12
        private ?array $privateCategories,
13
        private ?array $privateTags,
14
    ) {
15
    }
16
17 11
    public function isPublic(?string $category, array $tags): bool
18
    {
19 11
        if ($this->matchesPrivateConditions($category, $tags)) {
20 8
            return false;
21
        }
22
23 5
        return $this->matchesPublicConditions($category, $tags);
24
    }
25
26 6
    private function matchesPublicConditions(?string $category, array $tags): bool
27
    {
28 6
        if (empty($this->publicCategories) && empty($this->publicTags)) {
29 2
            return true;
30
        }
31
32 5
        if ($this->categoryIsUnderOneOf($category, $this->publicCategories)) {
0 ignored issues
show
Bug introduced by
It seems like $this->publicCategories can also be of type null; however, parameter $haystacks of App\Service\AccessContro...:categoryIsUnderOneOf() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        if ($this->categoryIsUnderOneOf($category, /** @scrutinizer ignore-type */ $this->publicCategories)) {
Loading history...
33 3
            return true;
34
        }
35
36 4
        if ($this->atLeastOneTagIsIn($tags, $this->publicTags)) {
0 ignored issues
show
Bug introduced by
It seems like $this->publicTags can also be of type null; however, parameter $haystacks of App\Service\AccessController::atLeastOneTagIsIn() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        if ($this->atLeastOneTagIsIn($tags, /** @scrutinizer ignore-type */ $this->publicTags)) {
Loading history...
37 3
            return true;
38
        }
39
40 3
        return false;
41
    }
42
43 12
    private function matchesPrivateConditions(?string $category, array $tags): bool
44
    {
45 12
        return $this->categoryIsUnderOneOf($category, $this->privateCategories) || $this->atLeastOneTagIsIn($tags, $this->privateTags);
0 ignored issues
show
Bug introduced by
It seems like $this->privateTags can also be of type null; however, parameter $haystacks of App\Service\AccessController::atLeastOneTagIsIn() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        return $this->categoryIsUnderOneOf($category, $this->privateCategories) || $this->atLeastOneTagIsIn($tags, /** @scrutinizer ignore-type */ $this->privateTags);
Loading history...
Bug introduced by
It seems like $this->privateCategories can also be of type null; however, parameter $haystacks of App\Service\AccessContro...:categoryIsUnderOneOf() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        return $this->categoryIsUnderOneOf($category, /** @scrutinizer ignore-type */ $this->privateCategories) || $this->atLeastOneTagIsIn($tags, $this->privateTags);
Loading history...
46
    }
47
48 20
    private function categoryIsUnderOneOf(?string $needle, array $haystacks): bool
49
    {
50 20
        foreach ($haystacks as $haystack) {
51 19
            if (preg_match(sprintf('#^%s#', $haystack), (string) $needle)) {
52 12
                return true;
53
            }
54
        }
55
56 12
        return false;
57
    }
58
59 14
    private function atLeastOneTagIsIn(array $needles, array $haystacks): bool
60
    {
61 14
        foreach ($haystacks as $haystack) {
62 14
            foreach ($needles as $needle) {
63 11
                if ($needle === $haystack) {
64 10
                    return true;
65
                }
66
            }
67
        }
68
69 9
        return false;
70
    }
71
}
72