Passed
Push — master ( bd13ca...36eff2 )
by Alexander
03:10
created

cacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
use Alex19pov31\BitrixHelper\ComponentHelper;
4
use Alex19pov31\BitrixHelper\HlBlockHelper;
5
use Alex19pov31\BitrixHelper\IblockHelper;
6
use Bitrix\Main\Application;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\Application 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
use Bitrix\Main\Data\Cache;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\Data\Cache 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
use Bitrix\Main\Data\TaggedCache;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\Data\TaggedCache 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...
9
use Bitrix\Main\DB\Exception;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\DB\Exception 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...
Bug introduced by
This use statement conflicts with another class in this namespace, Exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Bitrix\Main\DB\Result;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\DB\Result 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...
11
use Bitrix\Main\Loader;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\Loader 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...
12
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...
13
14
if (!function_exists('bxApp')) {
15
    /**
16
     * Entry point bitrix application
17
     *
18
     * @return CMain
19
     */
20
    function bxApp(): CMain
0 ignored issues
show
Bug introduced by
The type CMain 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...
21
    {
22
        global $APPLICATION;
23
        return $APPLICATION;
24
    }
25
}
26
27
if (!function_exists('appInstance')) {
28
    function appInstance(): Application
29
    {
30
        return Application::getInstance();
31
    }
32
}
33
34
if (!function_exists('sql')) {
35
    /**
36
     * Выполнить sql запрос
37
     *
38
     * @param string $sql
39
     * @return Result
40
     */
41
    function sql(string $sql): Result
42
    {
43
        return appInstance()->getConnection()->query($sql);
44
    }
45
}
46
47
if (!function_exists('loadModule')) {
48
    /**
49
     * Загрузить модуль
50
     *
51
     * @param string $moduleName
52
     * @return boolean
53
     */
54
    function loadModule(string $moduleName): bool
55
    {
56
        return (bool) Loader::includeModule($moduleName);
57
    }
58
}
59
60
if (!function_exists('cacheKey')) {
61
    /**
62
     * Ключ кеша
63
     *
64
     * @param array $data
65
     * @return string
66
     */
67
    function cacheKey(array $data): string
68
    {
69
        return md5(
70
            json_encode($data)
71
        );
72
    }
73
}
74
75
if (!function_exists('bxUser')) {
76
    /**
77
     * Объект локального пользователя
78
     *
79
     * @return CUser
80
     */
81
    function bxUser(): CUser
0 ignored issues
show
Bug introduced by
The type CUser 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...
82
    {
83
        global $USER;
84
        return $USER;
85
    }
86
}
87
88
if (!function_exists('getIblockId')) {
89
    /**
90
     * Идентификатор инфоблока
91
     *
92
     * @param string $code
93
     * @param string|null $iblockType
94
     * @return integer|null
95
     */
96
    function getIblockId(string $code, $iblockType = null, int $minutes = 0)
97
    {
98
        IblockHelper::setCacheTime($minutes);
99
        return IblockHelper::getIblockID($code, $iblockType);
100
    }
101
}
102
103
if (!function_exists('getHlBlock')) {
104
    /**
105
     * Информация о HL блоке
106
     *
107
     * @param string $code
108
     * @param integer $minutes
109
     * @return array|null
110
     */
111
    function getHlBlock(string $code, $minutes = 0)
112
    {
113
        HlBlockHelper::setCacheTime($minutes);
114
        return HlBlockHelper::getHlblock($code);
115
    }
116
}
117
118
if (!function_exists('getHlBlockClass')) {
119
    /**
120
     * Класс для работы с HL блоком
121
     *
122
     * @param string $code
123
     * @param integer $minutes
124
     * @return DataManager|null
125
     */
126
    function getHlBlockClass(string $code, $minutes = 0)
127
    {
128
        HlBlockHelper::setCacheTime($minutes);
129
        return HlBlockHelper::getHlblockClass($code);
130
    }
131
}
132
133
if (!function_exists('taggedCache')) {
134
    /**
135
     * Тэггированный кеш
136
     *
137
     * @return TaggedCache
138
     */
139
    function taggedCache(): TaggedCache
140
    {
141
        return appInstance()->getTaggedCache();
142
    }
143
}
144
145
if (!function_exists('initTagCache')) {
146
    /**
147
     * Инициализация тэггированного кеша
148
     *
149
     * @param array $tags
150
     * @param string $cacheDir
151
     * @return void
152
     */
153
    function initTagCache(array $tags, string $cacheDir = '/')
154
    {
155
        taggedCache()->startTagCache($cacheDir);
156
        foreach ($tags as $tag) {
157
            taggedCache()->registerTag($tag);
158
        }
159
        taggedCache()->endTagCache();
160
    }
161
}
162
163
if (!function_exists('cache')) {
164
    /**
165
     * Кеширование
166
     *
167
     * @param integer $minutes
168
     * @param string $key
169
     * @param string $initDir
170
     * @param string $baseDir
171
     * @param callable $func
172
     * @return mixed
173
     */
174
    function cache(int $minutes, string $key, $initDir = '/', string $baseDir = 'cache', callable $func)
175
    {
176
        $data = null;
177
        $ttl = $minutes * 60;
178
        $cache = Cache::createInstance();
179
        if ($cache->initCache($ttl, $key, $initDir, $baseDir)) {
180
            $data = $cache->getVars();
181
        } elseif ($cache->startDataCache($ttl, $key, $initDir, [], $baseDir)) {
182
            try {
183
                $data = $func();
184
                $cache->endDataCache($data);
185
            } catch (Exception $e) {
186
                $cache->abortDataCache();
187
                taggedCache()->abortTagCache();
188
            }
189
        }
190
191
        return $data;
192
    }
193
}
194
195
if (!function_exists('cleanCache')) {
196
    /**
197
     * Отчистка кеша
198
     *
199
     * @param string $key
200
     * @param string $initDir
201
     * @param string $baseDir
202
     * @return void
203
     */
204
    function cleanCache(string $key, $initDir = '/', string $baseDir = 'cache')
205
    {
206
        $cache = Cache::createInstance();
207
        $cache->clean($key, $initDir, $baseDir);
208
    }
209
}
210
211
if (!function_exists('setCacheData')) {
212
    /**
213
     * Зписать данные в кеш (с предварительной отчисткой)
214
     *
215
     * @param integer $minutes
216
     * @param string $key
217
     * @param string $initDir
218
     * @param string $baseDir
219
     * @param [type] $data
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
220
     * @return void
221
     */
222
    function setCacheData(int $minutes, string $key, $initDir = '/', string $baseDir = 'cache', $data)
223
    {
224
        cleanCache($key, $initDir, $baseDir);
225
        cache($minutes, $key, $initDir, $baseDir, function () use ($data) {
226
            return $data;
227
        });
228
    }
229
}
230
231
if (!function_exists('initEditIblockElement')) {
232
    /**
233
     * Область редактирования элемента инфоблока
234
     *
235
     * @param CBitrixComponentTemplate $tpl
236
     * @param integer $elementId
237
     * @param integer $iblockId
238
     * @param string $iblockType
239
     * @param string $description
240
     * @return string
241
     */
242
    function initEditIblockElement(CBitrixComponentTemplate $tpl, int $elementId, int $iblockId, string $iblockType, string $description = null): string
0 ignored issues
show
Bug introduced by
The type CBitrixComponentTemplate 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...
243
    {
244
        $link = '/bitrix/admin/iblock_element_edit.php?IBLOCK_ID=' . $iblockId . '&type=' . $iblockType . '&ID=' . $elementId . '&lang=ru&force_catalog=&filter_section=0&bxpublic=Y&from_module=iblock';
245
        if (is_null($description)) {
246
            $description = "Редактировать элемент";
247
        }
248
249
        $tpl->AddEditAction($elementId, $link, $description);
250
251
        return (string) $tpl->GetEditAreaId($elementId);
252
    }
253
}
254
255
if (!function_exists('initEditIblockSection')) {
256
    /**
257
     * Область редактирования раздела инфоблока
258
     *
259
     * @param CBitrixComponentTemplate $tpl
260
     * @param integer $sectionId
261
     * @param integer $iblockId
262
     * @param string $iblockType
263
     * @param string $description
264
     * @return string
265
     */
266
    function initEditIblockSection(CBitrixComponentTemplate $tpl, int $sectionId, int $iblockId, string $iblockType, string $description = null): string
267
    {
268
        $link = '/bitrix/admin/iblock_section_edit.php?IBLOCK_ID=' . $iblockId . '&type=' . $iblockType . '&ID=' . $sectionId . '&lang=ru&find_section_section=0&bxpublic=Y&from_module=iblock';
269
        if (is_null($description)) {
270
            $description = "Редактировать раздел";
271
        }
272
273
        $tpl->AddEditAction($sectionId, $link, $description);
274
275
        return (string) $tpl->GetEditAreaId($sectionId);
276
    }
277
}
278
279
if (!function_exists('initEditHLBlockElement')) {
280
    /**
281
     * Область редактирования элемента HL блока
282
     *
283
     * @param CBitrixComponentTemplate $tpl
284
     * @param integer $elementId
285
     * @param string $hlBlockName
286
     * @param string $description
287
     * @return string
288
     */
289
    function initEditHLBlockElement(CBitrixComponentTemplate $tpl, int $elementId, string $hlBlockName, string $description = null): string
290
    {
291
        $hlBlock = getHlBlock($hlBlockName);
292
        if (empty($hlBlock)) {
293
            return '';
294
        }
295
296
        $link = '/bitrix/admin/highloadblock_row_edit.php?ENTITY_ID=' . $hlBlock['ID'] . '&ID=' . $elementId . '&bxpublic=Y';
297
        if (is_null($description)) {
298
            $description = "Редактировать элемент";
299
        }
300
301
        $tpl->AddEditAction($elementId, $link, $description);
302
303
        return (string) $tpl->GetEditAreaId($elementId);
304
    }
305
}
306
307
if (!function_exists('initComponent')) {
308
    /**
309
     * Инициализация компонента
310
     *
311
     * @param string $name
312
     * @param string $template
313
     * @param array $params
314
     * @param mixed $parentComponent
315
     * @param array $functionParams
316
     * @return ComponentHelper
317
     */
318
    function initComponent(string $name, string $template = '', array $params = [], $parentComponent = null, $functionParams = []): ComponentHelper
319
    {
320
        return new ComponentHelper($name, $template, $params, $parentComponent, $functionParams);
321
    }
322
}
323
324
if (!function_exists('includeArea')) {
325
    /**
326
     * Включаемая область
327
     *
328
     * @param string $path
329
     * @param string|null $folder
330
     * @return void
331
     */
332
    function includeArea(string $path, $basePath = null)
333
    {
334
        if (is_null($basePath)) {
335
            $basePath = bxApp()->GetTemplatePath('') . 'include';
336
        }
337
338
        initComponent('bitrix:main.include')
339
            ->setTemplate('.default')
340
            ->setParams([
341
                "AREA_FILE_SHOW" => "file",
342
                "AREA_FILE_SUFFIX" => "inc",
343
                "AREA_FILE_RECURSIVE" => "Y",
344
                "EDIT_TEMPLATE" => "",
345
                "COMPONENT_TEMPLATE" => ".default",
346
                "PATH" => "{$basePath}/{$path}",
347
            ])
348
            ->show();
349
    }
350
}
351