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

SectionQuery::getList()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 28
Ratio 100 %

Importance

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