Completed
Push — develop ( d07230...dec153 )
by Schlaefer
05:14 queued 02:44
created

ErrorLog::log()   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 4
crap 2
1
<?php
2
/**
3
 * The Error Handler
4
 */
5
6
namespace Phile\Plugin\Phile\ErrorHandler;
7
8
use Phile\ServiceLocator\ErrorHandlerInterface;
9
10
/**
11
 * Class ErrorLog
12
 */
13
class ErrorLog implements ErrorHandlerInterface
14
{
15
    /**
16
     * handle the error
17
     *
18
     * @param int    $errno
19
     * @param string $errstr
20
     * @param string $errfile
21
     * @param int    $errline
22
     *
23
     * @return boolean
24
     */
25
    public function handleError(int $errno, string $errstr, ?string $errfile, ?string $errline)
26
    {
27
        $this->log($errno, $errstr, $errfile, $errline);
28
    }
29
30
    /**
31
     * handle all exceptions
32
     *
33
     * @param \Exception $exception
34
     *
35
     * @return mixed
36
     */
37
    public function handleException(\Throwable $exception)
38
    {
39
        $code = (int)$exception->getCode();
40
        $message = $exception->getMessage();
41
        $file = $exception->getFile();
42
        $line = $exception->getLine();
43
        $this->log($code, $message, $file, $line);
44
    }
45
46
    public function handleShutdown()
47
    {
48
        $error = error_get_last();
49
        if ($error === null) {
50
            return;
51
        }
52
        $this->log($error['type'], $error['message'], $error['file'], $error['line']);
53
    }
54
55
    protected function log(int $code, string $message, ?string $file, ?string $line): void
56
    {
57
        error_log("[{$code}] {$message} in {$file} on line {$line}");
58
    }
59
}
60