Passed
Push — master ( 51a308...702f01 )
by Alexander
01:42
created

setCacheData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 5
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
/**
15
 * Entry point bitrix application
16
 *
17
 * @return CMain
18
 */
19
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...
20
{
21
    global $APPLICATION;
22
    return $APPLICATION;
23
}
24
25
function appInstance(): Application
26
{
27
    return Application::getInstance();
28
}
29
30
/**
31
 * Выполнить sql запрос
32
 *
33
 * @param string $sql
34
 * @return Result
35
 */
36
function sql(string $sql): Result
37
{
38
    return appInstance()->getConnection()->query($sql);
39
}
40
41
/**
42
 * Загрузить модуль
43
 *
44
 * @param string $moduleName
45
 * @return boolean
46
 */
47
function loadModule(string $moduleName): bool
48
{
49
    return (bool) Loader::includeModule($moduleName);
50
}
51
52
/**
53
 * Идентификатор инфоблока
54
 *
55
 * @param string $code
56
 * @param string|null $iblockType
57
 * @return integer|null
58
 */
59
function getIblockId(string $code, $iblockType = null, int $minutes = 0)
60
{
61
    IblockHelper::setCacheTime($minutes);
62
    return IblockHelper::getIblockID($code, $iblockType);
63
}
64
65
/**
66
 * Информация о HL блоке
67
 *
68
 * @param string $code
69
 * @param integer $minutes
70
 * @return array|null
71
 */
72
function getHlBlock(string $code, $minutes = 0)
73
{
74
    HlBlockHelper::setCacheTime($minutes);
75
    return HlBlockHelper::getHlblock($code);
76
}
77
78
/**
79
 * Класс для работы с HL блоком
80
 *
81
 * @param string $code
82
 * @param integer $minutes
83
 * @return DataManager|null
84
 */
85
function getHlBlockClass(string $code, $minutes = 0)
86
{
87
    HlBlockHelper::setCacheTime($minutes);
88
    return HlBlockHelper::getHlblockClass($code);
89
}
90
91
/**
92
 * Тэггированный кеш
93
 *
94
 * @return TaggedCache
95
 */
96
function taggedCache(): TaggedCache
97
{
98
    return appInstance()->getTaggedCache();
99
}
100
101
/**
102
 * Инициализация тэггированного кеша
103
 *
104
 * @param array $tags
105
 * @param string $cacheDir
106
 * @return void
107
 */
108
function initTagCache(array $tags, string $cacheDir = '/')
109
{
110
    taggedCache()->startTagCache($cacheDir);
111
    foreach ($tags as $tag) {
112
        taggedCache()->registerTag($tag);
113
    }
114
    taggedCache()->endTagCache();
115
}
116
117
/**
118
 * Кеширование
119
 *
120
 * @param integer $minutes
121
 * @param string $key
122
 * @param string $initDir
123
 * @param string $baseDir
124
 * @param callable $func
125
 * @return mixed
126
 */
127
function cache(int $minutes, string $key, $initDir = '/', string $baseDir = 'cache', callable $func)
128
{
129
    $data = null;
130
    $ttl = $minutes * 60;
131
    $cache = new Cache(Cache::createInstance());
132
    if ($cache->initCache($ttl, $key, $initDir, $baseDir)) {
133
        $data = $cache->getVars();
134
    } elseif ($cache->startDataCache($ttl, $key, $initDir, [], $baseDir)) {
135
        try {
136
            $data = $func();
137
            $cache->endDataCache($data);
138
        } catch (Exception $e) {
139
            $cache->abortDataCache();
140
            taggedCache()->abortTagCache();
141
        }
142
    }
143
144
    return $data;
145
}
146
147
/**
148
 * Отчистка кеша
149
 *
150
 * @param string $key
151
 * @param string $initDir
152
 * @param string $baseDir
153
 * @return void
154
 */
155
function cleanCache(string $key, $initDir = '/', string $baseDir = 'cache')
156
{
157
    $cache = new Cache(Cache::createInstance());
158
    $cache->clean($key, $initDir, $baseDir);
159
}
160
161
/**
162
 * Зписать данные в кеш (с предварительной отчисткой)
163
 *
164
 * @param integer $minutes
165
 * @param string $key
166
 * @param string $initDir
167
 * @param string $baseDir
168
 * @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...
169
 * @return void
170
 */
171
function setCacheData(int $minutes, string $key, $initDir = '/', string $baseDir = 'cache', $data)
172
{
173
    cleanCache($key, $initDir, $baseDir);
174
    cache($minutes, $key, $initDir, $baseDir, function () use ($data) {
175
        return $data;
176
    });
177
}
178
179
/**
180
 * Область редактирования элемента инфоблока
181
 *
182
 * @param CBitrixComponentTemplate $tpl
183
 * @param integer $elementId
184
 * @param integer $iblockId
185
 * @param string $iblockType
186
 * @param string $description
187
 * @return string
188
 */
189
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...
190
{
191
    $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';
192
    if (is_null($description)) {
193
        $description = "Редактировать элемент";
194
    }
195
196
    $tpl->AddEditAction($elementId, $link, $description);
197
198
    return (string) $tpl->GetEditAreaId($elementId);
199
}
200
201
/**
202
 * Область редактирования раздела инфоблока
203
 *
204
 * @param CBitrixComponentTemplate $tpl
205
 * @param integer $sectionId
206
 * @param integer $iblockId
207
 * @param string $iblockType
208
 * @param string $description
209
 * @return string
210
 */
211
function initEditIblockSection(CBitrixComponentTemplate $tpl, int $sectionId, int $iblockId, string $iblockType, string $description = null): string
212
{
213
    $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';
214
    if (is_null($description)) {
215
        $description = "Редактировать раздел";
216
    }
217
218
    $tpl->AddEditAction($sectionId, $link, $description);
219
220
    return (string) $tpl->GetEditAreaId($sectionId);
221
}
222
223
/**
224
 * Область редактирования элемента HL блока
225
 *
226
 * @param CBitrixComponentTemplate $tpl
227
 * @param integer $elementId
228
 * @param string $hlBlockName
229
 * @param string $description
230
 * @return string
231
 */
232
function initEditHLBlockElement(CBitrixComponentTemplate $tpl, int $elementId, string $hlBlockName, string $description = null): string
233
{
234
    $hlBlock = getHlBlock($hlBlockName);
235
    if (empty($hlBlock)) {
236
        return '';
237
    }
238
239
    $link = '/bitrix/admin/highloadblock_row_edit.php?ENTITY_ID=' . $hlBlock['ID'] . '&ID=' . $elementId . '&bxpublic=Y';
240
    if (is_null($description)) {
241
        $description = "Редактировать элемент";
242
    }
243
244
    $tpl->AddEditAction($elementId, $link, $description);
245
246
    return (string) $tpl->GetEditAreaId($elementId);
247
}
248
249
/**
250
 * Инициализация компонента
251
 *
252
 * @param string $name
253
 * @param string $template
254
 * @param array $params
255
 * @param mixed $parentComponent
256
 * @param array $functionParams
257
 * @return ComponentHelper
258
 */
259
function initComponent(string $name, string $template = '', array $params = [], $parentComponent = null, $functionParams = []): ComponentHelper
260
{
261
    return new ComponentHelper($name, $template, $params, $parentComponent, $functionParams);
262
}
263
264
/**
265
 * Включаемая область
266
 *
267
 * @param string $path
268
 * @param string|null $folder
269
 * @return void
270
 */
271
function includeArea(string $path, $basePath = null)
272
{
273
    if (is_null($basePath)) {
274
        $basePath = bxApp()->GetTemplatePath('') . 'include';
275
    }
276
277
    initComponent('bitrix:main.include')
278
        ->setTemplate('.default')
279
        ->setParams([
280
            "AREA_FILE_SHOW" => "file",
281
            "AREA_FILE_SUFFIX" => "inc",
282
            "AREA_FILE_RECURSIVE" => "Y",
283
            "EDIT_TEMPLATE" => "",
284
            "COMPONENT_TEMPLATE" => ".default",
285
            "PATH" => "{$basePath}/{$path}",
286
        ])
287
        ->show();
288
}
289