Passed
Push — master ( f7ef7a...65c26d )
by Dmitriy
02:46
created

HttpStreamCollector::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Stream;
6
7
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
8
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
9
10
final class HttpStreamCollector implements SummaryCollectorInterface
11
{
12
    use CollectorTrait;
13
14
    public function __construct(private array $ignoredPathPatterns = [], private array $ignoredClasses = [])
15
    {
16
    }
17
18
    private array $requests = [];
19
20
    public function getCollected(): array
21
    {
22
        return $this->requests;
23
    }
24
25
    public function startup(): void
26
    {
27
        $this->isActive = true;
28
        HttpStreamProxy::register();
29
        // TODO: add cURL support, maybe through proxy?
30
        // https://github.com/php/php-src/issues/10509
31
        //stream_context_set_default([
32
        //    'http' => [
33
        //        'proxy' => 'yii-debug-http://127.0.0.1',
34
        //    ],
35
        //]);
36
        HttpStreamProxy::$collector = $this;
37
    }
38
39
    public function shutdown(): void
40
    {
41
        HttpStreamProxy::unregister();
42
        HttpStreamProxy::$collector = null;
43
44
        $this->reset();
45
        $this->isActive = false;
46
    }
47
48
    public function collect(string $operation, string $path, array $args): void
49
    {
50
        if (!$this->isActive()) {
51
            return;
52
        }
53
54
        $this->requests[$operation][] = [
55
            'uri' => $path,
56
            'args' => $args,
57
        ];
58
    }
59
60
    public function getSummary(): array
61
    {
62
        return [
63
            'http_stream' => array_merge(
64
                ...array_map(
65
                    fn (string $operation) => [
66
                        $operation => is_countable($this->requests[$operation]) ? count(
67
                            $this->requests[$operation]
68
                        ) : 0,
69
                    ],
70
                    array_keys($this->requests)
71
                )
72
            ),
73
        ];
74
    }
75
76
    private function reset(): void
77
    {
78
        $this->requests = [];
79
    }
80
}
81