Completed
Push — master ( fa7c53...535778 )
by Matze
04:38
created

ChannelStreamHandler::isHandling()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 8.6737
cc 5
eloc 11
nc 5
nop 1
crap 5
1
<?php
2
3
namespace BrainExe\Core\Logger;
4
5
use Monolog\Handler\StreamHandler;
6
use Monolog\Logger;
7
8
class ChannelStreamHandler extends StreamHandler
9
{
10
11
    /**
12
     * @var string
13
     */
14
    private $channel;
15
16
    /**
17
     * @param string $stream
18
     * @param boolean|integer $level
19
     * @param string|null $channel
20
     * @param boolean $bubble
21
     */
22 9
    public function __construct(
23
        $stream,
24
        $level = Logger::DEBUG,
25
        $channel = null,
26
        $bubble = true
27
    ) {
28 9
        parent::__construct($stream, $level, $bubble);
0 ignored issues
show
Bug introduced by
It seems like $level defined by parameter $level on line 24 can also be of type boolean; however, Monolog\Handler\StreamHandler::__construct() does only seem to accept integer, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
29
30 9
        $formatter = new Formatter(
31 9
            "[%datetime%] %level_name%: %message% %context% %extra%\n",
32 9
            null,
33 9
            false,
34 9
            true
35
        );
36
37 9
        $this->setFormatter(
38
            $formatter
39
        );
40
41 9
        $this->channel = $channel;
42 9
    }
43
44
    /**
45
     * @param string $channel
46
     */
47 6
    public function setChannel($channel)
48
    {
49 6
        $this->channel = $channel;
50 6
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 8
    public function isHandling(array $record)
56
    {
57 8
        if (count($record) == 1) {
58 3
            return parent::isHandling($record);
59
        }
60
61 7
        if (!parent::isHandling($record)) {
62 4
            return false;
63
        }
64
65 5
        if (empty($this->channel)) {
66 3
            return empty($record['context']['channel']);
67
        }
68
69 2
        if (empty($record['context']['channel'])) {
70
            return false;
71 2
        }
72
73 2
        $supported = $this->channel === $record['context']['channel'];
74
75
        return $supported;
76
    }
77
}
78