Passed
Branch master (4f08c0)
by Takashi
07:18 queued 05:07
created

AccessRestrictor::isPublic()   D

Complexity

Conditions 16
Paths 432

Size

Total Lines 57
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 27
nc 432
nop 2
dl 0
loc 57
rs 4.6792
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function __construct(array $publicCategories = [], array $publicTags = [], array $privateCategories = [], array $privateTags = [])
28
    {
29
        $this->publicCategories = $publicCategories;
30
        $this->publicTags = $publicTags;
31
        $this->privateCategories = $privateCategories;
32
        $this->privateTags = $privateTags;
33
    }
34
35
    /**
36
     * @param string $category
37
     * @param array $tags
38
     * @return bool
39
     */
40
    public function isPublic($category, array $tags)
41
    {
42
        $isPublic = false;
43
44
        // publish if no public categories are specified.
45
        if (empty($this->publicCategories)) {
46
            $isPublic = true;
47
        }
48
49
        // publish if no public tags are specified.
50
        if (empty($this->publicTags)) {
51
            $isPublic = true;
52
        }
53
54
        // publish if category is under one of public categories.
55
        if (!$isPublic) {
56
            foreach ($this->publicCategories as $publicCategory) {
57
                if (preg_match(sprintf('#^%s#', $publicCategory), $category)) {
58
                    $isPublic = true;
59
                    break;
60
                }
61
            }
62
        }
63
64
        // publish if at least one tag is one of public tags.
65
        if (!$isPublic) {
66
            foreach ($this->publicTags as $publicTag) {
67
                foreach ($tags as $tag) {
68
                    if (preg_match(sprintf('/%s/', $publicTag), $tag)) {
69
                        $isPublic = true;
70
                        break 2;
71
                    }
72
                }
73
            }
74
        }
75
76
        // unpublish if category is under one of private categories.
77
        foreach ($this->privateCategories as $privateCategory) {
78
            if (preg_match(sprintf('#^%s#', $privateCategory), $category)) {
79
                $isPublic = false;
80
                break;
81
            }
82
        }
83
84
        // unpublish if at least one tag is one of private tags.
85
        if ($isPublic) {
86
            foreach ($this->privateTags as $privateTag) {
87
                foreach ($tags as $tag) {
88
                    if (preg_match(sprintf('/%s/', $privateTag), $tag)) {
89
                        $isPublic = false;
90
                        break 2;
91
                    }
92
                }
93
            }
94
        }
95
96
        return $isPublic;
97
    }
98
99
    /**
100
     * @param array $publicCategories
101
     * @return $this
102
     */
103
    public function setPublicCategories($publicCategories)
104
    {
105
        $this->publicCategories = $publicCategories;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param array $publicTags
112
     * @return $this
113
     */
114
    public function setPublicTags($publicTags)
115
    {
116
        $this->publicTags = $publicTags;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @param array $privateCategories
123
     * @return $this
124
     */
125
    public function setPrivateCategories($privateCategories)
126
    {
127
        $this->privateCategories = $privateCategories;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param array $privateTags
134
     * @return $this
135
     */
136
    public function setPrivateTags($privateTags)
137
    {
138
        $this->privateTags = $privateTags;
139
140
        return $this;
141
    }
142
}
143