Passed
Branch release/v2.0.0 (bebdea)
by Anatoly
04:02
created

ExceptionFactory::routeNotFoundByUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router;
13
14
/**
15
 * Import classes
16
 */
17
use Sunrise\Http\Router\Exception\InvalidLoadResourceException;
18
use Sunrise\Http\Router\Exception\MethodNotAllowedException;
19
use Sunrise\Http\Router\Exception\MiddlewareAlreadyExistsException;
20
use Sunrise\Http\Router\Exception\RouteAlreadyExistsException;
21
use Sunrise\Http\Router\Exception\RouteNotFoundException;
22
23
/**
24
 * Import functions
25
 */
26
use function sprintf;
27
28
/**
29
 * ExceptionFactory
30
 */
31
class ExceptionFactory
32
{
33
34
    /**
35
     * Used by a routes loader when the given resource wasn't found as a file
36
     *
37
     * @param mixed $resource
38
     * @param array $context
39
     *
40
     * @return InvalidLoadResourceException
41
     */
42 3
    public function invalidLoadResourceForFile($resource, array $context = []) : InvalidLoadResourceException
43
    {
44 3
        $context['resource'] = $resource;
45
46 3
        $message = sprintf('The resource "%s" not found.', $resource);
47
48 3
        return new InvalidLoadResourceException($message, $context);
49
    }
50
51
    /**
52
     * Used if the given method isn't allowed
53
     *
54
     * It is recommended to add a request instance to the context.
55
     *
56
     * @param string $method
57
     * @param array $allowed
58
     * @param array $context
59
     *
60
     * @return MethodNotAllowedException
61
     */
62 4
    public function methodNotAllowed(string $method, array $allowed, array $context = []) : MethodNotAllowedException
63
    {
64 4
        $context['method'] = $method;
65 4
        $context['allowed'] = $allowed;
66
67 4
        $message = sprintf('The method "%s" is not allowed.', $method);
68
69 4
        return new MethodNotAllowedException($message, $context);
70
    }
71
72
    /**
73
     * Used when a middleware with the given hash already exists in a stack
74
     *
75
     * It is recommended to add a middleware instance to the context.
76
     *
77
     * @param string $hash
78
     * @param array $context
79
     *
80
     * @return MiddlewareAlreadyExistsException
81
     */
82 2
    public function middlewareAlreadyExists(string $hash, array $context = []) : MiddlewareAlreadyExistsException
83
    {
84 2
        $context['hash'] = $hash;
85
86 2
        $message = sprintf('A middleware with the hash "%s" already exists.', $hash);
87
88 2
        return new MiddlewareAlreadyExistsException($message, $context);
89
    }
90
91
    /**
92
     * Used when a route with the given name already exists in a stack
93
     *
94
     * It is recommended to add a route instance to the context.
95
     *
96
     * @param string $name
97
     * @param array $context
98
     *
99
     * @return RouteAlreadyExistsException
100
     */
101 2
    public function routeAlreadyExists(string $name, array $context = []) : RouteAlreadyExistsException
102
    {
103 2
        $context['name'] = $name;
104
105 2
        $message = sprintf('A route with the name "%s" already exists.', $name);
106
107 2
        return new RouteAlreadyExistsException($message, $context);
108
    }
109
110
    /**
111
     * Used when trying to find a route by the given name
112
     *
113
     * @param string $name
114
     * @param array $context
115
     *
116
     * @return RouteNotFoundException
117
     */
118 2
    public function routeNotFoundByName(string $name, array $context = []) : RouteNotFoundException
119
    {
120 2
        $context['name'] = $name;
121
122 2
        $message = sprintf('No route found for the name "%s".', $name);
123
124 2
        return new RouteNotFoundException($message, $context);
125
    }
126
127
    /**
128
     * Used when trying to find a route by the given URI
129
     *
130
     * It is recommended to add a request instance to the context.
131
     *
132
     * @param string $uri
133
     * @param array $context
134
     *
135
     * @return RouteNotFoundException
136
     */
137 4
    public function routeNotFoundByUri(string $uri, array $context = []) : RouteNotFoundException
138
    {
139 4
        $context['uri'] = $uri;
140
141 4
        $message = sprintf('No route found for the URI "%s".', $uri);
142
143 4
        return new RouteNotFoundException($message, $context);
144
    }
145
}
146