ChannelStreamHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 70
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setChannel() 0 4 1
B isHandling() 0 22 5
A __construct() 0 21 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