LoggerWrapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 71
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 28 3
A log() 0 12 2
1
<?php
2
3
namespace Abacaphiliac\PsrLog4Php;
4
5
use Psr\Log\AbstractLogger;
6
use Psr\Log\LogLevel;
7
8
class LoggerWrapper extends AbstractLogger
9
{
10
    /** @var \Logger */
11
    private $logger;
12
    
13
    /** @var DefaultFormatter */
14
    private $formatter;
15
16
    /** @var int[] */
17
    private $levels;
18
    
19
    /** @var string */
20
    private $defaultLevel;
21
22
    /**
23
     * Log4PhpPsr3Wrapper constructor.
24
     * @param \Logger $logger
25
     * @param FormatterInterface $formatter
26
     * @param int[] $levels
27
     * @param string $defaultLevel
28
     */
29
    public function __construct(
30
        \Logger $logger,
31
        FormatterInterface $formatter = null,
32
        array $levels = array(),
33
        $defaultLevel = LogLevel::ERROR
34
    ) {
35
        if (!$formatter) {
36
            $formatter = new DefaultFormatter();
37
        }
38
        
39
        if (!$levels) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $levels of type integer[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
            $levels = array(
41
                LogLevel::EMERGENCY => \LoggerLevel::FATAL,
42
                LogLevel::ALERT => \LoggerLevel::FATAL,
43
                LogLevel::CRITICAL => \LoggerLevel::FATAL,
44
                LogLevel::ERROR => \LoggerLevel::ERROR,
45
                LogLevel::WARNING => \LoggerLevel::WARN,
46
                LogLevel::NOTICE => \LoggerLevel::WARN,
47
                LogLevel::INFO => \LoggerLevel::INFO,
48
                LogLevel::DEBUG => \LoggerLevel::DEBUG,
49
            );
50
        }
51
        
52
        $this->logger = $logger;
53
        $this->formatter = $formatter;
54
        $this->levels = $levels;
0 ignored issues
show
Documentation Bug introduced by
It seems like $levels of type array is incompatible with the declared type array<integer,integer> of property $levels.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
        $this->defaultLevel = $defaultLevel;
56
    }
57
58
    /**
59
     * Logs with an arbitrary level.
60
     *
61
     * @param mixed $level
62
     * @param mixed $message
63
     * @param mixed[] $context
64
     * @return null
65
     */
66
    public function log($level, $message, array $context = array())
67
    {
68
        if (!array_key_exists($level, $this->levels)) {
69
            $level = $this->defaultLevel;
70
        }
71
        
72
        $level = \LoggerLevel::toLevel($this->levels[$level], $this->defaultLevel);
73
        
74
        $message = $this->formatter->format($level, $message, $context);
75
76
        $this->logger->log($level, $message);
77
    }
78
}
79