SectionModel   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 222
Duplicated Lines 3.6 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 3
dl 8
loc 222
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A iblockId() 0 9 2
A query() 0 4 1
A getDirectChildren() 0 12 1
A getAllChildren() 0 21 3
A getPanelButtons() 0 9 1
A internalDirectCreate() 0 4 1
A internalUpdate() 0 4 2
A setResort() 0 4 1
A setUpdateSearch() 0 4 1
A setResizePictures() 0 4 1
A create() 8 8 2
A scopeChildrenOf() 0 8 1
A scopeDirectChildrenOf() 0 6 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\Models;
4
5
use Arrilot\BitrixModels\Exceptions\ExceptionFromBitrix;
6
use Arrilot\BitrixModels\Queries\SectionQuery;
7
use CIBlock;
8
use Illuminate\Support\Collection;
9
use LogicException;
10
11
/**
12
 * SectionQuery methods
13
 * @method static static getByCode(string $code)
14
 * @method static static getByExternalId(string $id)
15
 * @method static SectionQuery countElements($value)
16
 *
17
 * Base Query methods
18
 * @method static Collection|static[] getList()
19
 * @method static static first()
20
 * @method static static getById(int $id)
21
 * @method static SectionQuery sort(string|array $by, string $order='ASC')
22
 * @method static SectionQuery order(string|array $by, string $order='ASC') // same as sort()
23
 * @method static SectionQuery filter(array $filter)
24
 * @method static SectionQuery addFilter(array $filters)
25
 * @method static SectionQuery resetFilter()
26
 * @method static SectionQuery navigation(array $filter)
27
 * @method static SectionQuery select($value)
28
 * @method static SectionQuery keyBy(string $value)
29
 * @method static SectionQuery limit(int $value)
30
 * @method static SectionQuery offset(int $value)
31
 * @method static SectionQuery page(int $num)
32
 * @method static SectionQuery take(int $value) // same as limit()
33
 * @method static SectionQuery forPage(int $page, int $perPage=15)
34
 * @method static \Illuminate\Pagination\LengthAwarePaginator paginate(int $perPage = 15, string $pageName = 'page')
35
 * @method static \Illuminate\Pagination\Paginator simplePaginate(int $perPage = 15, string $pageName = 'page')
36
 * @method static SectionQuery stopQuery()
37
 * @method static SectionQuery cache(float|int $minutes)
38
 *
39
 * Scopes
40
 * @method static SectionQuery active()
41
 * @method static SectionQuery childrenOf(SectionModel $section)
42
 * @method static SectionQuery directChildrenOf(SectionModel|int $section)
43
 */
44
class SectionModel extends BitrixModel
45
{
46
    /**
47
     * Corresponding IBLOCK_ID
48
     *
49
     * @var int
50
     */
51
    const IBLOCK_ID = null;
52
53
    /**
54
     * Bitrix entity object.
55
     *
56
     * @var object
57
     */
58
    public static $bxObject;
59
60
    /**
61
     * Corresponding object class name.
62
     *
63
     * @var string
64
     */
65
    protected static $objectClass = 'CIBlockSection';
66
67
    /**
68
     * Recalculate LEFT_MARGIN and RIGHT_MARGIN during add/update ($bResort for CIBlockSection::Add/Update).
69
     *
70
     * @var bool
71
     */
72
    protected static $resort = true;
73
74
    /**
75
     * Update search after each create or update.
76
     *
77
     * @var bool
78
     */
79
    protected static $updateSearch = true;
80
81
    /**
82
     * Resize pictures during add/update ($bResizePictures for CIBlockSection::Add/Update).
83
     *
84
     * @var bool
85
     */
86
    protected static $resizePictures = false;
87
88
    /**
89
     * Getter for corresponding iblock id.
90
     *
91
     * @throws LogicException
92
     *
93
     * @return int
94
     */
95
    public static function iblockId()
96
    {
97
        $id = static::IBLOCK_ID;
98
        if (!$id) {
99
            throw new LogicException('You must set $iblockId property or override iblockId() method');
100
        }
101
        
102
        return $id;
103
    }
104
105
    /**
106
     * Instantiate a query object for the model.
107
     *
108
     * @return SectionQuery
109
     */
110
    public static function query()
111
    {
112
        return new SectionQuery(static::instantiateObject(), get_called_class());
113
    }
114
115
    /**
116
     * Create new item in database.
117
     *
118
     * @param $fields
119
     *
120
     * @throws ExceptionFromBitrix
121
     *
122
     * @return static|bool
123
     */
124 View Code Duplication
    public static function create($fields)
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...
125
    {
126
        if (!isset($fields['IBLOCK_ID'])) {
127
            $fields['IBLOCK_ID'] = static::iblockId();
128
        }
129
130
        return static::internalCreate($fields);
131
    }
132
133
    /**
134
     * Get IDs of direct children of the section.
135
     * Additional filter can be specified.
136
     *
137
     * @param array $filter
138
     *
139
     * @return array
140
     */
141
    public function getDirectChildren(array $filter = [])
142
    {
143
        return static::query()
144
            ->filter($filter)
145
            ->filter(['SECTION_ID' => $this->id])
146
            ->select('ID')
147
            ->getList()
148
            ->transform(function ($section) {
149
                return (int) $section['ID'];
150
            })
151
            ->all();
152
    }
153
154
    /**
155
     * Get IDs of all children of the section (direct or not).
156
     * Additional filter can be specified.
157
     *
158
     * @param array $filter
159
     * @param array|string $sort
160
     *
161
     * @return array
162
     */
163
    public function getAllChildren(array $filter = [], $sort = ['LEFT_MARGIN' => 'ASC'])
164
    {
165
        if (!isset($this->fields['LEFT_MARGIN']) || !isset($this->fields['RIGHT_MARGIN'])) {
166
            $this->refresh();
167
        }
168
169
        return static::query()
170
            ->sort($sort)
171
            ->filter($filter)
172
            ->filter([
173
                '!ID' => $this->id,
174
                '>LEFT_MARGIN' => $this->fields['LEFT_MARGIN'],
175
                '<RIGHT_MARGIN' => $this->fields['RIGHT_MARGIN'],
176
            ])
177
            ->select('ID')
178
            ->getList()
179
            ->transform(function ($section) {
180
                return (int) $section['ID'];
181
            })
182
            ->all();
183
    }
184
185
    /**
186
     * Proxy for GetPanelButtons
187
     *
188
     * @param array $options
189
     * @return array
190
     */
191
    public function getPanelButtons($options = [])
192
    {
193
        return CIBlock::GetPanelButtons(
194
            static::iblockId(),
195
            0,
196
            $this->id,
197
            $options
198
        );
199
    }
200
201
    public static function internalDirectCreate($bxObject, $fields)
202
    {
203
        return $bxObject->add($fields, static::$resort, static::$updateSearch, static::$resizePictures);
204
    }
205
206
    /**
207
     * @param $fields
208
     * @param $fieldsSelectedForSave
209
     * @return bool
210
     */
211
    protected function internalUpdate($fields, $fieldsSelectedForSave)
212
    {
213
        return !empty($fields) ? static::$bxObject->update($this->id, $fields, static::$resort, static::$updateSearch, static::$resizePictures) : false;
214
    }
215
216
    /**
217
     * @param $value
218
     */
219
    public static function setResort($value)
220
    {
221
        static::$resort = $value;
222
    }
223
224
    /**
225
     * @param $value
226
     */
227
    public static function setUpdateSearch($value)
228
    {
229
        static::$updateSearch = $value;
230
    }
231
232
    /**
233
     * @param $value
234
     */
235
    public static function setResizePictures($value)
236
    {
237
        static::$resizePictures = $value;
238
    }
239
240
    /**
241
     * @param $query
242
     * @param SectionModel $section
243
     * @return SectionQuery
244
     */
245
    public function scopeChildrenOf(SectionQuery $query, SectionModel $section)
246
    {
247
        $query->filter['>LEFT_MARGIN'] = $section->fields['LEFT_MARGIN'];
248
        $query->filter['<RIGHT_MARGIN'] = $section->fields['RIGHT_MARGIN'];
249
        $query->filter['>DEPTH_LEVEL'] = $section->fields['DEPTH_LEVEL'];
250
251
        return $query;
252
    }
253
254
    /**
255
     * @param $query
256
     * @param SectionModel|int $section
257
     * @return SectionQuery
258
     */
259
    public function scopeDirectChildrenOf(SectionQuery $query, $section)
260
    {
261
        $query->filter['SECTION_ID'] = is_int($section) ? $section : $section->id;
262
263
        return $query;
264
    }
265
}
266