ErrorHandler::handleShutdown()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
/**
4
 * This file is part of coisa/error-handler.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\ErrorHandler;
15
16
use CoiSA\ErrorHandler\Handler\PhpErrorHandlerInterface;
17
use CoiSA\ErrorHandler\Handler\PhpLastErrorShutdownHandler;
18
use CoiSA\ErrorHandler\Handler\ShutdownHandlerInterface;
19
use CoiSA\ErrorHandler\Handler\ThrowableHandlerInterface;
20
use CoiSA\ErrorHandler\Handler\ThrowErrorExceptionPhpErrorHandler;
21
22
/**
23
 * Class ErrorHandler
24
 *
25
 * @package CoiSA\ErrorHandler
26
 */
27
class ErrorHandler implements
28
    ErrorHandlerInterface,
29
    ThrowableHandlerInterface,
30
    PhpErrorHandlerInterface,
31
    ShutdownHandlerInterface
32
{
33
    /**
34
     * @var PhpErrorHandlerInterface
35
     */
36
    private $phpErrorHandler;
37
38
    /**
39
     * @var ThrowableHandlerInterface
40
     */
41
    private $trowableHandler;
42
43
    /**
44
     * @var ShutdownHandlerInterface
45
     */
46
    private $shutdownHandler;
47
48
    /**
49
     * @var bool
50
     */
51
    private $isRegistered = false;
52
53
    /**
54
     * ErrorHandler constructor.
55
     *
56
     * @param ThrowableHandlerInterface     $throwableHandler
57
     * @param null|PhpErrorHandlerInterface $phpErrorHandler
58
     * @param null|ShutdownHandlerInterface $shutdownHandler
59
     */
60 18
    public function __construct(
61
        ThrowableHandlerInterface $throwableHandler,
62
        PhpErrorHandlerInterface $phpErrorHandler = null,
63
        ShutdownHandlerInterface $shutdownHandler = null
64
    ) {
65 18
        $this->trowableHandler = $throwableHandler;
66 18
        $this->phpErrorHandler = $phpErrorHandler ?? new ThrowErrorExceptionPhpErrorHandler();
67 18
        $this->shutdownHandler = $shutdownHandler ?? new PhpLastErrorShutdownHandler($this);
68 18
    }
69
70
    /**
71
     * Register error handler
72
     */
73 8
    public function register(): void
74
    {
75 8
        if ($this->isRegistered) {
76 1
            return;
77
        }
78
79 8
        \set_error_handler([$this, 'handlePhpError']);
80 8
        \set_exception_handler([$this, 'handleThrowable']);
81 8
        \register_shutdown_function([$this, 'handleShutdown']);
82
83 8
        $this->isRegistered = true;
84 8
    }
85
86
    /**
87
     * Unregister error-handler
88
     */
89 3
    public function unregister(): void
90
    {
91 3
        if (false === $this->isRegistered) {
92 1
            return;
93
        }
94
95 2
        \restore_error_handler();
96 2
        \restore_exception_handler();
97
98 2
        $this->isRegistered = false;
99 2
    }
100
101
    /**
102
     * @param \Throwable $throwable
103
     */
104 5
    public function handleThrowable(\Throwable $throwable): void
105
    {
106 5
        $this->trowableHandler->handleThrowable($throwable);
107 5
    }
108
109
    /**
110
     * @param int    $code
111
     * @param string $message
112
     * @param string $filename
113
     * @param int    $line
114
     *
115
     * @throws \ErrorException
116
     */
117 2
    public function handlePhpError(int $code, string $message, string $filename, int $line): void
118
    {
119
        try {
120 2
            $this->phpErrorHandler->handlePhpError($code, $message, $filename, $line);
121 1
        } catch (\ErrorException $throwable) {
122 1
            $this->handleThrowable($throwable);
123
        }
124 2
    }
125
126
    /**
127
     * Handle shutdown if registered
128
     */
129 2
    public function handleShutdown(): void
130
    {
131 2
        if (false === $this->isRegistered) {
132 1
            return;
133
        }
134
135
        try {
136 1
            $this->shutdownHandler->handleShutdown();
137 1
        } catch (\Throwable $throwable) {
138 1
            $this->handleThrowable($throwable);
139
        }
140 1
    }
141
}
142