Passed
Pull Request — master (#24)
by Rustam
08:29
created

CollectorRepository::getObject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 10
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Repository;
6
7
use Yiisoft\Yii\Debug\Api\Exception\NotFoundException;
8
use Yiisoft\Yii\Debug\Storage\StorageInterface;
9
10
class CollectorRepository implements CollectorRepositoryInterface
11
{
12
    private StorageInterface $storage;
13
14
    public function __construct(StorageInterface $storage)
15
    {
16
        $this->storage = $storage;
17
    }
18
19
    public function getSummary(?string $id = null): array
20
    {
21
        $data = $this->loadData(StorageInterface::TYPE_INDEX, $id);
22
        if ($id !== null) {
23
            return $data;
24
        }
25
26
        return array_values(array_reverse($data));
27
    }
28
29
    public function getDetail(string $id): array
30
    {
31
        return $this->loadData(StorageInterface::TYPE_DATA, $id);
32
    }
33
34
    public function getDumpObject(string $id): array
35
    {
36
        return $this->loadData(StorageInterface::TYPE_OBJECTS, $id);
37
    }
38
39
    public function getObject(string $id, string $objectId)
40
    {
41
        $dump = $this->loadData(StorageInterface::TYPE_OBJECTS, $id);
42
43
        foreach ($dump as $name => $value) {
44
            if (strrpos($name, "#$objectId") !== false) {
45
                return $value;
46
            }
47
        }
48
49
        return null;
50
    }
51
52
    private function loadData(string $fileType, ?string $id = null): array
53
    {
54
        $data = $this->storage->read($fileType);
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