Passed
Push — master ( 3312bd...c25b5e )
by Kirill
04:35
created

EventHandler::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 12
rs 10
c 1
b 0
f 0
eloc 8
nc 2
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Monolog;
13
14
use Monolog\Handler\AbstractHandler;
15
use Monolog\Logger;
16
use Spiral\Logger\Event\LogEvent;
17
use Spiral\Logger\ListenerRegistryInterface;
18
19
final class EventHandler extends AbstractHandler
20
{
21
    /** @var ListenerRegistryInterface */
22
    private $listenerRegistry;
23
24
    /**
25
     * @param ListenerRegistryInterface $listenerRegistry
26
     * @param int                       $level
27
     * @param bool                      $bubble
28
     */
29
    public function __construct(
30
        ListenerRegistryInterface $listenerRegistry,
31
        int $level = Logger::DEBUG,
32
        bool $bubble = true
33
    ) {
34
        $this->listenerRegistry = $listenerRegistry;
35
36
        parent::__construct($level, $bubble);
37
    }
38
39
    /**
40
     * @param array $record
41
     * @return bool|void
42
     */
43
    public function handle(array $record)
44
    {
45
        $e = new LogEvent(
46
            $record['datetime'],
47
            $record['channel'],
48
            strtolower(Logger::getLevelName($record['level'])),
49
            $record['message'],
50
            $record['context']
51
        );
52
53
        foreach ($this->listenerRegistry->getListeners() as $listener) {
54
            call_user_func($listener, $e);
55
        }
56
    }
57
}
58