Completed
Branch refactor/kernels (a21eb4)
by Atanas
02:06
created

HttpKernel::filterRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 17
ccs 0
cts 11
cp 0
crap 20
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Kernels;
11
12
use Exception;
13
use WPEmerge\Application\Application;
14
use WPEmerge\Exceptions\ErrorHandlerInterface;
15
use WPEmerge\Requests\RequestInterface;
16
use WPEmerge\Routing\HasQueryFilterInterface;
17
use WPEmerge\Routing\Router;
18
19
/**
20
 * Describes how a request is handled.
21
 */
22
class HttpKernel implements HttpKernelInterface {
23
	/**
24
	 * Application.
25
	 *
26
	 * @var Application
27
	 */
28
	protected $app = null;
29
30
	/**
31
	 * Request.
32
	 *
33
	 * @var RequestInterface
34
	 */
35
	protected $request = null;
36
37
	/**
38
	 * Router.
39
	 *
40
	 * @var Router
41
	 */
42
	protected $router = null;
43
44
	/**
45
	 * Error handler.
46
	 *
47
	 * @var ErrorHandlerInterface
48
	 */
49
	protected $error_handler = null;
50
51
	/**
52
	 * Middleware available to the application.
53
	 *
54
	 * @var array<string, string>
55
	 */
56
	protected $middleware = [];
57
58
	/**
59
	 * Middleware groups.
60
	 *
61
	 * @var array<string, array<string>>
62
	 */
63
	protected $middleware_groups = [];
64
65
	/**
66
	 * Global middleware that will be applied to all routes.
67
	 *
68
	 * @var array
69
	 */
70
	protected $global_middleware = [
71
		\WPEmerge\Flash\FlashMiddleware::class,
72
		\WPEmerge\Input\OldInputMiddleware::class,
73
	];
74
75
	/**
76
	 * Middleware sorted in order of execution.
77
	 *
78
	 * @var array<string>
79
	 */
80
	protected $middleware_priority = [];
81
82
	/**
83
	 * Constructor.
84
	 *
85
	 * @codeCoverageIgnore
86
	 * @param RequestInterface      $request
87
	 * @param Router                $router
88
	 * @param ErrorHandlerInterface $error_handler
89
	 */
90
	public function __construct( Application $app, RequestInterface $request, Router $router, ErrorHandlerInterface $error_handler ) {
91
		$this->app = $app;
92
		$this->request = $request;
93
		$this->router = $router;
94
		$this->error_handler = $error_handler;
95
	}
96
97
	/**
98
	 * {@inheritDoc}
99
	 * @codeCoverageIgnore
100
	 */
101
	public function bootstrap() {
102
		$this->router->setMiddleware( $this->middleware );
103
		$this->router->setMiddlewareGroups( $this->middleware_groups );
104
		$this->router->setGlobalMiddleware( $this->global_middleware );
105
		$this->router->setMiddlewarePriority( $this->middleware_priority );
106
107
		add_action( 'request', [$this, 'filterRequest'], 1000 );
108
		add_action( 'template_include', [$this, 'filterTemplateInclude'], 1000 );
109
	}
110
111
	/**
112
	 * {@inheritDoc}
113
	 */
114 2
	public function handle( RequestInterface $request, $view ) {
115 2
		$this->error_handler->register();
116
117
		try {
118 2
			$response = $this->router->execute( $request, $view );
119 2
		} catch ( Exception $exception ) {
120 1
			$response = $this->error_handler->getResponse( $exception );
121
		}
122
123 1
		$this->error_handler->unregister();
124
125 1
		return $response;
126
	}
127
128
	/**
129
	 * Filter the main query vars.
130
	 *
131
	 * @param  array $query_vars
132
	 * @return array
133
	 */
134
	public function filterRequest( $query_vars ) {
135
		$routes = $this->router->getRoutes();
136
137
		foreach ( $routes as $route ) {
138
			if ( ! $route instanceof HasQueryFilterInterface ) {
139
				continue;
140
			}
141
142
			if ( ! $route->isSatisfied( $this->request ) ) {
143
				continue;
144
			}
145
146
			$query_vars = $route->applyQueryFilter( $this->request, $query_vars );
147
			break;
148
		}
149
150
		return $query_vars;
151
	}
152
153
	/**
154
	 * Filter the main template file.
155
	 *
156
	 * @param  string $view
157
	 * @return string
158
	 */
159
	public function filterTemplateInclude( $view ) {
160
		$response = $this->handle( $this->request, $view );
161
162
		if ( $response instanceof \Psr\Http\Message\ResponseInterface ) {
163
			$container = $this->app->getContainer();
164
			$container[ WPEMERGE_RESPONSE_KEY ] = $response;
165
166
			return WPEMERGE_DIR . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'view.php';
167
		}
168
169
		return $view;
170
	}
171
}
172