EventsCollector   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 31
c 1
b 1
f 0
dl 0
loc 92
ccs 0
cts 14
cp 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A icon() 0 3 1
A formatTimelineData() 0 16 2
A display() 0 28 4
A getBadgeValue() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Debug\Toolbar\Collectors;
13
14
use BlitzPHP\Event\EventManager;
15
16
/**
17
 * Collecteur pour l'onglet "Evenements" de la barre d'outils de débogage.
18
 *
19
 * @credit	<a href="https://codeigniter.com">CodeIgniter 4.2 - CodeIgniter\Debug\Toolbar\Collectors\Events</a>
20
 */
21
class EventsCollector extends BaseCollector
22
{
23
    /**
24
     * {@inheritDoc}
25
     */
26
    protected bool $hasTimeline = true;
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    protected bool $hasTabContent = true;
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    protected bool $hasVarData = false;
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    protected string $title = 'Evenements';
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    protected function formatTimelineData(): array
47
    {
48
        $data = [];
49
50
        $rows = EventManager::getPerformanceLogs();
51
52
        foreach ($rows as $info) {
53
            $data[] = [
54
                'name'      => 'Evenement: ' . $info['event'],
55
                'component' => 'Events',
56
                'start'     => $info['start'],
57
                'duration'  => $info['end'] - $info['start'],
58
            ];
59
        }
60
61
        return $data;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function display(): array
68
    {
69
        $data = [
70
            'events' => [],
71
        ];
72
73
        foreach (EventManager::getPerformanceLogs() as $row) {
74
            $key = $row['event'];
75
76
            if (! array_key_exists($key, $data['events'])) {
77
                $data['events'][$key] = [
78
                    'event'    => $key,
79
                    'duration' => ($row['end'] - $row['start']) * 1000,
80
                    'count'    => 1,
81
                ];
82
83
                continue;
84
            }
85
86
            $data['events'][$key]['duration'] += ($row['end'] - $row['start']) * 1000;
87
            $data['events'][$key]['count']++;
88
        }
89
90
        foreach ($data['events'] as &$row) {
91
            $row['duration'] = number_format($row['duration'], 2);
92
        }
93
94
        return $data;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function getBadgeValue(): int
101
    {
102
        return count(EventManager::getPerformanceLogs());
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     *
108
     * Icon from https://icons8.com - 1em package
109
     */
110
    public function icon(): string
111
    {
112
        return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEASURBVEhL7ZXNDcIwDIVTsRBH1uDQDdquUA6IM1xgCA6MwJUN2hk6AQzAz0vl0ETUxC5VT3zSU5w81/mRMGZysixbFEVR0jSKNt8geQU9aRpFmp/keX6AbjZ5oB74vsaN5lSzA4tLSjpBFxsjeSuRy4d2mDdQTWU7YLbXTNN05mKyovj5KL6B7q3hoy3KwdZxBlT+Ipz+jPHrBqOIynZgcZonoukb/0ckiTHqNvDXtXEAaygRbaB9FvUTjRUHsIYS0QaSp+Dw6wT4hiTmYHOcYZsdLQ2CbXa4ftuuYR4x9vYZgdb4vsFYUdmABMYeukK9/SUme3KMFQ77+Yfzh8eYF8+orDuDWU5LAAAAAElFTkSuQmCC';
113
    }
114
}
115