Passed
Branch dev (8e1e05)
by Alex
03:51
created

MethodNotAllowedException::can()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Codeburner Framework.
5
 *
6
 * @author Alex Rohleder <[email protected]>
7
 * @copyright 2016 Alex Rohleder
8
 * @license http://opensource.org/licenses/MIT
9
 */
10
11
namespace Codeburner\Router\Exceptions\Http;
12
13
/**
14
 * MethodNotAllowed
15
 *
16
 * @author Alex Rohleder <[email protected]>
17
 */
18
19
class MethodNotAllowedException extends HttpExceptionAbstract
20
{
21
22
    /**
23
     * @var string
24
     */
25
26
    protected $requestedMethod;
27
28
    /**
29
     * @var string
30
     */
31
32
    protected $requestedUri;
33
34
    /**
35
     * @var string[]
36
     */
37
38
    protected $allowedMethods;
39
40
    /**
41
     * MethodNotAllowedException constructor.
42
     *
43
     * @param string $requestedMethod
44
     * @param string $requestedUri
45
     * @param string[] $allowedMethods
46
     */
47
48 5
    public function __construct($requestedMethod, $requestedUri, array $allowedMethods)
49
    {
50 5
        $this->requestedMethod = $requestedMethod;
51 5
        $this->requestedUri = $requestedUri;
52 5
        $this->allowedMethods = $allowedMethods;
53 5
        $this->code = 405;
54 5
        $this->message = "Method Not Allowed";
55 5
    }
56
57
    /**
58
     * Verify if the given HTTP method is allowed for the request.
59
     *
60
     * @param string $method An HTTP method
61
     * @return bool
62
     */
63
64 1
    public function can($method)
65
    {
66 1
        return in_array(strtolower($method), $this->allowedMethods);
67
    }
68
69
    /**
70
     * The HTTP specification requires that a 405 Method Not Allowed response include the
71
     * Allow: header to detail available methods for the requested resource.
72
     *
73
     * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7
74
     * @return string
75
     */
76
77 1
    public function allowed()
78
    {
79 1
        return implode(', ', $this->allowedMethods);
80
    }
81
82
}
83