Issues (53)

src/HlBlockHelper.php (7 issues)

Labels
Severity
1
<?php
2
3
namespace Alex19pov31\BitrixHelper;
4
5
use Bitrix\Highloadblock\HighloadBlockTable;
0 ignored issues
show
The type Bitrix\Highloadblock\HighloadBlockTable 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...
6
use Bitrix\Main\ORM\Data\DataManager;
0 ignored issues
show
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...
7
8
class HlBlockHelper
9
{
10
    /**
11
     * Список HL блоков
12
     *
13
     * @var array
14
     */
15
    private static $hlBlockList = [];
16
    private static $hlBlockTableCalss;
17
18
    /**
19
     * Время кеширования
20
     *
21
     * @var integer
22
     */
23
    private static $ttl = 0;
24
25
    public static function setHlBlockTableCalss(HighloadBlockTable $object)
26
    {
27
        static::$hlBlockTableCalss = $object;
0 ignored issues
show
Since $hlBlockTableCalss is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $hlBlockTableCalss to at least protected.
Loading history...
28
    }
29
30
    private static function getHlBlockTableClass()
31
    {
32
        if (!is_null(static::$hlBlockTableCalss)) {
0 ignored issues
show
Since $hlBlockTableCalss is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $hlBlockTableCalss to at least protected.
Loading history...
33
            return static::$hlBlockTableCalss;
34
        }
35
36
        loadModule('highloadblock');
37
        return HighloadBlockTable::class;
38
    }
39
40
    /**
41
     * Список доступных HL блоков
42
     *
43
     * @return array
44
     */
45
    private static function getListHlBlock(): array
46
    {
47
        if (!empty(static::$hlBlockList)) {
0 ignored issues
show
Since $hlBlockList is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $hlBlockList to at least protected.
Loading history...
48
            return static::$hlBlockList;
49
        }
50
51
        $hlTableClass = static::getHlBlockTableClass();
52
        $res = $hlTableClass::getList([
53
            'cache' => [
54
                'ttl' => static::$ttl,
0 ignored issues
show
Since $ttl is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $ttl to at least protected.
Loading history...
55
            ],
56
        ]);
57
58
        while ($hlBlock = $res->fetch()) {
59
            static::$hlBlockList['id_' . $hlBlock['ID']] = $hlBlock;
60
            static::$hlBlockList['name_' . $hlBlock['NAME']] = $hlBlock;
61
            static::$hlBlockList['table_' . $hlBlock['TABLE_NAME']] = $hlBlock;
62
        }
63
64
        return (array) static::$hlBlockList;
65
    }
66
67
    /**
68
     * Установить вермя кеширования
69
     *
70
     * @param integer $minutes
71
     * @return void
72
     */
73
    public static function setCacheTime(int $minutes)
74
    {
75
        static::$ttl = $minutes * 60;
0 ignored issues
show
Since $ttl is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $ttl to at least protected.
Loading history...
76
    }
77
78
    /**
79
     * Информация о HL блоке по идентификатору
80
     *
81
     * @param integer $id
82
     * @return array|null
83
     */
84
    public static function getHlblockByID(int $id)
85
    {
86
        $list = static::getListHlBlock();
87
        $hlBlock = $list['id_' . $id];
88
89
        return !empty($hlBlock) ? (array) $hlBlock : null;
90
    }
91
92
    /**
93
     * Информация о HL блоке по названию
94
     *
95
     * @param string $name
96
     * @return array|null
97
     */
98
    public static function getHlblockByName(string $name)
99
    {
100
        $list = static::getListHlBlock();
101
        $hlBlock = $list['name_' . $name];
102
103
        return !empty($hlBlock) ? (array) $hlBlock : null;
104
    }
105
106
    /**
107
     * Информация о HL блоке по названию таблицы
108
     *
109
     * @param string $tableName
110
     * @return array|null
111
     */
112
    public static function getHlblockByTableName(string $tableName)
113
    {
114
        $list = static::getListHlBlock();
115
        $hlBlock = $list['table_' . $tableName];
116
117
        return !empty($hlBlock) ? (array) $hlBlock : null;
118
    }
119
120
    /**
121
     * Информация о HL блоке
122
     *
123
     * @param string $name
124
     * @return array|null
125
     */
126
    public static function getHlblock(string $name)
127
    {
128
        $hlBlock = static::getHlblockByID((int) $name);
129
        if (!is_null($hlBlock)) {
130
            return (array) $hlBlock;
131
        }
132
133
        $hlBlock = static::getHlblockByName($name);
134
        if (!is_null($hlBlock)) {
135
            return (array) $hlBlock;
136
        }
137
138
        $hlBlock = static::getHlblockByTableName($name);
139
        if (!is_null($hlBlock)) {
140
            return (array) $hlBlock;
141
        }
142
143
        return null;
144
    }
145
146
    /**
147
     * Класс для работы с HL блоком
148
     *
149
     * @param string $name
150
     * @return DataManager|null
151
     */
152
    public static function getHlblockClass(string $name)
153
    {
154
        $hlBlock = static::getHlblock($name);
155
        if (is_null($hlBlock)) {
156
            return null;
157
        }
158
159
        $hlTableClass = static::getHlBlockTableClass();
160
        return $hlTableClass::compileEntity($hlBlock)->getDataClass();
161
    }
162
}
163