Completed
Push — master ( f7e861...136cb7 )
by Nekrasov
02:32
created

ElementQuery::addAllPropsToSelect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Arrilot\BitrixModels\Queries;
4
5
use Arrilot\BitrixCacher\Cache;
6
use CIBlock;
7
use Illuminate\Support\Collection;
8
use Arrilot\BitrixModels\Models\ElementModel;
9
use Exception;
10
11
/**
12
 * @method ElementQuery active()
13
 * @method ElementQuery sortByDate(string $sort = 'desc')
14
 * @method ElementQuery fromSectionWithId(int $id)
15
 * @method ElementQuery fromSectionWithCode(string $code)
16
 */
17
class ElementQuery extends BaseQuery
18
{
19
    /**
20
     * CIblock object or test double.
21
     *
22
     * @var object.
23
     */
24
    public static $cIblockObject;
25
26
    /**
27
     * Query sort.
28
     *
29
     * @var array
30
     */
31
    public $sort = ['SORT' => 'ASC'];
32
33
    /**
34
     * Query group by.
35
     *
36
     * @var array
37
     */
38
    public $groupBy = false;
39
40
    /**
41
     * Iblock id.
42
     *
43
     * @var int
44
     */
45
    protected $iblockId;
46
47
    /**
48
     * List of standard entity fields.
49
     *
50
     * @var array
51
     */
52
    protected $standardFields = [
53
        'ID',
54
        'TIMESTAMP_X',
55
        'TIMESTAMP_X_UNIX',
56
        'MODIFIED_BY',
57
        'DATE_CREATE',
58
        'DATE_CREATE_UNIX',
59
        'CREATED_BY',
60
        'IBLOCK_ID',
61
        'IBLOCK_SECTION_ID',
62
        'ACTIVE',
63
        'ACTIVE_FROM',
64
        'ACTIVE_TO',
65
        'SORT',
66
        'NAME',
67
        'PREVIEW_PICTURE',
68
        'PREVIEW_TEXT',
69
        'PREVIEW_TEXT_TYPE',
70
        'DETAIL_PICTURE',
71
        'DETAIL_TEXT',
72
        'DETAIL_TEXT_TYPE',
73
        'SEARCHABLE_CONTENT',
74
        'IN_SECTIONS',
75
        'SHOW_COUNTER',
76
        'SHOW_COUNTER_START',
77
        'CODE',
78
        'TAGS',
79
        'XML_ID',
80
        'EXTERNAL_ID',
81
        'TMP_ID',
82
        'CREATED_USER_NAME',
83
        'DETAIL_PAGE_URL',
84
        'LIST_PAGE_URL',
85
        'CREATED_DATE',
86
    ];
87
88
    /**
89
     * Constructor.
90
     *
91
     * @param object $bxObject
92
     * @param string $modelName
93
     */
94
    public function __construct($bxObject, $modelName)
95
    {
96
        static::instantiateCIblockObject();
97
        parent::__construct($bxObject, $modelName);
98
99
        $this->iblockId = $modelName::iblockId();
100
    }
101
102
    /**
103
     * Instantiate bitrix entity object.
104
     *
105
     * @throws Exception
106
     *
107
     * @return object
108
     */
109
    public static function instantiateCIblockObject()
110
    {
111
        if (static::$cIblockObject) {
112
            return static::$cIblockObject;
113
        }
114
115
        if (class_exists('CIBlock')) {
116
            return static::$cIblockObject = new CIBlock();
117
        }
118
119
        throw new Exception('CIblock object initialization failed');
120
    }
121
122
    /**
123
     * Setter for groupBy.
124
     *
125
     * @param $value
126
     *
127
     * @return $this
128
     */
129
    public function groupBy($value)
130
    {
131
        $this->groupBy = $value;
132
133
        return $this;
134
    }
135
136
    /**
137
     * Get list of items.
138
     *
139
     * @return Collection
140
     */
141 View Code Duplication
    public function getList()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        if ($this->queryShouldBeStopped) {
144
            return new Collection();
145
        }
146
147
        $sort = $this->sort;
148
        $filter = $this->normalizeFilter();
149
        $groupBy = $this->groupBy;
150
        $navigation = $this->navigation;
151
        $select = $this->normalizeSelect();
152
        $queryType = 'ElementQuery::getList';
153
        $keyBy = $this->keyBy;
154
155
        $callback = function() use ($sort, $filter, $groupBy, $navigation, $select) {
156
            $items = [];
157
            $rsItems = $this->bxObject->GetList($sort, $filter, $groupBy, $navigation, $select);
158
            while ($arItem = $rsItems->Fetch()) {
159
                $this->addItemToResultsUsingKeyBy($items, new $this->modelName($arItem['ID'], $arItem));
160
            }
161
162
            return new Collection($items);
163
        };
164
165
        return $this->handleCacheIfNeeded(compact('sort', 'filter', 'group', 'navigation', 'select', 'queryType', 'keyBy'), $callback);
166
    }
167
168
    /**
169
     * Get the first element with a given code.
170
     *
171
     * @param string $code
172
     *
173
     * @return ElementModel
174
     */
175
    public function getByCode($code)
176
    {
177
        $this->filter['CODE'] = $code;
178
179
        return $this->first();
180
    }
181
182
    /**
183
     * Get the first element with a given external id.
184
     *
185
     * @param string $id
186
     *
187
     * @return ElementModel
188
     */
189
    public function getByExternalId($id)
190
    {
191
        $this->filter['EXTERNAL_ID'] = $id;
192
193
        return $this->first();
194
    }
195
196
    /**
197
     * Get count of elements that match $filter.
198
     *
199
     * @return int
200
     */
201 View Code Duplication
    public function count()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203
        if ($this->queryShouldBeStopped) {
204
            return 0;
205
        }
206
207
        $filter = $this->normalizeFilter();
208
        $queryType = "ElementQuery::count";
209
210
        $callback = function () use ($filter) {
211
            return (int) $this->bxObject->GetList(false, $filter, []);
212
        };
213
214
        return $this->handleCacheIfNeeded(compact('filter', 'queryType'), $callback);
215
    }
216
217
//    /**
218
//     * Normalize properties's format converting it to 'PROPERTY_"CODE"_VALUE'.
219
//     *
220
//     * @param array $fields
221
//     *
222
//     * @return null
223
//     */
224
//    protected function normalizePropertyResultFormat(&$fields)
225
//    {
226
//        if (empty($fields['PROPERTIES'])) {
227
//            return;
228
//        }
229
//
230
//        foreach ($fields['PROPERTIES'] as $code => $prop) {
231
//            $fields['PROPERTY_'.$code.'_VALUE'] = $prop['VALUE'];
232
//            $fields['~PROPERTY_'.$code.'_VALUE'] = $prop['~VALUE'];
233
//            $fields['PROPERTY_'.$code.'_DESCRIPTION'] = $prop['DESCRIPTION'];
234
//            $fields['~PROPERTY_'.$code.'_DESCRIPTION'] = $prop['~DESCRIPTION'];
235
//            $fields['PROPERTY_'.$code.'_VALUE_ID'] = $prop['PROPERTY_VALUE_ID'];
236
//            if (isset($prop['VALUE_ENUM_ID'])) {
237
//                $fields['PROPERTY_'.$code.'_ENUM_ID'] = $prop['VALUE_ENUM_ID'];
238
//            }
239
//        }
240
//    }
241
242
    /**
243
     * Normalize filter before sending it to getList.
244
     * This prevents some inconsistency.
245
     *
246
     * @return array
247
     */
248
    protected function normalizeFilter()
249
    {
250
        $this->filter['IBLOCK_ID'] = $this->iblockId;
251
252
        return $this->filter;
253
    }
254
255
    /**
256
     * Normalize select before sending it to getList.
257
     * This prevents some inconsistency.
258
     *
259
     * @return array
260
     */
261 View Code Duplication
    protected function normalizeSelect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
    {
263
        if ($this->fieldsMustBeSelected()) {
264
            $this->select = array_merge($this->standardFields, $this->select);
265
        }
266
267
        if ($this->propsMustBeSelected()) {
268
            $this->addAllPropsToSelect();
269
        }
270
271
        $this->select[] = 'ID';
272
        $this->select[] = 'IBLOCK_ID';
273
274
        return $this->clearSelectArray();
275
    }
276
277
    /**
278
     * Add all iblock property codes to select.
279
     *
280
     * return null
281
     */
282
    protected function addAllPropsToSelect()
283
    {
284
        $this->select[] = 'ID';
285
        $this->select[] = 'IBLOCK_ID';
286
287
        //dd (static::$cIblockObject);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
288
        $rsProps = static::$cIblockObject->GetProperties($this->iblockId);
289
        while ($prop = $rsProps->Fetch()) {
290
            $this->select[] = 'PROPERTY_'.$prop['CODE'];
291
        }
292
    }
293
}
294