Passed
Pull Request — master (#186)
by Dmitriy
05:22 queued 02:51
created

FileStreamCollector::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
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
final class FileStreamCollector implements CollectorInterface, IndexCollectorInterface
8
{
9
    use CollectorTrait;
10
11
    public function __construct(private array $ignoredPathPatterns = [], private array $ignoredClasses = [])
12
    {
13
    }
14
15
    private array $requests = [];
16
17
    public function getCollected(): array
18
    {
19
        return $this->requests;
20
    }
21
22
    public function startup(): void
23
    {
24
        $this->isActive = true;
25
        FileStreamProxy::register();
26
        FileStreamProxy::$collector = $this;
27
        FileStreamProxy::$ignoredPathPatterns = $this->ignoredPathPatterns;
28
        FileStreamProxy::$ignoredClasses = $this->ignoredClasses;
29
    }
30
31
    public function shutdown(): void
32
    {
33
        FileStreamProxy::unregister();
34
        FileStreamProxy::$collector = null;
35
        FileStreamProxy::$ignoredPathPatterns = [];
36
        FileStreamProxy::$ignoredClasses = [];
37
38
        $this->reset();
39
        $this->isActive = false;
40
    }
41
42
    public function collect(string $operation, string $path, array $args): void
43
    {
44
        if (!$this->isActive()) {
45
            return;
46
        }
47
48
        $this->requests[$operation][] = [
49
            'path' => $path,
50
            'args' => $args,
51
        ];
52
    }
53
54
    public function getIndexData(): array
55
    {
56
        return [
57
            'file' => array_merge(
58
                ...array_map(
59
                    fn (string $operation) => [$operation => is_countable($this->requests[$operation]) ? count($this->requests[$operation]) : 0],
60
                    array_keys($this->requests)
61
                )
62
            ),
63
        ];
64
    }
65
66
    private function reset(): void
67
    {
68
        $this->requests = [];
69
    }
70
}
71