Test Failed
Push — master ( 6713c8...31ff09 )
by Rustam
51:08
created

WebAppInfoCollector::collect()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.1782

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
nc 6
nop 1
dl 0
loc 21
ccs 11
cts 13
cp 0.8462
crap 7.1782
rs 8.8333
c 1
b 0
f 0
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 1
    private float $requestProcessingTimeStopped = 0;
22
    private float $routeMatchTime = 0;
23
24 1
    public function __construct(ProfilerInterface $profiler)
25 1
    {
26 1
        $this->profiler = $profiler;
27 1
    }
28 1
    public function getCollected(): array
29 1
    {
30
        return [
31
            'application_processing_time' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,
32
            'application_preload' => $this->requestProcessingTimeStarted - $this->applicationProcessingTimeStarted,
33 1
            'request_processing_time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
34
            'application_emit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped,
35 1
            'memory_peak_usage' => memory_get_peak_usage(),
36
            'memory_usage' => memory_get_usage(),
37
        ];
38
    }
39 1
40 1
    public function collect(object $event): void
41 1
    {
42 1
        if (!is_object($event) || !$this->isActive()) {
43 1
            return;
44
        }
45 1
46 1
        if ($event instanceof BeforeRequest) {
47
            $this->requestProcessingTimeStarted = microtime(true);
48
            $this->applicationProcessingTimeStarted = $event->getRequest()->getAttribute(
49
                'applicationStartTime',
50 1
                $this->requestProcessingTimeStarted
51
            );
52 1
        } elseif ($event instanceof AfterRequest) {
53
            $this->requestProcessingTimeStopped = microtime(true);
54
            [$message] = $this->profiler->findMessages('Matching route');
0 ignored issues
show
Bug introduced by
The method findMessages() does not exist on Yiisoft\Profiler\ProfilerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            /** @scrutinizer ignore-call */ 
55
            [$message] = $this->profiler->findMessages('Matching route');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55 1
            if ($message !== null) {
56 1
                $this->routeMatchTime = $message->context('duration', 0);
57 1
            }
58
59
        } elseif ($event instanceof AfterEmit) {
60
            $this->applicationProcessingTimeStopped = microtime(true);
61 1
        }
62
    }
63 1
64 1
    public function getIndexData(): array
65 1
    {
66 1
        return [
67 1
            'time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
68
            'memory' => memory_get_peak_usage(),
69
            'timestamp' => $this->requestProcessingTimeStarted,
70
            'routeTime' => $this->routeMatchTime,
71
        ];
72
    }
73
74
    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
        $this->applicationProcessingTimeStarted = 0;
77
        $this->applicationProcessingTimeStopped = 0;
78
        $this->requestProcessingTimeStarted = 0;
79
        $this->requestProcessingTimeStopped = 0;
80
    }
81
}
82