Completed
Push — master ( 889fac...47d4c3 )
by Nekrasov
01:32
created

SectionModel   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 166
Duplicated Lines 4.82 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A iblockId() 0 9 2
A query() 0 4 1
A create() 8 8 2
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 setUpdateSearch() 0 4 1

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 SectionModel getByCode(string $code)
14
 * @method static SectionModel getByExternalId(string $id)
15
 * @method static SectionQuery countElements($value)
16
 *
17
 * Base Query methods
18
 * @method static Collection getList()
19
 * @method static SectionModel first()
20
 * @method static SectionModel 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
 */
42
class SectionModel extends BitrixModel
43
{
44
    /**
45
     * Corresponding IBLOCK_ID
46
     *
47
     * @var int
48
     */
49
    const IBLOCK_ID = null;
50
51
    /**
52
     * Bitrix entity object.
53
     *
54
     * @var object
55
     */
56
    public static $bxObject;
57
58
    /**
59
     * Corresponding object class name.
60
     *
61
     * @var string
62
     */
63
    protected static $objectClass = 'CIBlockSection';
64
65
    /**
66
     * Update search after each create or update.
67
     *
68
     * @var bool
69
     */
70
    protected static $updateSearch = true;
71
72
    /**
73
     * Getter for corresponding iblock id.
74
     *
75
     * @throws LogicException
76
     *
77
     * @return int
78
     */
79
    public static function iblockId()
80
    {
81
        $id = static::IBLOCK_ID;
82
        if (!$id) {
83
            throw new LogicException('You must set $iblockId property or override iblockId() method');
84
        }
85
        
86
        return $id;
87
    }
88
89
    /**
90
     * Instantiate a query object for the model.
91
     *
92
     * @return SectionQuery
93
     */
94
    public static function query()
95
    {
96
        return new SectionQuery(static::instantiateObject(), get_called_class());
97
    }
98
99
    /**
100
     * Create new item in database.
101
     *
102
     * @param $fields
103
     *
104
     * @throws ExceptionFromBitrix
105
     *
106
     * @return static|bool
107
     */
108 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...
109
    {
110
        if (!isset($fields['IBLOCK_ID'])) {
111
            $fields['IBLOCK_ID'] = static::iblockId();
112
        }
113
114
        return static::internalCreate($fields);
115
    }
116
117
    /**
118
     * Get IDs of direct children of the section.
119
     * Additional filter can be specified.
120
     *
121
     * @param array $filter
122
     *
123
     * @return array
124
     */
125
    public function getDirectChildren(array $filter = [])
126
    {
127
        return static::query()
128
            ->filter($filter)
129
            ->filter(['SECTION_ID' => $this->id])
130
            ->select('ID')
131
            ->getList()
132
            ->transform(function ($section) {
133
                return (int) $section['ID'];
134
            })
135
            ->all();
136
    }
137
138
    /**
139
     * Get IDs of all children of the section (direct or not).
140
     * Additional filter can be specified.
141
     *
142
     * @param array $filter
143
     * @param array|string $sort
144
     *
145
     * @return array
146
     */
147
    public function getAllChildren(array $filter = [], $sort = ['LEFT_MARGIN' => 'ASC'])
148
    {
149
        if (!isset($this->fields['LEFT_MARGIN']) || !isset($this->fields['RIGHT_MARGIN'])) {
150
            $this->refresh();
151
        }
152
153
        return static::query()
154
            ->sort($sort)
155
            ->filter($filter)
156
            ->filter([
157
                '!ID' => $this->id,
158
                '>LEFT_MARGIN' => $this->fields['LEFT_MARGIN'],
159
                '<RIGHT_MARGIN' => $this->fields['RIGHT_MARGIN'],
160
            ])
161
            ->select('ID')
162
            ->getList()
163
            ->transform(function ($section) {
164
                return (int) $section['ID'];
165
            })
166
            ->all();
167
    }
168
169
    /**
170
     * Proxy for GetPanelButtons
171
     *
172
     * @param array $options
173
     * @return array
174
     */
175
    public function getPanelButtons($options = [])
176
    {
177
        return CIBlock::GetPanelButtons(
178
            static::iblockId(),
179
            0,
180
            $this->id,
181
            $options
182
        );
183
    }
184
185
    public static function internalDirectCreate($bxObject, $fields)
186
    {
187
        return $bxObject->add($fields, true, static::$updateSearch);
188
    }
189
190
    /**
191
     * @param $fields
192
     * @param $fieldsSelectedForSave
193
     * @return bool
194
     */
195
    protected function internalUpdate($fields, $fieldsSelectedForSave)
196
    {
197
        return !empty($fields) ? static::$bxObject->update($this->id, $fields, true, static::$updateSearch) : false;
198
    }
199
200
    /**
201
     * @param $value
202
     */
203
    public static function setUpdateSearch($value)
204
    {
205
        static::$updateSearch = $value;
206
    }
207
}
208