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

ErrorHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 102
rs 10
c 0
b 0
f 0
ccs 13
cts 13
cp 1
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toPrettyErrorResponse() 0 6 1
A getResponse() 0 16 4
A unregister() 0 3 2
A toResponse() 0 12 3
A __construct() 0 3 1
A register() 0 3 2
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