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

MemoryStorage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 8
    public function read($type = self::TYPE_INDEX): array
29
    {
30 8
        return [$this->idGenerator->getId() => $this->getData()];
31
    }
32
33 24
    public function getData(): array
34
    {
35 24
        $data = [];
36
37 24
        foreach ($this->collectors as $collector) {
38 8
            $data[] = $collector->getCollected();
39
        }
40
41 24
        return $data;
42
    }
43
44 16
    public function flush(): void
45
    {
46 16
        $this->collectors = [];
47 16
    }
48
}
49