Passed
Push — master ( 1ffb2d...f18857 )
by Dmitriy
05:56 queued 02:52
created

CollectorRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 54
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getSummary() 0 8 2
A loadData() 0 12 3
A getObject() 0 11 3
A getDetail() 0 3 1
A getDumpObject() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Debug\Repository;
6
7
use Yiisoft\Yii\Debug\Api\Debug\Exception\NotFoundException;
8
use Yiisoft\Yii\Debug\Storage\StorageInterface;
9
10
final class CollectorRepository implements CollectorRepositoryInterface
11
{
12
    public function __construct(private StorageInterface $storage)
13
    {
14
    }
15
16
    public function getSummary(?string $id = null): array
17
    {
18
        $data = $this->loadData(StorageInterface::TYPE_SUMMARY, $id);
19
        if ($id !== null) {
20
            return $data;
21
        }
22
23
        return array_values(array_reverse($data));
24
    }
25
26
    public function getDetail(string $id): array
27
    {
28
        return $this->loadData(StorageInterface::TYPE_DATA, $id);
29
    }
30
31
    public function getDumpObject(string $id): array
32
    {
33
        return $this->loadData(StorageInterface::TYPE_OBJECTS, $id);
34
    }
35
36
    public function getObject(string $id, string $objectId): array|null
37
    {
38
        $dump = $this->loadData(StorageInterface::TYPE_OBJECTS, $id);
39
40
        foreach ($dump as $name => $value) {
41
            if (($pos = strrpos($name, "#$objectId")) !== false) {
42
                return [substr($name, 0, $pos), $value];
43
            }
44
        }
45
46
        return null;
47
    }
48
49
    /**
50
     * @throws NotFoundException
51
     */
52
    private function loadData(string $fileType, ?string $id = null): array
53
    {
54
        $data = $this->storage->read($fileType, $id);
55
        if (!empty($id)) {
56
            if (!isset($data[$id])) {
57
                throw new NotFoundException(sprintf('Unable to find debug data ID with "%s"', $id));
58
            }
59
60
            return $data[$id];
61
        }
62
63
        return $data;
64
    }
65
}
66