Completed
Push — master ( 83dee0...9c2066 )
by Márk
05:19
created

LogHandler::translateError()   C

Complexity

Conditions 16
Paths 16

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 5.0151
c 0
b 0
f 0
cc 16
eloc 23
nc 16
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nofw\Foundation\Whoops\Handler;
6
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LogLevel;
9
use Whoops\Handler\Handler;
10
11
/**
12
 * @author Márk Sági-Kazár <[email protected]>
13
 */
14
final class LogHandler extends Handler
15
{
16
    /**
17
     * @var LoggerInterface
18
     */
19
    private $logger;
20
21
    public function __construct(LoggerInterface $logger)
22
    {
23
        $this->logger = $logger;
24
    }
25
26
    public function handle(): int
27
    {
28
        $exception = $this->getException();
29
        $level = LogLevel::CRITICAL;
30
31
        if ($exception instanceof \ErrorException) {
32
            $level = $this->translateError($exception);
33
        }
34
35
        $this->logger->log($level, $exception->getMessage().$exception->getTraceAsString());
36
37
        return Handler::DONE;
38
    }
39
40
    /**
41
     * Translates an error exception into a log level.
42
     */
43
    private function translateError(\ErrorException $exception): string
44
    {
45
        switch ($exception->getSeverity()) {
46
            case E_ERROR:
47
            case E_RECOVERABLE_ERROR:
48
            case E_CORE_ERROR:
49
            case E_COMPILE_ERROR:
50
            case E_USER_ERROR:
51
            case E_PARSE:
52
                return LogLevel::ERROR;
53
54
            case E_WARNING:
55
            case E_USER_WARNING:
56
            case E_CORE_WARNING:
57
            case E_COMPILE_WARNING:
58
                return LogLevel::WARNING;
59
60
            case E_NOTICE:
61
            case E_USER_NOTICE:
62
                return LogLevel::NOTICE;
63
64
            case E_STRICT:
65
            case E_DEPRECATED:
66
            case E_USER_DEPRECATED:
67
                return LogLevel::INFO;
68
69
            default:
70
                return LogLevel::CRITICAL;
71
        }
72
    }
73
}
74