1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Service; |
6
|
|
|
|
7
|
|
|
class AccessController |
8
|
|
|
{ |
9
|
|
|
public function __construct( |
10
|
|
|
private array $publicCategories, |
11
|
|
|
private array $publicTags, |
12
|
|
|
private array $privateCategories, |
13
|
|
|
private array $privateTags, |
14
|
|
|
) { |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function isPublic(?string $category, array $tags): bool |
18
|
|
|
{ |
19
|
|
|
if ($this->matchesPrivateConditions($category, $tags)) { |
20
|
|
|
return false; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return empty($this->publicCategories) || $this->matchesPublicConditions($category, $tags); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function matchesPublicConditions(string $category, array $tags): bool |
27
|
|
|
{ |
28
|
|
|
return $this->categoryIsUnderOneOf($category, $this->publicCategories) || $this->atLeastOneTagIsIn($tags, $this->publicTags); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function matchesPrivateConditions(?string $category, array $tags): bool |
32
|
|
|
{ |
33
|
|
|
return $this->categoryIsUnderOneOf($category, $this->privateCategories) || $this->atLeastOneTagIsIn($tags, $this->privateTags); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function categoryIsUnderOneOf(?string $needle, array $haystacks): bool |
37
|
|
|
{ |
38
|
|
|
foreach ($haystacks as $haystack) { |
39
|
|
|
if (preg_match(sprintf('#^%s#', $haystack), $needle)) { |
|
|
|
|
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function atLeastOneTagIsIn(array $needles, array $haystacks): bool |
48
|
|
|
{ |
49
|
|
|
foreach ($haystacks as $haystack) { |
50
|
|
|
foreach ($needles as $needle) { |
51
|
|
|
if ($needle === $haystack) { |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|