Passed
Pull Request — master (#210)
by Dmitriy
02:36
created

HttpClientCollector::getSummary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 0
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 getCollected(): array
33
    {
34
        return array_merge(...array_values($this->requests));
35
    }
36
37
    public function getSummary(): array
38
    {
39
        return [
40
            'http' => [
41
                'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)),
42
                'totalTime' => array_sum(
43
                    array_merge(
44
                        ...array_map(
45
                            static fn (array $entry) => array_column($entry, 'totalTime'),
46
                            array_values($this->requests)
47
                        )
48
                    )
49
                ),
50
            ],
51
        ];
52
    }
53
54
    public function collect(RequestInterface $request, float|string $startTime, string $line, ?string $uniqueId): void
55
    {
56
        if (!$this->isActive()) {
57
            return;
58
        }
59
60
        $this->requests[$uniqueId][] = [
61
            'startTime' => $startTime,
62
            'endTime' => $startTime,
63
            'totalTime' => 0.0,
64
            'method' => $request->getMethod(),
65
            'uri' => (string) $request->getUri(),
66
            'headers' => $request->getHeaders(),
67
            'line' => $line,
68
        ];
69
    }
70
71
    public function collectTotalTime(?ResponseInterface $response, float|string $startTime, ?string $uniqueId): void
72
    {
73
        if (!$this->isActive()) {
74
            return;
75
        }
76
77
        if (!isset($this->requests[$uniqueId]) || !is_array($this->requests[$uniqueId])) {
78
            return;
79
        }
80
        $entry = &$this->requests[$uniqueId][count($this->requests[$uniqueId]) - 1];
81
        if ($response instanceof ResponseInterface) {
82
            $entry['responseRaw'] = Message::toString($response);
83
            $entry['responseStatus'] = $response->getStatusCode();
84
            Message::rewindBody($response);
85
        }
86
        $entry['endTime'] = $startTime;
87
        $entry['totalTime'] = $entry['endTime'] - $entry['startTime'];
88
    }
89
}
90