Completed
Push — master ( d94b70...ffe231 )
by Nikola
01:55
created

LogHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 74
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 14 2
A shouldLog() 0 10 3
A resolveLogLevel() 0 8 2
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
    /**
31
     * @var array
32
     */
33
    private $dontLog;
34
35
    /**
36
     * @var array
37
     */
38
    private static $errorSeverityLogLevelMap = [
39
        E_ERROR => LogLevel::ERROR,
40
        E_RECOVERABLE_ERROR => LogLevel::ERROR,
41
        E_CORE_ERROR => LogLevel::ERROR,
42
        E_COMPILE_ERROR => LogLevel::ERROR,
43
        E_USER_ERROR => LogLevel::ERROR,
44
        E_PARSE => LogLevel::ERROR,
45
        E_WARNING => LogLevel::WARNING,
46
        E_USER_WARNING => LogLevel::WARNING,
47
        E_CORE_WARNING => LogLevel::WARNING,
48
        E_COMPILE_WARNING => LogLevel::WARNING,
49
        E_NOTICE => LogLevel::NOTICE,
50
        E_USER_NOTICE => LogLevel::NOTICE,
51
        E_STRICT => LogLevel::NOTICE,
52
        E_DEPRECATED => LogLevel::NOTICE,
53
        E_USER_DEPRECATED => LogLevel::NOTICE,
54
    ];
55
56 8
    public function __construct(LoggerInterface $logger, array $dontLog = [])
57
    {
58 8
        $this->logger = $logger;
59 8
        $this->dontLog = array_merge($dontLog, [DontLogInterface::class]);
60 8
    }
61
62 8
    public function __invoke(Throwable $e)
63
    {
64 8
        if (!$this->shouldLog($e)) {
65 1
            return;
66
        }
67
68 7
        $logLevel = $this->resolveLogLevel($e);
69 7
        $message = $e->getMessage();
70
        $context = [
71 7
            'exception' => $e,
72
        ];
73
74 7
        $this->logger->log($logLevel, $message, $context);
75 7
    }
76
77 8
    private function shouldLog(Throwable $e) : bool
78
    {
79 8
        foreach ($this->dontLog as $type) {
80 8
            if ($e instanceof $type) {
81 8
                return false;
82
            }
83
        }
84
85 7
        return true;
86
    }
87
88 7
    private function resolveLogLevel(Throwable $e)
89
    {
90 7
        if ($e instanceof \ErrorException) {
91 4
            return self::$errorSeverityLogLevelMap[$e->getSeverity()] ?? LogLevel::ERROR;
92
        }
93
94 3
        return LogLevel::ERROR;
95
    }
96
}
97