MemoryStorage   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 58
rs 10
c 1
b 0
f 0
ccs 15
cts 15
cp 1
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getData() 0 9 2
A flush() 0 3 1
A read() 0 18 3
A clear() 0 2 1
A addCollector() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Storage;
6
7
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
8
use Yiisoft\Yii\Debug\DebuggerIdGenerator;
9
10
final class MemoryStorage implements StorageInterface
11
{
12
    /**
13
     * @var CollectorInterface[]
14
     */
15
    private array $collectors = [];
16
17
    public function __construct(
18 30
        private readonly DebuggerIdGenerator $idGenerator
19
    ) {
20 30
    }
21
22
    public function addCollector(CollectorInterface $collector): void
23 26
    {
24
        $this->collectors[$collector->getName()] = $collector;
25 26
    }
26
27
    public function read(string $type, ?string $id = null): array
28 8
    {
29
        if ($type === self::TYPE_SUMMARY) {
30 8
            return [
31 8
                $this->idGenerator->getId() => [
32
                    'id' => $this->idGenerator->getId(),
33
                    'collectors' => array_keys($this->collectors),
34 8
                ],
35
            ];
36
        }
37 24
38
        if ($type === self::TYPE_OBJECTS) {
39 24
            return [
40
                $this->idGenerator->getId() => array_merge(...array_values($this->getData())),
41 24
            ];
42 8
        }
43
44
        return [$this->idGenerator->getId() => $this->getData()];
45 24
    }
46
47
    public function getData(): array
48 17
    {
49
        $data = [];
50 17
51
        foreach ($this->collectors as $name => $collector) {
52
            $data[$name] = $collector->getCollected();
53
        }
54
55
        return $data;
56
    }
57
58
    public function flush(): void
59
    {
60
        $this->collectors = [];
61
    }
62
63
    /**
64
     * @codeCoverageIgnore
65
     */
66
    public function clear(): void
67
    {
68
    }
69
}
70