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

Unroutable::fromRoute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 14
ccs 5
cts 10
cp 0.5
crap 6
rs 9.2
c 0
b 0
f 0
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