ChannelStreamHandler::setChannel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 bool|int $level
19
     * @param string|null $channel
20
     * @param bool $bubble
21
     */
22 9
    public function __construct(
23
        string $stream,
24
        int $level = Logger::DEBUG,
25
        $channel = null,
26
        $bubble = true
27
    ) {
28 9
        parent::__construct($stream, $level, $bubble);
29
30 9
        $formatter = new Formatter(
31 9
            null,
32 9
            null,
33 9
            false,
34 9
            true
35
        );
36
37 9
        $this->setFormatter(
38 9
            $formatter
39
        );
40
41 9
        $this->channel = $channel;
42 9
    }
43
44
    /**
45
     * @param string $channel
46
     */
47 7
    public function setChannel(string $channel)
48
    {
49 7
        $this->channel = $channel;
50 7
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 7
    public function isHandling(array $record) : bool
56
    {
57 7
        if (count($record) == 1) {
58 1
            return parent::isHandling($record);
59
        }
60
61 6
        if (!parent::isHandling($record)) {
62 1
            return false;
63
        }
64
65 5
        if (empty($this->channel)) {
66 2
            return empty($record['context']['channel']);
67
        }
68
69 3
        if (empty($record['context']['channel'])) {
70 1
            return false;
71
        }
72
73 2
        $supported = $this->channel === $record['context']['channel'];
74
75 2
        return $supported;
76
    }
77
}
78