Passed
Push — master ( eaec8c...7391a5 )
by Rustam
04:57
created

WebAppInfoCollector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 38
c 3
b 0
f 0
dl 0
loc 67
ccs 33
cts 36
cp 0.9167
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 6 1
A getCollected() 0 9 1
B collect() 0 20 7
A __construct() 0 3 1
A getIndexData() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use Yiisoft\Profiler\ProfilerInterface;
8
use Yiisoft\Yii\Web\Event\AfterEmit;
9
use Yiisoft\Yii\Web\Event\AfterRequest;
10
use Yiisoft\Yii\Web\Event\BeforeRequest;
11
use function is_object;
12
13
final class WebAppInfoCollector implements CollectorInterface, IndexCollectorInterface
14
{
15
    use CollectorTrait;
16
17
    private ProfilerInterface $profiler;
18
    private float $applicationProcessingTimeStarted = 0;
19
    private float $applicationProcessingTimeStopped = 0;
20
    private float $requestProcessingTimeStarted = 0;
21
    private float $requestProcessingTimeStopped = 0;
22
    private float $routeMatchTime = 0;
23
24 1
    public function __construct(ProfilerInterface $profiler)
25
    {
26 1
        $this->profiler = $profiler;
27 1
    }
28
29 1
    public function getCollected(): array
30
    {
31
        return [
32 1
            'application_processing_time' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,
33 1
            'application_preload' => $this->requestProcessingTimeStarted - $this->applicationProcessingTimeStarted,
34 1
            'request_processing_time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
35 1
            'application_emit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped,
36 1
            'memory_peak_usage' => memory_get_peak_usage(),
37 1
            'memory_usage' => memory_get_usage(),
38
        ];
39
    }
40
41 1
    public function collect(object $event): void
42
    {
43 1
        if (!is_object($event) || !$this->isActive()) {
44
            return;
45
        }
46
47 1
        if ($event instanceof BeforeRequest) {
48 1
            $this->requestProcessingTimeStarted = microtime(true);
49 1
            $this->applicationProcessingTimeStarted = $event->getRequest()->getAttribute(
50 1
                'applicationStartTime',
51 1
                $this->requestProcessingTimeStarted
52
            );
53 1
        } elseif ($event instanceof AfterRequest) {
54 1
            $this->requestProcessingTimeStopped = microtime(true);
55 1
            $message = $this->profiler->findMessages('Matching route')[0] ?? null;
56 1
            if ($message !== null) {
57 1
                $this->routeMatchTime = $message->context('duration', 0);
58
            }
59
        } elseif ($event instanceof AfterEmit) {
60
            $this->applicationProcessingTimeStopped = microtime(true);
61
        }
62 1
    }
63
64 1
    public function getIndexData(): array
65
    {
66
        return [
67 1
            'time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
68 1
            'memory' => memory_get_peak_usage(),
69 1
            'timestamp' => $this->requestProcessingTimeStarted,
70 1
            'routeTime' => $this->routeMatchTime,
71
        ];
72
    }
73
74 1
    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...
75
    {
76 1
        $this->applicationProcessingTimeStarted = 0;
77 1
        $this->applicationProcessingTimeStopped = 0;
78 1
        $this->requestProcessingTimeStarted = 0;
79 1
        $this->requestProcessingTimeStopped = 0;
80 1
    }
81
}
82