Completed
Push — master ( f4be61...d5c601 )
by Nikola
08:05
created

LogHandler::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.0078
1
<?php
2
/**
3
 * This file is part of the Phoundation package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Phoundation\ErrorHandling\Handler;
14
15
use Psr\Log\LoggerInterface;
16
use Throwable;
17
use Phoundation\Exception\DontLogInterface;
18
use Psr\Log\LogLevel;
19
20
/**
21
 * @author Nikola Posa <[email protected]>
22
 */
23
final class LogHandler
24
{
25
    /**
26
     * @var LoggerInterface
27
     */
28
    private $logger;
29
30 7
    public function __construct(LoggerInterface $logger)
31
    {
32 7
        $this->logger = $logger;
33 7
    }
34
35 7
    public function __invoke(Throwable $e)
36
    {
37 7
        if ($e instanceof DontLogInterface) {
38
            return;
39
        }
40
41 7
        $logLevel = $this->resolveLogLevel($e);
42 7
        $message = $e->getMessage();
43
        $context = [
44 7
            'exception' => $e,
45
        ];
46
47 7
        $this->logger->log($logLevel, $message, $context);
48 7
    }
49
50 7
    private function resolveLogLevel(Throwable $e)
51
    {
52 7
        if ($e instanceof \ErrorException) {
1 ignored issue
show
Bug introduced by
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
53 4
            switch ($e->getSeverity()) {
54 4
                case E_ERROR:
55 4
                case E_RECOVERABLE_ERROR:
56 4
                case E_CORE_ERROR:
57 3
                case E_COMPILE_ERROR:
58 3
                case E_USER_ERROR:
59 3
                case E_PARSE:
60 1
                    return LogLevel::ERROR;
61 3
                case E_WARNING:
62 3
                case E_USER_WARNING:
63 2
                case E_CORE_WARNING:
64 2
                case E_COMPILE_WARNING:
65 1
                    return LogLevel::WARNING;
66 2
                case E_NOTICE:
67 1
                case E_USER_NOTICE:
68 1
                case E_STRICT:
69 1
                case E_DEPRECATED:
70
                case E_USER_DEPRECATED:
71 2
                    return LogLevel::NOTICE;
72
                default:
73
                    break;
74
            }
75
        }
76
77 3
        return LogLevel::ERROR;
78
    }
79
}
80