Passed
Push — master ( 6034b7...b65dcf )
by Ayan
03:03
created

Log   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 3
Bugs 3 Features 0
Metric Value
wmc 25
eloc 41
c 3
b 3
f 0
dl 0
loc 82
ccs 44
cts 46
cp 0.9565
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setLogger() 0 5 1
A __construct() 0 4 2
C handleErrorException() 0 32 16
A buildLogMessage() 0 11 3
A handle() 0 10 3
1
<?php
2
3
namespace Phprest\ErrorHandler\Handler;
4
5
use ErrorException;
6
use Exception;
7
use League\BooBoo\Handler\HandlerInterface;
8
use Phprest\Exception\Exception as PhprestException;
9
use Psr\Log\LoggerInterface;
10
11
class Log implements HandlerInterface
12
{
13
    /**
14
     * @var LoggerInterface|null
15
     */
16
    protected $logger;
17
18 79
    public function __construct(LoggerInterface $logger = null)
19
    {
20 79
        if (null !== $logger) {
21 6
            $this->setLogger($logger);
22
        }
23 79
    }
24
25 14
    public function setLogger(LoggerInterface $logger): self
26
    {
27 14
        $this->logger = $logger;
28
29 14
        return $this;
30
    }
31
32
    /**
33
     * @param Exception $exception
34
     */
35 6
    public function handle($exception): void
36
    {
37 6
        if ($exception instanceof ErrorException) {
38 4
            $this->handleErrorException($exception);
39
40 4
            return;
41
        }
42
43 2
        if ($this->logger) {
44 2
            $this->logger->critical($this->buildLogMessage($exception));
45
        }
46 2
    }
47
48 4
    protected function handleErrorException(ErrorException $exception): bool
49
    {
50 4
        switch ($exception->getSeverity()) {
51 4
            case E_ERROR:
52 3
            case E_RECOVERABLE_ERROR:
53 3
            case E_CORE_ERROR:
54 3
            case E_COMPILE_ERROR:
55 3
            case E_USER_ERROR:
56 3
            case E_PARSE:
57 1
                $this->logger->error($this->buildLogMessage($exception));
0 ignored issues
show
Bug introduced by
The method error() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
                $this->logger->/** @scrutinizer ignore-call */ 
58
                               error($this->buildLogMessage($exception));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58 1
                break;
59
60 3
            case E_WARNING:
61 2
            case E_USER_WARNING:
62 2
            case E_CORE_WARNING:
63 2
            case E_COMPILE_WARNING:
64 1
                $this->logger->warning($this->buildLogMessage($exception));
65 1
                break;
66
67 2
            case E_NOTICE:
68 1
            case E_USER_NOTICE:
69 1
                $this->logger->notice($this->buildLogMessage($exception));
70 1
                break;
71
72 1
            case E_STRICT:
73
            case E_DEPRECATED:
74
            case E_USER_DEPRECATED:
75 1
                $this->logger->info($this->buildLogMessage($exception));
76 1
                break;
77
        }
78
79 4
        return true;
80
    }
81
82 6
    protected function buildLogMessage(Exception $exception): string
83
    {
84 6
        $message = $exception->getMessage() . "({$exception->getCode()})";
85
86 6
        if ($exception instanceof PhprestException && $exception->getDetails()) {
87 1
            $message .= ' Details :: ' . json_encode($exception->getDetails());
88
        }
89
90 6
        $message .= ' Stack trace :: ' . $exception->getTraceAsString();
91
92 6
        return $message;
93
    }
94
}
95