AllowHeader   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 231
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A allowMethod() 0 6 1
A setState() 0 9 2
A setAllState() 0 6 2
A fromString() 0 12 2
A allowMethods() 0 8 2
A disallowMethods() 0 8 2
A disallowMethod() 0 6 1
A getAllowedMethods() 0 4 1
A getDisallowedMethods() 0 4 1
A isMethodAllowed() 0 8 2
A isMethodDisallowed() 0 4 1
A __toString() 0 4 1
A getFieldName() 0 4 1
A getFieldValue() 0 4 1
A getAllMethods() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http\Header;
12
13
use Borobudur\Http\Header\Exception\InvalidHeaderValueException;
14
use Borobudur\Http\Request;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     7/19/15
19
 */
20
class AllowHeader implements HeaderInterface
21
{
22
    /**
23
     * List of request methods, true state indicate that method is allowed
24
     * and false is disallowed.
25
     * By default GET and POST are allowed.
26
     *
27
     * @var array
28
     */
29
    private $methods = array(
30
        Request::HTTP_METHOD_GET     => true,
31
        Request::HTTP_METHOD_POST    => true,
32
        Request::HTTP_METHOD_PATCH   => false,
33
        Request::HTTP_METHOD_PUT     => false,
34
        Request::HTTP_METHOD_OPTIONS => false,
35
        Request::HTTP_METHOD_HEAD    => false,
36
    );
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param array $allowedMethods
42
     */
43
    public function __construct(array $allowedMethods = array())
44
    {
45
        if (!empty($allowedMethods)) {
46
            $this->setAllState(false);
47
            foreach ($allowedMethods as $method) {
48
                $this->allowMethod($method);
49
            }
50
        }
51
    }
52
53
    /**
54
     * Allow method.
55
     *
56
     * @param string $method
57
     *
58
     * @return $this
59
     */
60
    public function allowMethod($method)
61
    {
62
        $this->setState($method, true);
63
64
        return $this;
65
    }
66
67
    /**
68
     * Change method state.
69
     *
70
     * @param string $method
71
     * @param bool   $state
72
     *
73
     * @throws InvalidHeaderValueException
74
     */
75
    private function setState($method, $state)
76
    {
77
        $method = trim(strtoupper($method));
78
        if (false === array_key_exists($method, $this->methods)) {
79
            throw new InvalidHeaderValueException(sprintf('Method "%s" is not supported.', $method));
80
        }
81
82
        $this->methods[$method] = $state;
83
    }
84
85
    /**
86
     * Set all method state.
87
     *
88
     * @param bool $state
89
     */
90
    private function setAllState($state)
91
    {
92
        foreach (array_keys($this->methods) as $method) {
93
            $this->methods[$method] = $state;
94
        }
95
    }
96
97
    /**
98
     * Factory create AllowHeader from string representation.
99
     *
100
     * @param string $headerLine
101
     *
102
     * @return $this
103
     */
104
    public static function fromString($headerLine)
105
    {
106
        list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine);
107
        GenericHeader::assertHeaderFieldName('Allow', $fieldName);
108
109
        if ($fieldValue !== null) {
110
            $fieldValue = array_filter(explode(',', $fieldValue), 'trim');
111
            $fieldValue = array_map('trim', $fieldValue);
112
        }
113
114
        return new static($fieldValue);
115
    }
116
117
    /**
118
     * Allow sets of method.
119
     *
120
     * @param array $methods
121
     *
122
     * @return $this
123
     */
124
    public function allowMethods(array $methods)
125
    {
126
        foreach ($methods as $method) {
127
            $this->allowMethod($method);
128
        }
129
130
        return $this;
131
    }
132
133
    /**
134
     * Disallow sets of method.
135
     *
136
     * @param array $methods
137
     *
138
     * @return $this
139
     */
140
    public function disallowMethods(array $methods)
141
    {
142
        foreach ($methods as $method) {
143
            $this->disallowMethod($method);
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * Disallow method.
151
     *
152
     * @param string $method
153
     *
154
     * @return $this
155
     */
156
    public function disallowMethod($method)
157
    {
158
        $this->setState($method, false);
159
160
        return $this;
161
    }
162
163
    /**
164
     * Get allowed methods.
165
     *
166
     * @return array
167
     */
168
    public function getAllowedMethods()
169
    {
170
        return array_keys($this->methods, true, true);
171
    }
172
173
    /**
174
     * Get disallowed methods.
175
     *
176
     * @return array
177
     */
178
    public function getDisallowedMethods()
179
    {
180
        return array_keys($this->methods, false, true);
181
    }
182
183
    /**
184
     * Check that method is allowed.
185
     *
186
     * @param string $method
187
     *
188
     * @return bool Return true if method is allowed, false otherwise
189
     */
190
    public function isMethodAllowed($method)
191
    {
192
        $method = trim(strtoupper($method));
193
194
        return !isset($this->methods[$method])
195
            ? false //unknown method mark as disallowed
196
            : true === $this->methods[$method];
197
    }
198
199
    /**
200
     * Check that method is disallowed.
201
     *
202
     * @param string $method
203
     *
204
     * @return bool Return true if method is disallowed, false otherwise
205
     */
206
    public function isMethodDisallowed($method)
207
    {
208
        return false === $this->isMethodAllowed($method);
209
    }
210
211
    /**
212
     * Cast header to string.
213
     *
214
     * @return string
215
     */
216
    public function __toString()
217
    {
218
        return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue());
219
    }
220
221
    /**
222
     * Get header field name.
223
     *
224
     * @return string
225
     */
226
    public function getFieldName()
227
    {
228
        return 'Allow';
229
    }
230
231
    /**
232
     * Get header field value.
233
     *
234
     * @return mixed
235
     */
236
    public function getFieldValue()
237
    {
238
        return implode(',', $this->getAllowedMethods());
239
    }
240
241
    /**
242
     * Get all methods.
243
     *
244
     * @return array
245
     */
246
    public function getAllMethods()
247
    {
248
        return $this->methods;
249
    }
250
}
251