EventCollector::collect()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 2
nop 2
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
crap 4.0466
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use ReflectionClass;
8
use Yiisoft\Yii\Console\Event\ApplicationStartup as ConsoleApplicationStartup;
9
use Yiisoft\Yii\Http\Event\ApplicationStartup as HttpApplicationStartup;
10
11
final class EventCollector implements SummaryCollectorInterface
12
{
13
    use CollectorTrait;
14
15
    private array $events = [];
16
17
    public function __construct(
18
        private readonly TimelineCollector $timelineCollector
19 1
    ) {
20
    }
21 1
22
    public function getCollected(): array
23
    {
24 1
        if (!$this->isActive()) {
25
            return [];
26
        }
27
        return $this->events;
28
    }
29 1
30
    public function collect(object $event, string $line): void
31
    {
32
        if (
33
            !$event instanceof HttpApplicationStartup
34 1
            && !$event instanceof ConsoleApplicationStartup
35
            && !$this->isActive()
36
        ) {
37 1
            return;
38
        }
39 1
40 1
        $this->events[] = [
41
            'name' => $event::class,
42 1
            'event' => $event,
43
            'file' => (new ReflectionClass($event))->getFileName(),
44 1
            'line' => $line,
45
            'time' => microtime(true),
46
        ];
47
        $this->timelineCollector->collect($this, spl_object_id($event), $event::class);
48 1
    }
49
50
    public function getSummary(): array
51
    {
52 1
        if (!$this->isActive()) {
53
            return [];
54
        }
55
        return [
56
            'event' => [
57 1
                'total' => count($this->events),
58
            ],
59 1
        ];
60
    }
61
62
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
63
    {
64
        $this->events = [];
65
    }
66
}
67