Completed
Push — master ( 93bd80...c62968 )
by Nekrasov
01:18
created

SectionQuery::getList()   D

Complexity

Conditions 9
Paths 2

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
rs 4.909
cc 9
eloc 25
nc 2
nop 0
1
<?php
2
3
namespace Arrilot\BitrixModels\Queries;
4
5
use Arrilot\BitrixModels\Helpers;
6
use Illuminate\Support\Collection;
7
use Arrilot\BitrixModels\Models\SectionModel;
8
9
/**
10
 * @method SectionQuery active()
11
 */
12
class SectionQuery extends OldCoreQuery
13
{
14
    /**
15
     * Query sort.
16
     *
17
     * @var array
18
     */
19
    public $sort = ['SORT' => 'ASC'];
20
21
    /**
22
     * Query bIncCnt.
23
     * This is sent to getList directly.
24
     *
25
     * @var array|false
26
     */
27
    public $countElements = false;
28
29
    /**
30
     * Iblock id.
31
     *
32
     * @var int
33
     */
34
    protected $iblockId;
35
36
    /**
37
     * List of standard entity fields.
38
     *
39
     * @var array
40
     */
41
    protected $standardFields = [
42
        'ID',
43
        'CODE',
44
        'EXTERNAL_ID',
45
        'IBLOCK_ID',
46
        'IBLOCK_SECTION_ID',
47
        'TIMESTAMP_X',
48
        'SORT',
49
        'NAME',
50
        'ACTIVE',
51
        'GLOBAL_ACTIVE',
52
        'PICTURE',
53
        'DESCRIPTION',
54
        'DESCRIPTION_TYPE',
55
        'LEFT_MARGIN',
56
        'RIGHT_MARGIN',
57
        'DEPTH_LEVEL',
58
        'SEARCHABLE_CONTENT',
59
        'SECTION_PAGE_URL',
60
        'MODIFIED_BY',
61
        'DATE_CREATE',
62
        'CREATED_BY',
63
        'DETAIL_PICTURE',
64
    ];
65
66
    /**
67
     * Constructor.
68
     *
69
     * @param object $bxObject
70
     * @param string $modelName
71
     */
72
    public function __construct($bxObject, $modelName)
73
    {
74
        parent::__construct($bxObject, $modelName);
75
76
        $this->iblockId = $modelName::iblockId();
77
    }
78
79
    /**
80
     * CIBlockSection::getList substitution.
81
     *
82
     * @return Collection
83
     */
84
    public function getList()
85
    {
86
        if ($this->queryShouldBeStopped) {
87
            return new Collection();
88
        }
89
90
        $queryType = 'SectionQuery::getList';
91
        $sort = $this->sort;
92
        $filter = $this->normalizeFilter();
93
        $countElements = $this->countElements;
94
        $select = $this->normalizeSelect();
95
        $navigation = $this->navigation;
96
        $keyBy = $this->keyBy;
97
98
        $callback = function() use ($sort, $filter, $countElements, $select, $navigation) {
99
            $sections = [];
100
            $rsSections = $this->bxObject->getList($sort, $filter, $countElements, $select, $navigation);
101
            while ($arSection = $this->performFetchUsingSelectedMethod($rsSections)) {
102
103
                // Если передать nPageSize, то Битрикс почему-то перестает десериализовать множественные свойсвта...
104
                // Проверим это еще раз, и если есть проблемы то пофиксим.
105
                foreach ($arSection as $field => $value) {
106
                    if (
107
                    is_string($value)
108
                    && Helpers::startsWith($value, 'a:')
109
                    && (Helpers::startsWith($field, 'UF_') || Helpers::startsWith($field, '~UF_'))
110
                    ) {
111
                        $unserializedValue = @unserialize($value);
112
                        $arSection[$field] = $unserializedValue === false ? $value : $unserializedValue;
113
                    }
114
                }
115
116
                $this->addItemToResultsUsingKeyBy($sections, new $this->modelName($arSection['ID'], $arSection));
117
            }
118
119
            return new Collection($sections);
120
        };
121
122
        $cacheParams = compact('queryType', 'sort', 'filter', 'countElements', 'select', 'navigation', 'keyBy');
123
124
        return $this->handleCacheIfNeeded($cacheParams, $callback);
125
    }
126
127
    /**
128
     * Get the first section with a given code.
129
     *
130
     * @param string $code
131
     *
132
     * @return SectionModel
133
     */
134
    public function getByCode($code)
135
    {
136
        $this->filter['CODE'] = $code;
137
138
        return $this->first();
139
    }
140
141
    /**
142
     * Get the first section with a given external id.
143
     *
144
     * @param string $id
145
     *
146
     * @return SectionModel
147
     */
148
    public function getByExternalId($id)
149
    {
150
        $this->filter['EXTERNAL_ID'] = $id;
151
152
        return $this->first();
153
    }
154
155
    /**
156
     * Get count of sections that match filter.
157
     *
158
     * @return int
159
     */
160 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...
161
    {
162
        if ($this->queryShouldBeStopped) {
163
            return 0;
164
        }
165
166
        $queryType = 'SectionQuery::count';
167
        $filter = $this->normalizeFilter();
168
        $callback = function() use ($filter) {
169
            return (int) $this->bxObject->getCount($filter);
170
        };
171
172
        return $this->handleCacheIfNeeded(compact('queryType', 'filter'), $callback);
173
    }
174
175
    /**
176
     * Setter for countElements.
177
     *
178
     * @param $value
179
     *
180
     * @return $this
181
     */
182
    public function countElements($value)
183
    {
184
        $this->countElements = $value;
185
186
        return $this;
187
    }
188
189
    /**
190
     * Normalize filter before sending it to getList.
191
     * This prevents some inconsistency.
192
     *
193
     * @return array
194
     */
195
    protected function normalizeFilter()
196
    {
197
        $this->filter['IBLOCK_ID'] = $this->iblockId;
198
199
        return $this->filter;
200
    }
201
202
    /**
203
     * Normalize select before sending it to getList.
204
     * This prevents some inconsistency.
205
     *
206
     * @return array
207
     */
208
    protected function normalizeSelect()
209
    {
210
        if ($this->fieldsMustBeSelected()) {
211
            $this->select = array_merge($this->standardFields, $this->select);
212
        }
213
214
        if ($this->propsMustBeSelected()) {
215
            $this->select[] = 'IBLOCK_ID';
216
            $this->select[] = 'UF_*';
217
        }
218
219
        $this->select[] = 'ID';
220
221
        return $this->clearSelectArray();
222
    }
223
}
224