IblockLoader::getItem()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Alex19pov31\LinkedData\Loaders;
4
5
use Alex19pov31\BitrixHelper\IblockHelper;
6
use Alex19pov31\LinkedData\LoaderInterface;
7
use Alex19pov31\LinkedData\Storage;
8
9
class IblockLoader implements LoaderInterface
10
{
11
    use TraitBitrixLoader;
12
13
    /**
14
     * Код инфоблока
15
     *
16
     * @var string
17
     */
18
    private $iblockCode;
19
20
    /**
21
     * Идентификатор инфоблока
22
     *
23
     * @var integer
24
     */
25
    private $iblockId;
26
27
    /**
28
     * Менеджер данных
29
     *
30
     * @var \CIBlockElement|null
0 ignored issues
show
Bug introduced by
The type CIBlockElement 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...
31
     */
32
    private $dataManager;
33
34
    /**
35
     * Хелпер инфоблоков
36
     *
37
     * @var IblockHelper|null
38
     */
39
    private $iblockHelper;
40
41
    public function __construct(string $iblockCode, $nameRepository = null, $dataManager = null, $storage = null, $iblockHelper = null)
42
    {
43
        $this->iblockCode = $iblockCode;
44
        $this->iblockHelper = $iblockHelper;
45
        $this->iblockId = $this->getIblockId($iblockCode);
46
        $this->nameRepository = $nameRepository;
47
        $this->dataManager = !is_null($dataManager) ? $dataManager : \CIBlockElement::class;
0 ignored issues
show
Documentation Bug introduced by
It seems like ! is_null($dataManager) ...: CIBlockElement::class can also be of type string. However, the property $dataManager is declared as type CIBlockElement|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
48
49
        $storageClass = !is_null($storage) ? $storage : Storage::class;
50
        $storageClass::getRepository($this->repositoryName($iblockCode))
51
            ->setLoaderData($this);
52
    }
53
54
    private function getIblockId(string $iblockCode): int
55
    {
56
        if (!is_null($this->iblockHelper)) {
57
            return (int) $this->iblockHelper::getIblockID($iblockCode);
58
        }
59
60
        IblockHelper::setCacheTime(60);
61
        $this->iblockHelper = new IblockHelper;
62
        return (int) $this->iblockHelper::getIblockID($iblockCode);
63
    }
64
65
    /**
66
     * Имя репозитория по-умолчанию
67
     *
68
     * @param string $iblockCode
69
     * @return string
70
     */
71
    public static function getRepositoryName(string $iblockCode): string
72
    {
73
        return 'iblock_' . $iblockCode;
74
    }
75
76
    /**
77
     * Класс для получения данных
78
     *
79
     * @return \CIBlockElement
80
     */
81
    private function getDataManager()
82
    {
83
        return $this->dataManager;
84
    }
85
86
    /**
87
     * Возвращает данные
88
     *
89
     * @return array
90
     */
91
    public function getData(): array
92
    {
93
        $data = [];
94
        $filter = $this->filter;
95
        $filter['IBLOCK_ID'] = $this->iblockId;
96
        $res = $this->getDataManager()::GetList(
97
            $this->sort,
98
            $filter,
99
            false,
100
            false,
101
            $this->select
102
        );
103
104
        while ($item = $res->Fetch()) {
105
            if (empty($item[$this->getKey()])) {
106
                $data[] = $item;
107
                continue;
108
            }
109
110
            $key = $item[$this->getKey()];
111
            $data[$key] = $item;
112
        }
113
114
        return $data;
115
    }
116
117
    /**
118
     * Возвращает элемент по ключу
119
     *
120
     * @param mixed $key
121
     * @return mixed
122
     */
123
    public function getItem($key)
124
    {
125
        $filter = $this->filter;
126
        $filter[$this->getKey()] = $key;
127
        $filter['IBLOCK_ID'] = $this->iblockId;
128
        $data = $this->getDataManager()::GetList(
129
            $this->sort,
130
            $filter,
131
            false,
132
            false,
133
            $this->select
134
        )->Fetch();
135
136
        return !empty($data) ? (array) $data : [];
137
    }
138
}
139