Passed
Branch dev (473855)
by Alex
02:25
created

MethodNotAllowedException   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 3
c 5
b 0
f 0
lcom 1
cbo 1
dl 0
loc 71
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A can() 0 4 1
A allowed() 0 4 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;
12
13
/**
14
 * Exception thrown when none route is matched, but a similar
15
 * route is found in another http method.
16
 *
17
 * @author Alex Rohleder <[email protected]>
18
 */
19
20
class MethodNotAllowedException extends BadRouteException
21
{
22
23
    /**
24
     * The HTTP method from request.
25
     *
26
     * @var string
27
     */
28
29
    public $requestedMethod;
30
31
    /**
32
     * The requested URi.
33
     *
34
     * @var string
35
     */
36
37
    public $requestedUri;
38
39
    /**
40
     * All the allowed HTTP methods and routes for the request.
41
     *
42
     * @var array
43
     */
44
45
    public $allowedMethods;
46
	
47
    /**
48
     * Exception constructor.
49
     *
50
     * @param string  $requestedMethod The request HTTP method.
51
     * @param string  $requestedUri    The request URi.
52
     * @param array   $allowedMethods  All the allowed HTTP methods and routes for the request.
53
     * @param string  $message         The exception error message.
54
     * @param integer $code            The exception error code.
55
     */
56
57 5
    public function __construct($requestedMethod, $requestedUri, array $allowedMethods, $message = null, $code = 405)
58
    {
59 5
        $this->requestedMethod = $requestedMethod;
60 5
        $this->requestedUri    = $requestedUri;
61 5
        $this->allowedMethods  = $allowedMethods;
62 5
        parent::__construct($message, $code);
63 5
    }
64
65
    /**
66
     * Verify if the given HTTP method is allowed for the request.
67
     *
68
     * @param string $method An HTTP method
69
     * @return bool
70
     */
71
72 1
    public function can($method)
73
    {
74 1
        return in_array(strtolower($method), $this->allowedMethods);
75
    }
76
77
    /**
78
     * The HTTP specification requires that a 405 Method Not Allowed response include the 
79
     * Allow: header to detail available methods for the requested resource.
80
     *
81
     * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7
82
     * @return string
83
     */
84
    
85 1
    public function allowed()
86
    {
87 1
        return implode(', ', $this->allowedMethods);
88
    }
89
90
}
91