Test Failed
Push — main ( ad999b...e26c51 )
by Rafael
06:04
created

MonologCollector::collect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the DebugBar package.
4
 *
5
 * (c) 2013 Maxime Bouroumeau-Fuseau
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Alxarafe\Core\Singletons\DebugBarCollectors;
12
13
use DebugBar\DataCollector\DataCollectorInterface;
14
use DebugBar\DataCollector\MessagesAggregateInterface;
15
use DebugBar\DataCollector\Renderable;
16
use Monolog\Handler\AbstractProcessingHandler;
17
use Monolog\Logger;
18
19
/**
20
 * A monolog handler as well as a data collector
21
 *
22
 * https://github.com/Seldaek/monolog
23
 *
24
 * <code>
25
 * $debugbar->addCollector(new MonologCollector($logger));
26
 * </code>
27
 */
28
class MonologCollector extends AbstractProcessingHandler implements DataCollectorInterface, Renderable, MessagesAggregateInterface
29
{
30
    protected $name;
31
32
    protected $records = array();
33
34
    /**
35
     * @param Logger $logger
36
     * @param int $level
37
     * @param boolean $bubble
38
     * @param string $name
39
     */
40
    public function __construct(Logger $logger = null, $level = Logger::DEBUG, $bubble = true, $name = 'monolog')
41
    {
42
        parent::__construct($level, $bubble);
43
        $this->name = $name;
44
        if ($logger !== null) {
45
            $this->addLogger($logger);
46
        }
47
    }
48
49
    /**
50
     * Adds logger which messages you want to log
51
     *
52
     * @param Logger $logger
53
     */
54
    public function addLogger(Logger $logger)
55
    {
56
        $logger->pushHandler($this);
57
    }
58
59
    /**
60
     * @param array $record
61
     */
62
    protected function write(array $record): void
63
    {
64
        $this->records[] = array(
65
            'message' => $record['formatted'],
66
            'is_string' => true,
67
            'label' => strtolower($record['level_name']),
68
            'time' => $record['datetime']->format('U')
69
        );
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function getMessages()
76
    {
77
        return $this->records;
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function collect()
84
    {
85
        return array(
86
            'count' => count($this->records),
87
            'records' => $this->records
88
        );
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getName()
95
    {
96
        return $this->name;
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getWidgets()
103
    {
104
        $name = $this->getName();
105
        return array(
106
            $name => array(
107
                "icon" => "suitcase",
108
                "widget" => "PhpDebugBar.Widgets.MessagesWidget",
109
                "map" => "$name.records",
110
                "default" => "[]"
111
            ),
112
            "$name:badge" => array(
113
                "map" => "$name.count",
114
                "default" => "null"
115
            )
116
        );
117
    }
118
}
119