Passed
Branch refactor/kernels (d3615b)
by Atanas
01:46
created

WordPressHttpKernel::bootstrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
c 0
b 0
f 0
cc 1
crap 2
rs 10
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\Exceptions\ErrorHandlerInterface;
14
use WPEmerge\Requests\RequestInterface;
15
use WPEmerge\Routing\Router;
16
17
/**
18
 * Describes how a request is handled.
19
 */
20
class WordPressHttpKernel implements HttpKernel {
21
	/**
22
	 * Error handler.
23
	 *
24
	 * @var ErrorHandlerInterface
25
	 */
26
	protected $error_handler = null;
27
28
	/**
29
	 * Router.
30
	 *
31
	 * @var Router
32
	 */
33
	protected $router = null;
34
35
	/**
36
	 * Middleware available to the application.
37
	 *
38
	 * @var array<string, string>
39
	 */
40
	protected $middleware = [];
41
42
	/**
43
	 * Middleware groups.
44
	 *
45
	 * @var array<string, array<string>>
46
	 */
47
	protected $middleware_groups = [];
48
49
	/**
50
	 * Global middleware that will be applied to all routes.
51
	 *
52
	 * @var array
53
	 */
54
	protected $global_middleware = [
55
		\WPEmerge\Flash\FlashMiddleware::class,
56
		\WPEmerge\Input\OldInputMiddleware::class,
57
	];
58
59
	/**
60
	 * Middleware sorted in order of execution.
61
	 *
62
	 * @var array<string>
63
	 */
64
	protected $middleware_priority = [];
65
66
	/**
67
	 * Constructor.
68
	 *
69
	 * @codeCoverageIgnore
70
	 * @param Router $router
71
	 * @param ErrorHandlerInterface $error_handler
72
	 */
73
	public function __construct( $router, ErrorHandlerInterface $error_handler ) {
74
		$this->router = $router;
75
		$this->error_handler = $error_handler;
76
	}
77
78
	/**
79
	 * {@inheritDoc}
80
	 */
81
	public function bootstrap() {
82
		$this->router->setMiddleware( $this->middleware );
83
		$this->router->setMiddlewareGroups( $this->middleware_groups );
84
		$this->router->setGlobalMiddleware( $this->global_middleware );
85
		$this->router->setMiddlewarePriority( $this->middleware_priority );
86
	}
87
88
	/**
89
	 * {@inheritDoc}
90
	 */
91 1
	public function handle( RequestInterface $request, $view ) {
92
		try {
93 1
			$this->error_handler->register();
94
95 1
			$response = $this->router->execute( $request, $view );
96
97
			$this->error_handler->unregister();
98 1
		} catch ( Exception $exception ) {
99 1
			$response = $this->error_handler->getResponse( $exception );
100
		}
101
102
		return $response;
103
	}
104
}
105