Passed
Push — 0.7.0 ( e442b6...679d5f )
by Alexander
11:21 queued 12s
created

RouteResolver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 114
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatchToRoute() 0 3 1
A callResponse() 0 11 5
A resolve() 0 3 1
A runRoute() 0 8 1
A runRouteStack() 0 14 3
A findRoute() 0 8 1
1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Routing\Concerns;
24
25
use Closure;
26
use JsonSerializable;
27
use Syscodes\Http\Request;
28
use Syscodes\Http\Response;
29
use Syscodes\Routing\Route;
30
use Syscodes\Routing\Pipeline;
31
use Syscodes\Http\JsonResponse;
32
33
/**
34
 * This trait resolve the given route and called the method that belongs to the route.
35
 * 
36
 * @author Alexander Campo <[email protected]>
37
 */
38
trait RouteResolver
39
{
40
	/**
41
	 * The currently dispatched route instance.
42
	 * 
43
	 * @var \Syscodes\Routing\Route|null
44
	 */
45
	protected $current;
46
47
	/**
48
	 * Resolve the given route and call the method that belongs to the route.
49
	 *
50
	 * @param  \Syscodes\Http\Request  $request
51
	 *
52
	 * @return \Syscodes\Http\Response
53
	 */
54
	public function resolve(Request $request)
55
	{
56
		return $this->dispatchToRoute($request);
57
	}
58
59
	/**
60
	 * Dispatch the request to a route and return the response.
61
	 * 
62
	 * @param  \Syscodes\Http\Request  $request
63
	 *
64
	 * @return \Syscodes\Http\Response
65
	 */
66
	protected function dispatchToRoute(Request $request)
67
	{
68
		return $this->runRoute($request, $this->findRoute($request));
69
	}
70
71
	/**
72
	 * Find the route matching a given request.
73
	 * 
74
	 * @param  \Syscodes\Http\Request  $request
75
	 * 
76
	 * @return \Syscodes\Routing\Route
77
	 */
78
	protected function findRoute($request)
79
	{
80
		// Get all register routes with the same request method
81
		$this->current = $route = $this->routes->match($request);
82
83
		$this->container->instance(Route::class, $route);
84
85
		return $route;
86
	}
87
88
	/**
89
	 * Return the response for the given route.
90
	 * 
91
	 * @param  \Syscodes\Http\Request  $request
92
	 * @param  \Syscodes\Routing\Route  $route
93
	 * 
94
	 * @return \Syscodes\Http\Response
95
	 */
96
	protected function runRoute(Request $request, Route $route)
97
	{
98
		$request->setRouteResolver(function () use ($route) {
99
			return $route;
100
		});
101
102
		return $this->callResponse($request, 
103
			$this->runRouteStack($route, $request)
104
		); 
105
	}
106
107
	/**
108
	 * Run the given route through a stack response instance.
109
	 * 
110
	 * @param  \Syscodes\Routing\Route  $route
111
	 * @param  \Syscodes\Http\Request  $request
112
	 * 
113
	 * @return mixed
114
	 */
115
	protected function runRouteStack(Route $route, Request $request)
116
	{
117
		$skipMiddleware = $this->container->bound('middleware.disable') &&
118
						  ($this->container->make('middleware.disable') === true);
119
						  
120
		
121
		$middleware = $skipMiddleware ? [] : $this->gatherRouteMiddleware($route);
0 ignored issues
show
Bug introduced by
It seems like gatherRouteMiddleware() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
		$middleware = $skipMiddleware ? [] : $this->/** @scrutinizer ignore-call */ gatherRouteMiddleware($route);
Loading history...
122
123
		return (new Pipeline($this->container))
124
				->send($request)
125
				->through($middleware)
126
				->then(function ($request) use ($route) {
127
					return $this->callResponse(
128
						$request, $route->runResolver()
129
					); 
130
				});
131
	}
132
133
	/**
134
	 * Create a response instance from the given value.
135
	 * 
136
	 * @param  \Syscodes\Http\Request  $request
137
	 * @param  mixed  $response
138
	 * 
139
	 * @return \Syscodes\Http\Response
140
	 */
141
	protected function callResponse($request, $response)
142
	{
143
		if ( ! $response instanceof Response && 
144
		      ($response instanceof Jsonserializable || 
145
			   is_array($response))) {
146
			$response = new JsonResponse($response);
147
		} elseif ( ! $response instanceof Response) {
148
			$response = new Response($response, 200, ['Content-Type' => 'text/html']);
149
		}
150
151
		return $response->prepare($request);
152
	}
153
}