Passed
Pull Request — master (#74)
by Rustam
02:49
created

MemoryStorage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 41
ccs 16
cts 17
cp 0.9412
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addCollector() 0 3 1
A getData() 0 9 2
A flush() 0 3 1
A read() 0 7 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
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 24
    public function __construct(DebuggerIdGenerator $idGenerator)
20
    {
21 24
        $this->idGenerator = $idGenerator;
22 24
    }
23
24 24
    public function addCollector(CollectorInterface $collector): void
25
    {
26 24
        $this->collectors[get_class($collector)] = $collector;
27 24
    }
28
29 8
    public function read($type = self::TYPE_INDEX): array
30
    {
31 8
        if ($type === self::TYPE_INDEX) {
32
            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 16
    public function flush(): void
50
    {
51 16
        $this->collectors = [];
52 16
    }
53
}
54