Passed
Push — 0.7.0 ( 01cd5f...881fd9 )
by Alexander
03:57
created

Lenevor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 35
c 5
b 0
f 0
dl 0
loc 160
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 11 2
A sendRequestThroughRouter() 0 13 2
A reportException() 0 3 1
A dispatchToRouter() 0 6 1
A bootstrappers() 0 3 1
A bootstrap() 0 4 2
A renderException() 0 3 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\Core\Http;
24
25
use Closure;
26
use Throwable; 
27
use Syscodes\Routing\Router;
28
use Syscodes\Routing\Pipeline;
29
use Syscodes\Support\Facades\Facade;
30
use Syscodes\Contracts\Core\Application;
31
use Syscodes\Contracts\Debug\ExceptionHandler;
32
use Syscodes\Contracts\Http\Lenevor as LenevorContract;
33
34
/**
35
 * The Lenevor class is the heart of the system framework.
36
 * 
37
 * @author Alexander Campo <[email protected]>
38
 */
39
class Lenevor implements LenevorContract
40
{
41
	/**
42
	 * The application implementation.
43
	 * 
44
	 * @var \Syscodes\Contracts\Core\Application $app
45
	 */
46
	protected $app;
47
	
48
	/**
49
	 * The bootstrap classes for the application.
50
	 * 
51
	 * @var array $bootstrappers
52
	 */
53
	protected $bootstrappers = [
54
		\Syscodes\Core\Bootstrap\BootDetectEnvironment::class,
55
		\Syscodes\Core\Bootstrap\BootConfiguration::class,
56
		\Syscodes\Core\Bootstrap\BootHandleExceptions::class,
57
		\Syscodes\Core\Bootstrap\BootRegisterFacades::class,
58
		\Syscodes\Core\Bootstrap\BootRegisterProviders::class,
59
		\Syscodes\Core\Bootstrap\BootProviders::class,
60
	];
61
62
	/**
63
	 * Get the application's middleware.
64
	 * 
65
	 * @var array $middleware
66
	 */
67
	protected $middleware = [];
68
69
	/**
70
	 * The router instance.
71
	 * 
72
	 * @var \Syscodes\Routing\Router $router
73
	 */
74
	protected $router;
75
76
	/**
77
	 * Total app execution time.
78
	 * 
79
	 * @var float $totalTime
80
	 */
81
	protected $totalTime;
82
83
	/**
84
	 * Constructor. Lenevor class instance.
85
	 * 
86
	 * @param  \Syscodes\Contracts\Core\Application  $app
87
	 * @param  \Syscodes\Routing\Router  $router
88
	 * 
89
	 * @return void
90
	 */
91
	public function __construct(Application $app, Router $router)
92
	{
93
		$this->app    = $app;
94
		$this->router = $router;
95
	}
96
	 
97
	/**
98
	 * Initializes the framework, this can only be called once.
99
	 * Launch the application.
100
	 * 
101
	 * @param  \Syscodes\http\Request  $request
102
	 *
103
	 * @return \Syscodes\Http\Response
104
	 */
105
	public function handle($request)
106
	{
107
		try {
108
			$response = $this->sendRequestThroughRouter($request);
109
		} catch (Throwable $e) {
110
			$this->reportException($e);
111
112
			$response = $this->renderException($request, $e);
113
		}		
114
115
		return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Syscodes\Http\Response which is incompatible with the return type mandated by Syscodes\Contracts\Http\Lenevor::handle() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
116
	}
117
118
	/**
119
	 * Send the given request through the router.
120
	 * 
121
	 * @param  \Syscodes\Http\Request  $request
122
	 * 
123
	 * @return \Syscodes\Http\Response
124
	 */
125
	protected function sendRequestThroughRouter($request)
126
	{
127
		$this->app->instance('request', $request);  
128
129
		Facade::clearResolvedInstance('request');
130
131
		// Load configuration system
132
		$this->bootstrap();
133
134
		return (new Pipeline($this->app))
135
				->send($request)
136
				->through($this->app->skipGoingMiddleware() ? [] : $this->middleware)
137
				->then($this->dispatchToRouter());
138
	}
139
140
	/**
141
	 * Bootstrap the application for HTTP requests.
142
	 * 
143
	 * @return void
144
	 */
145
	protected function bootstrap()
146
	{		
147
		if ( ! $this->app->hasBeenBootstrapped()) {
148
			$this->app->bootstrapWith($this->bootstrappers());
149
		}
150
	}
151
152
	/**
153
	 * Get the bootstrap classes for the application.
154
	 * 
155
	 * @return array
156
	 */
157
	protected function bootstrappers()
158
	{
159
		return $this->bootstrappers;
160
	}
161
162
	/**
163
	 * Get the dispatcher of routes.
164
	 * 	  
165
	 * @return \Closure
166
 	 */
167
	protected function dispatchToRouter()
168
	{
169
		return function ($request) {
170
			$this->app->instance('request', $request);
171
172
			return $this->router->dispatch($request);
173
		};
174
	}
175
176
	/**
177
	 * Report the exception to the exception handler.
178
	 * 
179
	 * @param  \Throwable  $e
180
	 * 
181
	 * @return void
182
	 */
183
	protected function reportException(Throwable $e)
184
	{
185
		return $this->app[ExceptionHandler::class]->report($e);
186
	}
187
	
188
	/**
189
	 * Render the exception to a response.
190
	 * 
191
	 * @param  \Syscodes\Http\Request  $request
192
	 * @param  \Throwable  $e
193
	 * 
194
	 * @return \Syscodes\Http\Response
195
	 */
196
	protected function renderException($request, Throwable $e)
197
	{
198
		return $this->app[ExceptionHandler::class]->render($request, $e);
199
	}
200
 }