Completed
Push — master ( 293c05...417c40 )
by Jeroen
16:45
created

Handler::handleError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 5
crap 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
13
	/**
14
	 * @var FormatterInterface
15
	 */
16
	private $formatter;
17
18
	/**
19
	 * Handler constructor.
20
	 *
21
	 * @param FormatterInterface $formatter
22
	 */
23
	public function __construct(FormatterInterface $formatter)
24
	{
25
		$this->formatter = $formatter;
26
	}
27
28
	/**
29
	 * Register the PHP error handler.
30
	 *
31
	 * @return void
32
	 */
33
	protected function registerErrorHandler()
34
	{
35
		set_error_handler(array($this, self::ERROR_HANDLER));
36
	}
37
38
	/**
39
	 * Register the PHP exception handler.
40
	 *
41
	 * @return void
42
	 */
43
	protected function registerExceptionHandler()
44
	{
45
		set_exception_handler(array($this, self::EXCEPTION_HANDLER));
46
	}
47
48
	/**
49
	 * @param int $level
50
	 * @param string $message
51
	 * @param string $file
52
	 * @param int $line
53
	 * @param array $context
54
	 *
55
	 * @throws ErrorException
56
	 */
57
	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...
58
	{
59
		throw new ErrorException($message, 0, $level, $file, $line);
60
	}
61
62
	public function handleException(\Throwable $e)
63
	{
64
		ob_start();
65
		$response = $this->formatter->format($e);
66
		ob_end_clean();
67
		http_response_code(500);
68
		print $response;
69
		return;
70
	}
71
}