Completed
Pull Request — master (#1)
by Matthew
01:34
created

ErrorHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.24%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 101
ccs 20
cts 21
cp 0.9524
rs 10
c 2
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A register() 0 6 1
A handle() 0 21 4
A setExceptionHandler() 0 6 1
A setErrorHandler() 0 10 3
A handleFatalError() 0 10 2
1
<?php
2
3
namespace Fyuze\Error;
4
5
use Closure;
6
use ErrorException;
7
use Exception;
8
use Fyuze\Error\Handlers\Exception as ExceptionHandler;
9
10
class ErrorHandler implements ErrorHandling
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $handlers = [];
16
17
    /**
18
     * @codeCoverageIgnore
19
     */
20
    public function __construct()
21
    {
22
        register_shutdown_function($this->handleFatalError());
23
        set_error_handler($this->setErrorHandler());
24
        set_exception_handler($this->setExceptionHandler());
25
26
        $this->register('Exception', function (Exception $exception) {
27
            $handler = new ExceptionHandler($exception);
28
            $handler->display();
29
        });
30
    }
31
32
    /**
33
     * @param $exception
34
     * @param Closure $handler
35
     * @return $this
36
     */
37 11
    public function register($exception, Closure $handler)
38
    {
39 11
        array_unshift($this->handlers, [$exception, $handler]);
40
41 11
        return $this;
42
    }
43
44
    /**
45
     * @param Exception $exception
46
     *
47
     * @return void
48
     */
49
    public function handle(Exception $exception)
50
    {
51
        try {
52
53 5
            $handlers = array_filter($this->handlers, function ($handler) use ($exception) {
54 5
                return $exception instanceof $handler[0];
55 5
            });
56
57 5
            foreach ($handlers as $handler) {
58 5
                $error = $handler[1]($exception);
59
60 4
                if ($error !== null) {
61 4
                    break;
62
                }
63
            }
64
65 1
        } catch (Exception $e) {
66
67 1
            echo sprintf('[%d] %s in %s', $e->getLine(), $e->getMessage(), $e->getFile());
68
        }
69 5
    }
70
71
    /**
72
     * @return Closure
73
     */
74
    protected function setExceptionHandler()
75
    {
76 11
        return function ($exception) {
77 1
            $this->handle($exception);
78 11
        };
79
    }
80
81
    /**
82
     * @return Closure
83
     * @throws ErrorException
84
     */
85
    protected function setErrorHandler()
86
    {
87 11
        return function ($severity, $message, $file, $line) {
88 1
            if (error_reporting() && $severity) {
89 1
                throw new ErrorException($message, 0, $severity, $file, $line);
90
            }
91
92
            return false;
93 11
        };
94
    }
95
96
    /**
97
     * @return Closure
98
     * @codeCoverageIgnore
99
     */
100
    protected function handleFatalError()
101
    {
102
        return function () {
103
            $error = error_get_last();
104
105
            if ($error['type'] === E_ERROR) {
106
                $this->handle(new ErrorException($error['message']));
107
            }
108
        };
109
    }
110
}
111