Completed
Push — master ( 80ce94...4c89ca )
by Nekrasov
01:54
created

ElementQuery   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 266
Duplicated Lines 13.91 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 2
dl 37
loc 266
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A instantiateCIblockObject() 0 12 3
A groupBy() 0 6 1
A getList() 22 22 3
A getByCode() 0 6 1
A getByExternalId() 0 6 1
A count() 0 8 2
A normalizeFilter() 0 6 1
A normalizeSelect() 15 15 3
A addAllPropsToSelect() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Arrilot\BitrixModels\Queries;
4
5
use CIBlock;
6
use Illuminate\Support\Collection;
7
use Arrilot\BitrixModels\Models\ElementModel;
8
use Exception;
9
10
/**
11
 * @method ElementQuery sortByDate(string $sort = 'desc')
12
 */
13
class ElementQuery extends BaseQuery
14
{
15
    /**
16
     * CIblock object or test double.
17
     *
18
     * @var object.
19
     */
20
    public static $cIblockObject;
21
22
    /**
23
     * Query sort.
24
     *
25
     * @var array
26
     */
27
    public $sort = ['SORT' => 'ASC'];
28
29
    /**
30
     * Query group by.
31
     *
32
     * @var array
33
     */
34
    public $groupBy = false;
35
36
    /**
37
     * Iblock id.
38
     *
39
     * @var int
40
     */
41
    protected $iblockId;
42
43
    /**
44
     * List of standard entity fields.
45
     *
46
     * @var array
47
     */
48
    protected $standardFields = [
49
        'ID',
50
        'TIMESTAMP_X',
51
        'TIMESTAMP_X_UNIX',
52
        'MODIFIED_BY',
53
        'DATE_CREATE',
54
        'DATE_CREATE_UNIX',
55
        'CREATED_BY',
56
        'IBLOCK_ID',
57
        'IBLOCK_SECTION_ID',
58
        'ACTIVE',
59
        'ACTIVE_FROM',
60
        'ACTIVE_TO',
61
        'SORT',
62
        'NAME',
63
        'PREVIEW_PICTURE',
64
        'PREVIEW_TEXT',
65
        'PREVIEW_TEXT_TYPE',
66
        'DETAIL_PICTURE',
67
        'DETAIL_TEXT',
68
        'DETAIL_TEXT_TYPE',
69
        'SEARCHABLE_CONTENT',
70
        'IN_SECTIONS',
71
        'SHOW_COUNTER',
72
        'SHOW_COUNTER_START',
73
        'CODE',
74
        'TAGS',
75
        'XML_ID',
76
        'EXTERNAL_ID',
77
        'TMP_ID',
78
        'CREATED_USER_NAME',
79
        'DETAIL_PAGE_URL',
80
        'LIST_PAGE_URL',
81
        'CREATED_DATE',
82
    ];
83
84
    /**
85
     * Constructor.
86
     *
87
     * @param object $bxObject
88
     * @param string $modelName
89
     */
90
    public function __construct($bxObject, $modelName)
91
    {
92
        static::instantiateCIblockObject();
93
        parent::__construct($bxObject, $modelName);
94
95
        $this->iblockId = $modelName::iblockId();
96
    }
97
98
    /**
99
     * Instantiate bitrix entity object.
100
     *
101
     * @throws Exception
102
     *
103
     * @return object
104
     */
105
    public static function instantiateCIblockObject()
106
    {
107
        if (static::$cIblockObject) {
108
            return static::$cIblockObject;
109
        }
110
111
        if (class_exists('CIBlock')) {
112
            return static::$cIblockObject = new CIBlock();
113
        }
114
115
        throw new Exception('CIblock object initialization failed');
116
    }
117
118
    /**
119
     * Setter for groupBy.
120
     *
121
     * @param $value
122
     *
123
     * @return $this
124
     */
125
    public function groupBy($value)
126
    {
127
        $this->groupBy = $value;
128
129
        return $this;
130
    }
131
132
    /**
133
     * Get list of items.
134
     *
135
     * @return Collection
136
     */
137 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...
138
    {
139
        if ($this->queryShouldBeStopped) {
140
            return new Collection();
141
        }
142
143
        $items = [];
144
145
        $rsItems = $this->bxObject->GetList(
146
            $this->sort,
147
            $this->normalizeFilter(),
148
            $this->groupBy,
149
            $this->navigation,
150
            $this->normalizeSelect()
151
        );
152
    
153
        while ($arItem = $rsItems->Fetch()) {
154
            $this->addItemToResultsUsingKeyBy($items, new $this->modelName($arItem['ID'], $arItem));
155
        }
156
157
        return new Collection($items);
158
    }
159
160
    /**
161
     * Get the first element with a given code.
162
     *
163
     * @param string $code
164
     *
165
     * @return ElementModel
166
     */
167
    public function getByCode($code)
168
    {
169
        $this->filter['CODE'] = $code;
170
171
        return $this->first();
172
    }
173
174
    /**
175
     * Get the first element with a given external id.
176
     *
177
     * @param string $id
178
     *
179
     * @return ElementModel
180
     */
181
    public function getByExternalId($id)
182
    {
183
        $this->filter['EXTERNAL_ID'] = $id;
184
185
        return $this->first();
186
    }
187
188
    /**
189
     * Get count of elements that match $filter.
190
     *
191
     * @return int
192
     */
193
    public function count()
194
    {
195
        if ($this->queryShouldBeStopped) {
196
            return 0;
197
        }
198
199
        return (int) $this->bxObject->GetList(false, $this->normalizeFilter(), []);
200
    }
201
202
//    /**
203
//     * Normalize properties's format converting it to 'PROPERTY_"CODE"_VALUE'.
204
//     *
205
//     * @param array $fields
206
//     *
207
//     * @return null
208
//     */
209
//    protected function normalizePropertyResultFormat(&$fields)
210
//    {
211
//        if (empty($fields['PROPERTIES'])) {
212
//            return;
213
//        }
214
//
215
//        foreach ($fields['PROPERTIES'] as $code => $prop) {
216
//            $fields['PROPERTY_'.$code.'_VALUE'] = $prop['VALUE'];
217
//            $fields['~PROPERTY_'.$code.'_VALUE'] = $prop['~VALUE'];
218
//            $fields['PROPERTY_'.$code.'_DESCRIPTION'] = $prop['DESCRIPTION'];
219
//            $fields['~PROPERTY_'.$code.'_DESCRIPTION'] = $prop['~DESCRIPTION'];
220
//            $fields['PROPERTY_'.$code.'_VALUE_ID'] = $prop['PROPERTY_VALUE_ID'];
221
//            if (isset($prop['VALUE_ENUM_ID'])) {
222
//                $fields['PROPERTY_'.$code.'_ENUM_ID'] = $prop['VALUE_ENUM_ID'];
223
//            }
224
//        }
225
//    }
226
227
    /**
228
     * Normalize filter before sending it to getList.
229
     * This prevents some inconsistency.
230
     *
231
     * @return array
232
     */
233
    protected function normalizeFilter()
234
    {
235
        $this->filter['IBLOCK_ID'] = $this->iblockId;
236
237
        return $this->filter;
238
    }
239
240
    /**
241
     * Normalize select before sending it to getList.
242
     * This prevents some inconsistency.
243
     *
244
     * @return array
245
     */
246 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...
247
    {
248
        if ($this->fieldsMustBeSelected()) {
249
            $this->select = array_merge($this->standardFields, $this->select);
250
        }
251
252
        if ($this->propsMustBeSelected()) {
253
            $this->addAllPropsToSelect();
254
        }
255
256
        $this->select[] = 'ID';
257
        $this->select[] = 'IBLOCK_ID';
258
259
        return $this->clearSelectArray();
260
    }
261
262
    /**
263
     * Add all iblock property codes to select.
264
     *
265
     * return null
266
     */
267
    protected function addAllPropsToSelect()
268
    {
269
        $this->select[] = 'ID';
270
        $this->select[] = 'IBLOCK_ID';
271
272
        //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...
273
        $rsProps = static::$cIblockObject->GetProperties($this->iblockId);
274
        while ($prop = $rsProps->Fetch()) {
275
            $this->select[] = 'PROPERTY_'.$prop['CODE'];
276
        }
277
    }
278
}
279