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

FileStreamCollector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 9
eloc 26
c 2
b 1
f 0
dl 0
loc 62
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 3 1
A startup() 0 7 1
A getCollected() 0 3 1
A collect() 0 9 2
A shutdown() 0 9 1
A __construct() 0 2 1
A getIndexData() 0 7 2
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