Completed
Push — master ( 3ac6d3...638eef )
by Nazar
07:17
created

Middleware::process()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 15
rs 8.8571
1
<?php
2
/**
3
 * @package  Psr15
4
 * @category modules
5
 * @author   Nazar Mokrynskyi <[email protected]>
6
 * @license  0BSD
7
 */
8
namespace cs\modules\Psr15;
9
use
10
	cs\App,
11
	cs\ExitException,
12
	cs\Page,
13
	cs\Response as System_response,
14
	cs\User,
15
	cs\modules\Psr7\Request,
16
	cs\modules\Psr7\Response,
17
	Psr\Http\Server\MiddlewareInterface,
18
	Psr\Http\Server\RequestHandlerInterface,
19
	Psr\Http\Message\ServerRequestInterface,
20
	Psr\Http\Message\ResponseInterface;
21
22
class Middleware implements MiddlewareInterface {
23
	private $memory_cache_disabled = false;
24
	/**
25
	 * @var string
26
	 */
27
	protected $psr7_response_class_name;
28
	/**
29
	 * @param string $psr7_response_class_name
30
	 */
31
	public function __construct ($psr7_response_class_name) {
32
		$this->psr7_response_class_name = $psr7_response_class_name;
33
	}
34
	/**
35
	 * @inheritdoc
36
	 */
37
	public function process (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
38
		try {
39
			System_response::instance()->init_with_typical_default_settings();
40
			Request::init_from_psr7($request);
41
			App::instance()->execute();
42
			if (!$this->memory_cache_disabled) {
43
				$this->memory_cache_disabled = true;
44
				User::instance()->disable_memory_cache();
45
			}
46
		} catch (ExitException $e) {
47
			if ($e->getCode() >= 400) {
48
				Page::instance()->error($e->getMessage() ?: null, $e->getJson());
49
			}
50
		}
51
		return Response::output_to_psr7(new $this->psr7_response_class_name);
52
	}
53
}
54