Completed
Push — master ( fd38ca...72a345 )
by Jeroen
07:39 queued 05:06
created

Handler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 92
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A registerErrorHandler() 0 4 1
A registerExceptionHandler() 0 4 1
A registerShutdownHandler() 0 4 1
A handleError() 0 4 1
A handleException() 0 9 1
A handleShutdown() 0 8 2
1
<?php
2
3
namespace Protoku\Exceptions;
4
5
use ErrorException;
6
use Protoku\Interfaces\FormatterInterface;
7
8
class Handler
9
{
10
	const ERROR_HANDLER = 'handleError';
11
	const EXCEPTION_HANDLER = 'handleException';
12
	const SHUTDOWN_HANDLER = 'handleShutdown';
13
14
	/**
15
	 * @var FormatterInterface
16
	 */
17
	private $formatter;
18
19
	/**
20
	 * Handler constructor.
21
	 *
22
	 * @param FormatterInterface $formatter
23
	 */
24
	public function __construct(FormatterInterface $formatter)
25
	{
26
		$this->formatter = $formatter;
27
		$this->registerErrorHandler();
28
		$this->registerExceptionHandler();
29
	}
30
31
	/**
32
	 * Register the PHP error handler.
33
	 *
34
	 * @return void
35
	 */
36
	protected function registerErrorHandler()
37
	{
38
		set_error_handler([$this, self::ERROR_HANDLER]);
39
	}
40
41
	/**
42
	 * Register the PHP exception handler.
43
	 *
44
	 * @return void
45
	 */
46
	protected function registerExceptionHandler()
47
	{
48
		set_exception_handler([$this, self::EXCEPTION_HANDLER]);
49
	}
50
51
	/**
52
	 * Register a function for execution on shutdown
53
	 *
54
	 * @return void
55
	 */
56
	protected function registerShutdownHandler()
57
	{
58
		register_shutdown_function(self::SHUTDOWN_HANDLER);
59
	}
60
61
	/**
62
	 * @param int $level
63
	 * @param string $message
64
	 * @param string $file
65
	 * @param int $line
66
	 * @param array $context
67
	 *
68
	 * @throws ErrorException
69
	 */
70
	public function handleError(int $level, string $message, string $file = '', int $line = 0, array $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
	{
72
		throw new ErrorException($message, 0, $level, $file, $line);
73
	}
74
75
	/**
76
	 * @param \Throwable $e
77
	 */
78
	public function handleException(\Throwable $e)
79
	{
80
		ob_start();
81
		$response = json_encode($this->formatter->format($e));
82
		ob_end_clean();
83
		http_response_code(500);
84
		print $response;
85
		return;
86
	}
87
88
	/**
89
	 *
90
	 */
91
	public function handleShutdown()
92
	{
93
		$error = error_get_last();
94
95
		if ($error) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
96
			$this->handleException(new ErrorException($error['type'], $error['message'], $error['file'], $error['line']));
97
		}
98
	}
99
}