Passed
Branch master (f36b3c)
by Matthew
08:01
created

ErrorHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 14
eloc 30
c 4
b 1
f 0
dl 0
loc 99
ccs 23
cts 23
cp 1
rs 10

6 Methods

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