Passed
Pull Request — master (#79)
by Rustam
03:02
created

MemoryStorage::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
    private DebuggerIdGenerator $idGenerator;
13
    /**
14
     * @var CollectorInterface[]
15
     */
16
    private array $collectors = [];
17
18 24
    public function __construct(DebuggerIdGenerator $idGenerator)
19
    {
20 24
        $this->idGenerator = $idGenerator;
21 24
    }
22
23 24
    public function addCollector(CollectorInterface $collector): void
24
    {
25 24
        $this->collectors[get_class($collector)] = $collector;
26 24
    }
27
28 24
    public function getData(): array
29
    {
30 24
        $data = [];
31
32 24
        foreach ($this->collectors as $collector) {
33 8
            $data[] = $collector->getCollected();
34
        }
35
36 24
        return $data;
37
    }
38
39 8
    public function read($type = self::TYPE_INDEX): array
40
    {
41 8
        return [$this->idGenerator->getId() => $this->getData()];
42
    }
43
44 16
    public function flush(): void
45
    {
46 16
        $this->collectors = [];
47 16
    }
48
}
49