|
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)) { |
|
|
|
|
|
|
33
|
3 |
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
4 |
|
if ($this->atLeastOneTagIsIn($tags, $this->publicTags)) { |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|