Failed Conditions
Branch refactor/kernels (2fb3f9)
by Atanas
01:46
created

ErrorHandler::getResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 12
rs 10
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\Exceptions;
11
12
use Exception as PhpException;
13
use Psr\Http\Message\ResponseInterface;
14
use Whoops\RunInterface;
15
use WPEmerge\Csrf\InvalidCsrfTokenException;
16
use WPEmerge\Facades\Response;
17
use WPEmerge\Requests\RequestInterface;
18
use WPEmerge\Routing\NotFoundException;
19
use WPEmerge\Support\Arr;
20
21
class ErrorHandler implements ErrorHandlerInterface {
22
	/**
23
	 * Pretty handler.
24
	 *
25
	 * @var RunInterface|null
26
	 */
27
	protected $whoops = null;
28
29
	/**
30
	 * Whether debug mode is enabled.
31
	 *
32
	 * @var boolean
33
	 */
34
	protected $debug = false;
35
36
	/**
37
	 * Constructor.
38
	 *
39
	 * @codeCoverageIgnore
40
	 * @param RunInterface|null $whoops
41
	 * @param boolean           $debug
42
	 */
43
	public function __construct( $whoops, $debug = false ) {
44
		$this->whoops = $whoops;
45
		$this->debug = $debug;
46
	}
47
48
	/**
49
	 * {@inheritDoc}
50
	 * @codeCoverageIgnore
51
	 */
52
	public function register() {
53
		if ( $this->whoops !== null ) {
54
			$this->whoops->register();
55
		}
56
	}
57
58
	/**
59
	 * {@inheritDoc}
60
	 * @codeCoverageIgnore
61
	 */
62
	public function unregister() {
63
		if ( $this->whoops !== null ) {
64
			$this->whoops->unregister();
65
		}
66
	}
67
68
	/**
69
	 * Convert an exception to a ResponseInterface instance if possible.
70
	 *
71
	 * @param  PhpException            $exception
72 2
	 * @return ResponseInterface|false
73
	 */
74
	protected function toResponse( $exception ) {
75
		// @codeCoverageIgnoreStart
76
		if ( $exception instanceof InvalidCsrfTokenException ) {
77
			wp_nonce_ays( '' );
78
		}
79 2
		// @codeCoverageIgnoreEnd
80 1
81
		if ( $exception instanceof NotFoundException ) {
82
			return Response::error( 404 );
83 1
		}
84
85
		return false;
86
	}
87
88
	/**
89
	 * Convert an exception to a debug ResponseInterface instance if possible.
90
	 *
91
	 * @throws PhpException
92
	 * @param  RequestInterface  $request
93
	 * @param  PhpException      $exception
94
	 * @return ResponseInterface
95
	 */
96
	protected function toDebugResponse( RequestInterface $request, PhpException $exception ) {
97
		if ( $request->isAjax() ) {
98
			return Response::json( [
99
				'message' => $exception->getMessage(),
100
				'exception' => get_class( $exception ),
101
				'file' => $exception->getFile(),
102
				'line' => $exception->getLine(),
103
				'trace' => array_map( function ( $trace ) {
104
					return Arr::except( $trace, ['args'] );
105 4
				}, $exception->getTrace() ),
106 4
			] )->withStatus( 500 );
107
		}
108 4
109 1
		if ( $this->whoops !== null ) {
110
			return $this->toPrettyErrorResponse( $exception );
111
		}
112 3
113 1
		throw $exception;
114
	}
115
116 2
	/**
117 1
	 * Convert an exception to a pretty error response.
118
	 *
119
	 * @codeCoverageIgnore
120 1
	 * @param  PhpException      $exception
121
	 * @return ResponseInterface
122
	 */
123
	protected function toPrettyErrorResponse( $exception ) {
124
		$method = RunInterface::EXCEPTION_HANDLER;
125
		ob_start();
126
		$this->whoops->$method( $exception );
127
		$response = ob_get_clean();
128
		return Response::output( $response )->withStatus( 500 );
129
	}
130
131
	/**
132
	 * {@inheritDoc}
133
	 * @throws PhpException
134
	 */
135
	public function getResponse( RequestInterface $request, PhpException $exception ) {
136
		$response = $this->toResponse( $exception );
137
138
		if ( $response !== false ) {
139
			return $response;
140
		}
141
142
		if ( ! $this->debug ) {
143
			return Response::error( 500 );
144
		}
145
146
		return $this->toDebugResponse( $request, $exception );
147
	}
148
}
149