Completed
Pull Request — dev (#11)
by
unknown
05:12 queued 01:17
created

MethodNotAllowedException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 32
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAssociatedStatusCode() 0 7 3
1
<?php
2
3
namespace Vectorface\SnappyRouter\Exception;
4
5
use \Exception;
6
use Vectorface\SnappyRouter\Response\AbstractResponse;
7
8
/**
9
 * An exception indicating an invalid HTTP action was specified.
10
 * @copyright Copyright (c) 2014, VectorFace, Inc.
11
 * @author Dan Bruce <[email protected]>
12
 */
13
class MethodNotAllowedException extends Exception implements RouterExceptionInterface
14
{
15
    // as per RFC2616 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6)
16
    // we must specify a comma separated list of allowed methods if we return
17
    // a 405 response
18
    private $allowedMethods;
19
20
    /**
21
     * Constructor for the method.
22
     * @param string $message The error message string.
23
     * @param array $allowedMethods The array of methods that are allowed.
24
     */
25 1
    public function __construct($message, $allowedMethods)
26
    {
27 1
        parent::__construct($message);
28 1
        $this->allowedMethods = $allowedMethods;
29 1
    }
30
31
    /**
32
     * Returns the associated status code with the exception. By default, most exceptions correspond
33
     * to a server error (HTTP 500). Override this method if you want your exception to generate a
34
     * different status code.
35
     * @return The associated status code.
36
     */
37 1
    public function getAssociatedStatusCode()
38
    {
39 1
        if (!empty($this->allowedMethods) && is_array($this->allowedMethods)) {
40 1
            @header(sprintf('Allow: %s', implode(',', $this->allowedMethods)));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
41 1
        }
42 1
        return AbstractResponse::RESPONSE_METHOD_NOT_ALLOWED;
43
    }
44
}
45