MiddlewareCollector   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A reset() 0 4 1
B collect() 0 35 6
A getCollected() 0 19 4
A getActionHandler() 0 9 1
A getSummary() 0 8 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Web;
6
7
use ReflectionClass;
8
use Yiisoft\Middleware\Dispatcher\Event\AfterMiddleware;
9
use Yiisoft\Middleware\Dispatcher\Event\BeforeMiddleware;
10
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
11
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
12
use Yiisoft\Yii\Debug\Collector\TimelineCollector;
13
14
final class MiddlewareCollector implements SummaryCollectorInterface
15
{
16
    use CollectorTrait;
17
18
    private array $beforeStack = [];
19
    private array $afterStack = [];
20
21
    public function __construct(
22
        private readonly TimelineCollector $timelineCollector
23
    ) {
24
    }
25
26
    public function getCollected(): array
27
    {
28
        if (!$this->isActive()) {
29
            return [];
30
        }
31
        $beforeStack = $this->beforeStack;
32
        $afterStack = $this->afterStack;
33
        $beforeAction = array_pop($beforeStack);
34
        $afterAction = array_shift($afterStack);
35
        $actionHandler = [];
36
37
        if ($beforeAction !== null && $afterAction !== null) {
38
            $actionHandler = $this->getActionHandler($beforeAction, $afterAction);
39
        }
40
41
        return [
42
            'beforeStack' => $beforeStack,
43
            'actionHandler' => $actionHandler,
44
            'afterStack' => $afterStack,
45
        ];
46
    }
47
48
    public function collect(BeforeMiddleware|AfterMiddleware $event): void
49
    {
50
        if (!$this->isActive()) {
51
            return;
52
        }
53
54
        if (
55
            method_exists($event->getMiddleware(), '__debugInfo')
56
            && (new ReflectionClass($event->getMiddleware()))->isAnonymous()
57
        ) {
58
            $callback = $event->getMiddleware()->__debugInfo()['callback'];
59
            if (is_array($callback)) {
60
                $name = implode('::', $callback);
61
            } else {
62
                $name = 'object(Closure)#' . spl_object_id($callback);
63
            }
64
        } else {
65
            $name = $event->getMiddleware()::class;
66
        }
67
        if ($event instanceof BeforeMiddleware) {
68
            $this->beforeStack[] = [
69
                'name' => $name,
70
                'time' => microtime(true),
71
                'memory' => memory_get_usage(),
72
                'request' => $event->getRequest(),
73
            ];
74
        } else {
75
            $this->afterStack[] = [
76
                'name' => $name,
77
                'time' => microtime(true),
78
                'memory' => memory_get_usage(),
79
                'response' => $event->getResponse(),
80
            ];
81
        }
82
        $this->timelineCollector->collect($this, spl_object_id($event));
83
    }
84
85
    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...
86
    {
87
        $this->beforeStack = [];
88
        $this->afterStack = [];
89
    }
90
91
    public function getSummary(): array
92
    {
93
        if (!$this->isActive()) {
94
            return [];
95
        }
96
        return [
97
            'middleware' => [
98
                'total' => ($total = count($this->beforeStack)) > 0 ? $total - 1 : 0, // Remove action handler
99
            ],
100
        ];
101
    }
102
103
    private function getActionHandler(array $beforeAction, array $afterAction): array
104
    {
105
        return [
106
            'name' => $beforeAction['name'],
107
            'startTime' => $beforeAction['time'],
108
            'request' => $beforeAction['request'],
109
            'response' => $afterAction['response'],
110
            'endTime' => $afterAction['time'],
111
            'memory' => $afterAction['memory'],
112
        ];
113
    }
114
}
115