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

LogHandler::resolveLogLevel()   D

Complexity

Conditions 17
Paths 17

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 17.1903

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 21
cts 23
cp 0.913
rs 4.9807
c 0
b 0
f 0
cc 17
eloc 24
nc 17
nop 1
crap 17.1903

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
 * 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