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

CollectorRepository::getDumpObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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