Passed
Pull Request — master (#186)
by Dmitriy
03:58 queued 01:17
created

FilesystemStreamCollector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 26
c 0
b 0
f 0
dl 0
loc 65
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A startup() 0 7 1
A collect() 0 9 2
A shutdown() 0 9 1
A getIndexData() 0 7 1
A __construct() 0 2 1
A getCollected() 0 3 1
A reset() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
final class FilesystemStreamCollector implements CollectorInterface, IndexCollectorInterface
8
{
9
    use CollectorTrait;
10
11
    public function __construct(private array $ignoredPathPatterns = [], private array $ignoredClasses = [])
12
    {
13
    }
14
15
    /**
16
     * @var array[]
17
     */
18
    private array $operations = [];
19
20
    public function getCollected(): array
21
    {
22
        return array_map('array_values', $this->operations);
23
    }
24
25
    public function startup(): void
26
    {
27
        $this->isActive = true;
28
        FilesystemStreamProxy::register();
29
        FilesystemStreamProxy::$collector = $this;
30
        FilesystemStreamProxy::$ignoredPathPatterns = $this->ignoredPathPatterns;
31
        FilesystemStreamProxy::$ignoredClasses = $this->ignoredClasses;
32
    }
33
34
    public function shutdown(): void
35
    {
36
        FilesystemStreamProxy::unregister();
37
        FilesystemStreamProxy::$collector = null;
38
        FilesystemStreamProxy::$ignoredPathPatterns = [];
39
        FilesystemStreamProxy::$ignoredClasses = [];
40
41
        $this->reset();
42
        $this->isActive = false;
43
    }
44
45
    public function collect(string $operation, string $path, array $args): void
46
    {
47
        if (!$this->isActive()) {
48
            return;
49
        }
50
51
        $this->operations[$operation][] = [
52
            'path' => $path,
53
            'args' => $args,
54
        ];
55
    }
56
57
    public function getIndexData(): array
58
    {
59
        return [
60
            'fs_stream' => array_merge(
61
                ...array_map(
62
                    fn (string $operation) => [$operation => count($this->operations[$operation])],
63
                    array_keys($this->operations)
64
                )
65
            ),
66
        ];
67
    }
68
69
    private function reset(): void
70
    {
71
        $this->operations = [];
72
    }
73
}
74