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

LogHandler   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 91.18%

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 1
dl 0
loc 57
ccs 31
cts 34
cp 0.9118
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 14 2
D resolveLogLevel() 0 29 17
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