Passed
Push — master ( 04e4ef...0bbdaa )
by Takashi
02:31
created

AccessRestrictor::isPublic()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Ttskch;
4
5
class AccessRestrictor
6
{
7
    /**
8
     * @var array
9
     */
10
    private $publicCategories;
11
12
    /**
13
     * @var array
14
     */
15
    private $publicTags;
16
17
    /**
18
     * @var array
19
     */
20
    private $privateCategories;
21
22
    /**
23
     * @var array
24
     */
25
    private $privateTags;
26
27 25
    public function __construct(array $publicCategories = [], array $publicTags = [], array $privateCategories = [], array $privateTags = [])
28
    {
29 25
        $this->publicCategories = $publicCategories;
30 25
        $this->publicTags = $publicTags;
31 25
        $this->privateCategories = $privateCategories;
32 25
        $this->privateTags = $privateTags;
33 25
    }
34
35
    /**
36
     * @param string $category
37
     * @param array $tags
38
     * @return bool
39
     */
40 10
    public function isPublic($category, array $tags)
41
    {
42 10
        if ($this->isWithheld($category, $tags)) {
43 7
            return false;
44
        }
45
46 4
        return empty($this->publicCategories) || $this->isPublished($category, $tags);
47
    }
48
49
    /**
50
     * @param string $category
51
     * @param array $tags
52
     * @return bool
53
     */
54 5
    public function isPublished($category, array $tags)
55
    {
56 5
        return $this->categoryIsUnderOneOf($category, $this->publicCategories) || $this->atLeastOneTagIsIn($tags, $this->publicTags);
57
    }
58
59
    /**
60
     * @param string $category
61
     * @param array $tags
62
     * @return bool
63
     */
64 11
    public function isWithheld($category, array $tags)
65
    {
66 11
        return $this->categoryIsUnderOneOf($category, $this->privateCategories) || $this->atLeastOneTagIsIn($tags, $this->privateTags);
67
    }
68
69
    /**
70
     * @param string $needle
71
     * @param array $haystacks
72
     * @return bool
73
     */
74 19
    public function categoryIsUnderOneOf($needle, array $haystacks)
75
    {
76 19
        foreach ($haystacks as $haystack) {
77 19
            if (preg_match(sprintf('#^%s#', $haystack), $needle)) {
78 11
                return true;
79
            }
80 13
        }
81
82 11
        return false;
83
    }
84
85
    /**
86
     * @param array $needles
87
     * @param array $haystacks
88
     * @return bool
89
     */
90 13
    public function atLeastOneTagIsIn(array $needles, array $haystacks)
91
    {
92 13
        foreach ($haystacks as $haystack) {
93 13
            foreach ($needles as $needle) {
94 10
                if ($needle === $haystack) {
95 9
                    return true;
96
                }
97 10
            }
98 9
        }
99
100 8
        return false;
101
    }
102
}
103