HLBlockLoader::getItem()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Alex19pov31\LinkedData\Loaders;
4
5
use Alex19pov31\LinkedData\LoaderInterface;
6
use Alex19pov31\LinkedData\Storage;
7
use Bitrix\Main\ORM\Data\DataManager;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\ORM\Data\DataManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class HLBlockLoader implements LoaderInterface
10
{
11
    use TraitBitrixLoader;
12
13
    /**
14
     * Название таблицы
15
     *
16
     * @var string
17
     */
18
    private $tableName;
19
20
    /**
21
     * Менеджер данных
22
     *
23
     * @var DataManager|null
24
     */
25
    private $dataManager;
26
27
    public function __construct(string $tableName, $nameRepository = null, $dataManager = null, $storage = null)
28
    {
29
        $this->tableName = $tableName;
30
        $this->nameRepository = $nameRepository;
31
        $this->dataManager = $dataManager;
32
33
        $storageClass = !is_null($storage) ? $storage : Storage::class;
34
        $storageClass::getRepository($this->repositoryName($tableName))
35
            ->setLoaderData($this);
36
    }
37
38
    /**
39
     * Имя репозитория по-умолчанию
40
     *
41
     * @param string $tableName
42
     * @return string
43
     */
44
    public static function getRepositoryName(string $tableName): string
45
    {
46
        return 'hl_' . $tableName;
47
    }
48
49
    /**
50
     * Класс для получения данных
51
     *
52
     * @return DataManager|null
53
     */
54
    private function getDataManager()
55
    {
56
        if (!is_null($this->dataManager)) {
57
            return $this->dataManager;
58
        }
59
60
        return $this->dataManager = getHlBlockClass($this->tableName);
61
    }
62
63
    /**
64
     * Возвращает данные
65
     *
66
     * @return array
67
     */
68
    public function getData(): array
69
    {
70
        $dataManager = $this->getDataManager();
71
        if (is_null($dataManager)) {
72
            return [];
73
        }
74
75
        $data = [];
76
        $res = $dataManager::getList([
77
            'filter' => $this->filter,
78
            'select' => $this->select,
79
            'order' => $this->sort,
80
        ]);
81
82
        while ($item = $res->fetch()) {
83
            if (empty($item[$this->getKey()])) {
84
                $data[] = $item;
85
                continue;
86
            }
87
88
            $key = $item[$this->getKey()];
89
            $data[$key] = $item;
90
        }
91
92
        return $data;
93
    }
94
95
    /**
96
     * Возвращает элемент по ключу
97
     *
98
     * @param mixed $key
99
     * @return mixed
100
     */
101
    public function getItem($key)
102
    {
103
        $dataManager = $this->getDataManager();
104
        if (is_null($dataManager)) {
105
            return [];
106
        }
107
108
        $item = $dataManager::getList([
109
            'filter' => [$this->getKey() => $key],
110
        ])->fetch();
111
112
        return !empty($item) ? (array) $item : [];
113
    }
114
}
115