Passed
Pull Request — master (#91)
by Rustam
02:39
created

MemoryStorage::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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