Passed
Push — master ( 755731...71ac2a )
by Dmitriy
02:36
created

HttpClientCollector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use GuzzleHttp\Psr7\Message;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
use function count;
12
13
final class HttpClientCollector implements SummaryCollectorInterface
14
{
15
    use CollectorTrait;
16
17
    /**
18
     * @psalm-var array<string, non-empty-list<array{
19
     *     startTime: float|string,
20
     *     endTime: float|string,
21
     *     totalTime: float,
22
     *     method: string,
23
     *     uri: string,
24
     *     headers: string[][],
25
     *     line: string,
26
     *     responseRaw?: string,
27
     *     responseStatus?: int,
28
     * }>>
29
     */
30
    private array $requests = [];
31
32
    public function __construct(private TimelineCollector $timelineCollector)
33
    {
34
    }
35
36
    public function getCollected(): array
37
    {
38
        return array_merge(...array_values($this->requests));
39
    }
40
41
    public function getSummary(): array
42
    {
43
        return [
44
            'http' => [
45
                'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)),
46
                'totalTime' => array_sum(
47
                    array_merge(
48
                        ...array_map(
49
                            static fn (array $entry) => array_column($entry, 'totalTime'),
50
                            array_values($this->requests)
51
                        )
52
                    )
53
                ),
54
            ],
55
        ];
56
    }
57
58
    public function collect(RequestInterface $request, float|string $startTime, string $line, ?string $uniqueId): void
59
    {
60
        if (!$this->isActive()) {
61
            return;
62
        }
63
64
        $this->requests[$uniqueId][] = [
65
            'startTime' => $startTime,
66
            'endTime' => $startTime,
67
            'totalTime' => 0.0,
68
            'method' => $request->getMethod(),
69
            'uri' => (string) $request->getUri(),
70
            'headers' => $request->getHeaders(),
71
            'line' => $line,
72
        ];
73
        $this->timelineCollector->collect($this, $uniqueId);
0 ignored issues
show
Bug introduced by
It seems like $uniqueId can also be of type null; however, parameter $reference of Yiisoft\Yii\Debug\Collec...ineCollector::collect() does only seem to accept integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

73
        $this->timelineCollector->collect($this, /** @scrutinizer ignore-type */ $uniqueId);
Loading history...
74
    }
75
76
    public function collectTotalTime(?ResponseInterface $response, float|string $startTime, ?string $uniqueId): void
77
    {
78
        if (!$this->isActive()) {
79
            return;
80
        }
81
82
        if (!isset($this->requests[$uniqueId]) || !is_array($this->requests[$uniqueId])) {
83
            return;
84
        }
85
        $entry = &$this->requests[$uniqueId][count($this->requests[$uniqueId]) - 1];
86
        if ($response instanceof ResponseInterface) {
87
            $entry['responseRaw'] = Message::toString($response);
88
            $entry['responseStatus'] = $response->getStatusCode();
89
            Message::rewindBody($response);
90
        }
91
        $entry['endTime'] = $startTime;
92
        $entry['totalTime'] = $entry['endTime'] - $entry['startTime'];
93
    }
94
}
95