Completed
Push — master ( da5ca0...e55bc1 )
by Atanas
02:05
created

ErrorHandler::getResponse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\Exceptions;
4
5
use Exception as PhpException;
6
use Psr\Http\Message\ResponseInterface;
7
use Whoops\RunInterface;
8
use WPEmerge\Csrf\InvalidCsrfTokenException;
9
use WPEmerge\Facades\Response;
10
use WPEmerge\Routing\NotFoundException;
11
12
class ErrorHandler implements ErrorHandlerInterface {
13
	/**
14
	 * Pretty handler.
15
	 *
16
	 * @var RunInterface|null
17
	 */
18
	protected $whoops = null;
19
20
	/**
21
	 * Whether debug mode is enabled.
22
	 *
23
	 * @var boolean
24
	 */
25
	protected $debug = false;
26
27
	/**
28
	 * Constructor.
29
	 *
30
	 * @codeCoverageIgnore
31
	 * @param RunInterface|null $whoops
32
	 * @param boolean           $debug
33
	 */
34
	public function __construct( $whoops, $debug = false ) {
35
		$this->whoops = $whoops;
36
		$this->debug = $debug;
37
	}
38
39
	/**
40
	 * {@inheritDoc}
41
	 * @codeCoverageIgnore
42
	 */
43
	public function register() {
44
		if ( $this->whoops !== null ) {
45
			$this->whoops->register();
46
		}
47
	}
48
49
	/**
50
	 * {@inheritDoc}
51
	 * @codeCoverageIgnore
52
	 */
53
	public function unregister() {
54
		if ( $this->whoops !== null ) {
55
			$this->whoops->unregister();
56
		}
57
	}
58
59
	/**
60
	 * Convert an exception to a ResponseInterface instance if possible.
61
	 *
62
	 * @param  PhpException            $exception
63
	 * @return ResponseInterface|false
64
	 */
65 2
	protected function toResponse( $exception ) {
66
		// @codeCoverageIgnoreStart
67
		if ( $exception instanceof InvalidCsrfTokenException ) {
68
			wp_nonce_ays( '' );
69
		}
70
		// @codeCoverageIgnoreEnd
71
72 2
		if ( $exception instanceof NotFoundException ) {
73 1
			return Response::error( 404 );
74
		}
75
76 1
		return false;
77
	}
78
79
	/**
80
	 * Convert an exception to a pretty error response.
81
	 *
82
	 * @codeCoverageIgnore
83
	 * @param  PhpException      $exception
84
	 * @return ResponseInterface
85
	 */
86
	protected function toPrettyErrorResponse( $exception ) {
87
		$method = RunInterface::EXCEPTION_HANDLER;
88
		ob_start();
89
		$this->whoops->$method( $exception );
90
		$response = ob_get_clean();
91
		return Response::output( $response )->withStatus( 500 );
92
	}
93
94
	/**
95
	 * {@inheritDoc}
96
	 * @throws PhpException
97
	 */
98 4
	public function getResponse( PhpException $exception ) {
99 4
		$response = $this->toResponse( $exception );
100
101 4
		if ( $response !== false ) {
102 1
			return $response;
103
		}
104
105 3
		if ( ! $this->debug ) {
106 1
			return Response::error( 500 );
107
		}
108
109 2
		if ( $this->whoops !== null ) {
110 1
			return $this->toPrettyErrorResponse( $exception );
111
		}
112
113 1
		throw $exception;
114
	}
115
}
116