MemoryStorage::flush()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
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