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

MemoryStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getData() 0 9 2
A flush() 0 3 1
A read() 0 3 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
    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