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

MemoryStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

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
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
        return [$this->idGenerator->getId() => $this->getData()];
32
    }
33
34 24
    public function getData(): array
35
    {
36 24
        $data = [];
37
38 24
        foreach ($this->collectors as $collector) {
39 8
            $data[] = $collector->getCollected();
40
        }
41
42 24
        return $data;
43
    }
44
45 16
    public function flush(): void
46
    {
47 16
        $this->collectors = [];
48 16
    }
49
}
50