Completed
Push — master ( 36c690...2340c9 )
by David
33s queued 30s
created

LevelFilter::log()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 3
nc 3
nop 3
1
<?php
2
3
4
namespace TheCodingMachine\TDBM\Utils\Logs;
5
6
use Psr\Log\AbstractLogger;
7
use Psr\Log\InvalidArgumentException;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\LogLevel;
10
use function array_search;
11
use function sprintf;
12
13
class LevelFilter extends AbstractLogger
14
{
15
    /**
16
     * Logging levels from syslog protocol defined in RFC 5424
17
     *
18
     * @var string[] $levels Logging levels
19
     */
20
    private static $levels = array(
21
        LogLevel::EMERGENCY, // 0
22
        LogLevel::ALERT,     // 1
23
        LogLevel::CRITICAL,  // 2
24
        LogLevel::ERROR,     // 3
25
        LogLevel::WARNING,   // 4
26
        LogLevel::NOTICE,    // 5
27
        LogLevel::INFO,      // 6
28
        LogLevel::DEBUG      // 7
29
    );
30
31
    /**
32
     * @var int
33
     */
34
    private $logLevel;
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    /**
41
     * @param LoggerInterface $logger
42
     * @param string $level \Psr\Log\LogLevel string
43
     */
44
    public function __construct(LoggerInterface $logger, $level)
45
    {
46
        $this->logger = $logger;
47
48
        $this->logLevel = array_search($level, self::$levels, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_search($level, self::levels, true) can also be of type false or string. However, the property $logLevel is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
        if ($this->logLevel === false) {
50
            throw new InvalidArgumentException(
51
                sprintf(
52
                    'Cannot use logging level "%s"',
53
                    $level
54
                )
55
            );
56
        }
57
    }
58
59
    /**
60
     * Logs with an arbitrary level.
61
     *
62
     * @param mixed  $level
63
     * @param string $message
64
     * @param array  $context
65
     *
66
     * @return void
67
     */
68
    public function log($level, $message, array $context = array())
69
    {
70
        $levelCode = array_search($level, self::$levels, true);
71
        if ($levelCode === false) {
72
            throw new InvalidArgumentException(
73
                sprintf(
74
                    'Cannot use unknown logging level "%s"',
75
                    $level
76
                )
77
            );
78
        }
79
        if ($levelCode > $this->logLevel) {
80
            return;
81
        }
82
83
        $this->logger->log($level, $message, $context);
84
    }
85
}
86