Completed
Push — master ( 24fba4...970de3 )
by
unknown
04:30
created

Unroutable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getHeaders() 0 4 1
A fromRoute() 0 14 4
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Route\Exception;
22
23
/**
24
 * Exceptions for requests that cannot be routed to a controller method.
25
 */
26
class Unroutable extends \RuntimeException implements \Minotaur\Route\Exception
27
{
28
    /**
29
     * @var array<string,string>
30
     */
31
    private $headers;
32
33
    /**
34
     * Creates a new Unroutable.
35
     */
36 5
    public function __construct(
37
        string $message = "",
38
        int $code = 0,
39
        \Exception $cause = null,
40
        array $headers = []
41
    ) {
42 5
        parent::__construct($message, $code, $cause);
43 5
        $this->headers = $headers;
44 5
    }
45
46
    /**
47
     * Gets the headers to set.
48
     *
49
     * @return array<string,string> associative array of header names to values
50
     */
51 3
    public function getHeaders(): array
52
    {
53 3
        return $this->headers;
54
    }
55
56
    /**
57
     * Creates a new Unroutable based on a failed Aura Route.
58
     *
59
     * @param \Aura\Router\Route $failedRoute The failed Aura Route
60
     * @return static A new Unroutable
61
     */
62 1
    public static function fromRoute(\Aura\Router\Route $failedRoute): Unroutable
63
    {
64 1
        switch ($failedRoute->failedRule) {
65 1
            case \Aura\Router\Rule\Allows::class:
66 1
                $allows = ['Allow' => implode(',', $failedRoute->allows)];
67 1
                return new self("Method Not Allowed", 405, null, $allows);
68
            case \Aura\Router\Rule\Accepts::class:
69
                return new self("Not Acceptable", 406);
70
            case \Minotaur\Route\AuthRule::class:
71
                return new self("You Must Be Authenticated", 403);
72
            default:
73
                return new self("Not Found", 404);
74
        }
75
    }
76
}
77