Conditions | 12 |
Paths | 21 |
Total Lines | 92 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
77 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
78 | { |
||
79 | // Ugly URLs store the path in a query parameter. |
||
80 | $url_route = Validator::queryParams($request)->string('route', ''); |
||
81 | |||
82 | if (Validator::attributes($request)->boolean('rewrite_urls', false)) { |
||
83 | // We are creating pretty URLs, but received an ugly one. Probably a search-engine. Redirect it. |
||
84 | if ($url_route !== '') { |
||
85 | $uri = $request->getUri() |
||
86 | ->withPath($url_route) |
||
87 | ->withQuery(explode('&', $request->getUri()->getQuery(), 2)[1] ?? ''); |
||
88 | |||
89 | return Registry::responseFactory()->redirectUrl($uri, StatusCodeInterface::STATUS_PERMANENT_REDIRECT); |
||
90 | } |
||
91 | |||
92 | $pretty = $request; |
||
93 | } else { |
||
94 | // Turn the ugly URL into a pretty one, so the router can parse it. |
||
95 | $uri = $request->getUri()->withPath($url_route); |
||
96 | $pretty = $request->withUri($uri); |
||
97 | } |
||
98 | |||
99 | // Match the request to a route. |
||
100 | $matcher = $this->router_container->getMatcher(); |
||
101 | $route = $matcher->match($pretty); |
||
102 | |||
103 | // No route matched? |
||
104 | if ($route === false) { |
||
105 | $failed_route = $matcher->getFailedRoute(); |
||
106 | |||
107 | if ($failed_route instanceof Route) { |
||
108 | if ($failed_route->failedRule === Allows::class) { |
||
109 | return Registry::responseFactory()->response('', StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED, [ |
||
110 | 'Allow' => implode(', ', $failed_route->allows), |
||
111 | ]); |
||
112 | } |
||
113 | |||
114 | if ($failed_route->failedRule === Accepts::class) { |
||
115 | return Registry::responseFactory()->response('Negotiation failed', StatusCodeInterface::STATUS_NOT_ACCEPTABLE); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | // Not found |
||
120 | return $handler->handle($request); |
||
121 | } |
||
122 | |||
123 | // Add the route as attribute of the request |
||
124 | $request = $request->withAttribute('route', $route); |
||
125 | |||
126 | // This middleware cannot run until after the routing, as it needs to know the route. |
||
127 | $post_routing_middleware = [CheckCsrf::class]; |
||
128 | |||
129 | // Firstly, apply the route middleware |
||
130 | $route_middleware = $route->extras['middleware'] ?? []; |
||
131 | |||
132 | // Secondly, apply any module middleware |
||
133 | $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all(); |
||
134 | |||
135 | // Finally, run the handler using middleware |
||
136 | $handler_middleware = [RequestHandler::class]; |
||
137 | |||
138 | $middleware = array_merge( |
||
139 | $post_routing_middleware, |
||
140 | $route_middleware, |
||
141 | $module_middleware, |
||
142 | $handler_middleware |
||
143 | ); |
||
144 | |||
145 | // Add the matched attributes to the request. |
||
146 | foreach ($route->attributes as $key => $value) { |
||
147 | if ($key === 'tree') { |
||
148 | $value = $this->tree_service->all()->get($value); |
||
149 | |||
150 | if ($value instanceof Tree) { |
||
151 | Registry::container()->set(Tree::class, $value); |
||
152 | } |
||
153 | |||
154 | // Missing mandatory parameter? Let the default handler take care of it. |
||
155 | if ($value === null && str_contains($route->path, '{tree}')) { |
||
156 | return $handler->handle($request); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | $request = $request->withAttribute((string) $key, $value); |
||
161 | } |
||
162 | |||
163 | // Bind the updated request into the container |
||
164 | Registry::container()->set(ServerRequestInterface::class, $request); |
||
165 | |||
166 | $dispatcher = new Dispatcher($middleware, Registry::container()); |
||
167 | |||
168 | return $dispatcher->dispatch($request); |
||
169 | } |
||
171 |