Test Failed
Push — master ( e02c6e...13fa39 )
by Alexey
05:36
created

Item   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 362
Duplicated Lines 12.15 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 44
loc 362
rs 6.8
c 0
b 0
f 0
wmc 55
lcom 1
cbo 9

10 Methods

Rating   Name   Duplication   Size   Complexity  
C simpleItemHandler() 18 51 8
A realType() 0 15 4
A indexes() 0 22 1
D beforeSave() 22 35 17
B relations() 0 42 1
B getPrice() 0 18 9
A name() 0 9 4
B beforeDelete() 0 17 7
A getHref() 0 3 1
A inFav() 4 9 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Item often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Item, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Item
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ecommerce;
13
14
class Item extends \Model {
15
16
    public static $categoryModel = 'Ecommerce\Category';
17
    public static $objectName = 'Товар';
18
    public static $labels = [
19
        'name' => 'Название',
20
        'alias' => 'Алиас',
21
        'item_badge_id' => 'Наклейка',
22
        'category_id' => 'Раздел',
23
        'description' => 'Описание',
24
        'item_type_id' => 'Тип товара',
25
        'image_file_id' => 'Изображение',
26
        'best' => 'Лучшее предложение',
27
        'options' => 'Параметры',
28
        'offers' => 'Торговые предложения',
29
        'widget' => 'Виджет для отображения в каталоге',
30
        'view' => 'Шаблон для отображения полной информации',
31
        'deleted' => 'Удален',
32
        'imgs' => 'Фото',
33
        'date_create' => 'Дата создания'
34
    ];
35
    public static $cols = [
36
        //Основные параметры
37
        'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'],
38
        'image_file_id' => ['type' => 'image'],
39
        'name' => ['type' => 'text'],
40
        'alias' => ['type' => 'text'],
41
        'description' => ['type' => 'html'],
42
        'item_type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'],
43
        'best' => ['type' => 'bool'],
44
        'item_badge_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'badge'],
45
        'deleted' => ['type' => 'bool'],
46
        //Системные
47
        'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'],
48
        'weight' => ['type' => 'number'],
49
        'sales' => ['type' => 'number'],
50
        'imported' => ['type' => 'text'],
51
        'tree_path' => ['type' => 'text'],
52
        'search_index' => ['type' => 'text', 'logging' => false],
53
        'date_create' => ['type' => 'dateTime'],
54
        'widget' => ['type' => 'text'],
55
        'view' => ['type' => 'text'],
56
        //Менеджеры
57
        'options' => ['type' => 'dataManager', 'relation' => 'options'],
58
        'offers' => ['type' => 'dataManager', 'relation' => 'offers'],
59
        'imgs' => ['type' => 'dataManager', 'relation' => 'images'],
60
    ];
61
62
    public static function simpleItemHandler($request) {
63
        if ($request) {
64
            $item = new Item();
65
            $item->name = $request['name'];
66
            $item->description = $request['description'];
67
            $item->category_id = $request['category'];
68
            $item->save();
69
            if (!empty($_FILES['ActiveForm_simpleItem']['tmp_name']['Ecommerce\Item']['image'])) {
70
                $file_id = \App::$primary->files->upload([
71
                    'tmp_name' => $_FILES['ActiveForm_simpleItem']['tmp_name']['Ecommerce\Item']['image'],
72
                    'name' => $_FILES['ActiveForm_simpleItem']['name']['Ecommerce\Item']['image'],
73
                    'type' => $_FILES['ActiveForm_simpleItem']['type']['Ecommerce\Item']['image'],
74
                    'size' => $_FILES['ActiveForm_simpleItem']['size']['Ecommerce\Item']['image'],
75
                    'error' => $_FILES['ActiveForm_simpleItem']['error']['Ecommerce\Item']['image'],
76
                ], [
77
                    'upload_code' => 'activeForm:' . 'Ecommerce\Item' . ':' . $item->pk(),
78
                    'accept_group' => 'image'
79
                ]);
80
                if ($file_id) {
81
                    $item->image_file_id = $file_id;
82
                    $item->save();
83
                }
84
            }
85 View Code Duplication
            if (!empty($request['options']['option'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86
                foreach ($request['options']['option'] as $key => $option_id) {
87
                    $param = new Item\Param();
88
                    $param->item_id = $item->id;
89
                    $param->value = $request['options']['value'][$key];
90
                    $param->item_option_id = $option_id;
91
                    $param->save();
92
                }
93
            }
94
            $offer = new Item\Offer();
95
            $offer->item_id = $item->id;
96
            $offer->save();
97 View Code Duplication
            if (!empty($request['offerOptions']['option'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
98
                foreach ($request['offerOptions']['option'] as $key => $option_id) {
99
                    $param = new Item\Offer\Param();
100
                    $param->item_offer_id = $offer->id;
101
                    $param->value = $request['offerOptions']['value'][$key];
102
                    $param->item_offer_option_id = $option_id;
103
                    $param->save();
104
                }
105
            }
106
            $price = new Item\Offer\Price();
107
            $price->price = $request['price'];
108
            $price->item_offer_id = $offer->id;
109
            $price->currency_id = $request['currency'];
110
            $price->save();
111
        }
112
    }
113
114
    public static $dataManagers = [
115
        'manager' => [
116
            'name' => 'Товары',
117
            'cols' => [
118
                'name',
119
                'imgs',
120
                'category_id',
121
                'item_type_id',
122
                'best',
123
                'deleted',
124
                'options',
125
                'offers',
126
                'date_create'
127
            ],
128
            'categorys' => [
129
                'model' => 'Ecommerce\Category',
130
            ],
131
            'sortable' => ['date_create'],
132
            'preSort' => [
133
                'date_create' => 'desc'
134
            ],
135
            'filters' => [
136
                'name', 'best', 'deleted', 'date_create'
137
            ],
138
            'sortMode' => true
139
        ]
140
    ];
141
    public static $forms = [
142
        'manager' => [
143
            'map' => [
144
                ['name', 'alias'],
145
                ['category_id', 'item_type_id', 'deleted'],
146
                ['widget', 'view'],
147
                ['best', 'item_badge_id', 'image_file_id'],
148
                ['description'],
149
                ['imgs'],
150
                ['options'],
151
                ['offers'],
152
            ]
153
        ],
154
        'simpleItem' => [
155
            'options' => [
156
                'access' => [
157
                    'groups' => [
158
                        3
159
                    ]
160
                ],
161
            ],
162
            'name' => 'Простой товар с ценой',
163
            'inputs' => [
164
                'name' => ['type' => 'text'],
165
                'description' => ['type' => 'html'],
166
                'category' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Category', 'label' => 'Категория'],
167
                'image' => ['type' => 'image', 'label' => 'Изображение'],
168
                'price' => ['type' => 'text', 'label' => 'Цена'],
169
                'currency' => ['type' => 'select', 'source' => 'model', 'model' => 'Money\Currency', 'label' => 'Валюта'],
170
                'options' => ['type' => 'dynamicList', 'source' => 'options', 'options' => [
171
                    'inputs' => [
172
                        'option' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Item\Option', 'label' => 'Свойство'],
173
                        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType', 'label' => 'Значение'],
174
                    ]
175
                ]
176
                ],
177
                'offerOptions' => ['type' => 'dynamicList', 'source' => 'options', 'options' => [
178
                    'inputs' => [
179
                        'option' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Item\Offer\Option', 'label' => 'Свойство предложения'],
180
                        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType', 'label' => 'Значение'],
181
                    ]
182
                ], 'label' => 'Параметры предложения'
183
                ]
184
            ],
185
            'map' => [
186
                ['name', 'category'],
187
                ['description'],
188
                ['image'],
189
                ['price', 'currency'],
190
                ['options'],
191
                ['offerOptions'],
192
            ],
193
            'handler' => 'simpleItemHandler'
194
        ]
195
    ];
196
197
    public function realType() {
198
        if ($this->option && $this->option->type) {
199
            $type = $this->option->type;
200
201
            if ($type == 'select') {
202
                return [
203
                    'type' => 'select',
204
                    'source' => 'relation',
205
                    'relation' => 'option:items',
206
                ];
207
            }
208
            return $type;
209
        }
210
        return 'text';
211
    }
212
213
    public static function indexes() {
214
        return [
215
            'ecommerce_item_item_category_id' => [
216
                'type' => 'INDEX',
217
                'cols' => [
218
                    'item_category_id'
219
                ]
220
            ],
221
            'inji_ecommerce_item_item_tree_path' => [
222
                'type' => 'INDEX',
223
                'cols' => [
224
                    'item_tree_path(255)'
225
                ]
226
            ],
227
            'ecommerce_item_item_search_index' => [
228
                'type' => 'INDEX',
229
                'cols' => [
230
                    'item_search_index(255)'
231
                ]
232
            ],
233
        ];
234
    }
235
236
    public function beforeSave() {
237
238
        if ($this->id) {
239
            $this->search_index = $this->name . ' ';
240
            if ($this->category) {
241
                $this->search_index .= $this->category->name . ' ';
242
            }
243 View Code Duplication
            if ($this->options) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
244
                foreach ($this->options as $option) {
245
                    if ($option->item_option_searchable && $option->value) {
246
                        if ($option->item_option_type != 'select') {
247
                            $this->search_index .= $option->value . ' ';
248
                        } elseif (!empty($option->option->items[$option->value])) {
249
                            $option->option->items[$option->value]->value . ' ';
250
                        }
251
                    }
252
                }
253
            }
254
            if ($this->offers) {
255
                foreach ($this->offers as $offer) {
256 View Code Duplication
                    if ($offer->options) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
257
                        foreach ($offer->options as $option) {
258
                            if ($option->item_offer_option_searchable && $option->value) {
259
                                if ($option->item_offer_option_type != 'select') {
260
                                    $this->search_index .= $option->value . ' ';
261
                                } elseif (!empty($option->option->items[$option->value])) {
262
                                    $option->option->items[$option->value]->value . ' ';
263
                                }
264
                            }
265
                        }
266
                    }
267
                }
268
            }
269
        }
270
    }
271
272
    public static function relations() {
273
274
        return [
275
            'badge' => [
276
                'model' => 'Ecommerce\Item\Badge',
277
                'col' => 'item_badge_id'
278
            ],
279
            'category' => [
280
                'model' => 'Ecommerce\Category',
281
                'col' => 'category_id'
282
            ],
283
            'options' => [
284
                'type' => 'many',
285
                'model' => 'Ecommerce\Item\Param',
286
                'col' => 'item_id',
287
                'resultKey' => 'item_option_id',
288
                'join' => [Item\Option::table(), Item\Option::index() . ' = ' . Item\Param::colPrefix() . Item\Option::index()]
289
            ],
290
            'offers' => [
291
                'type' => 'many',
292
                'model' => 'Ecommerce\Item\Offer',
293
                'col' => 'item_id',
294
            ],
295
            'type' => [
296
                'model' => 'Ecommerce\Item\Type',
297
                'col' => 'item_type_id',
298
            ],
299
            'image' => [
300
                'model' => 'Files\File',
301
                'col' => 'image_file_id'
302
            ],
303
            'images' => [
304
                'type' => 'many',
305
                'model' => 'Ecommerce\Item\Image',
306
                'col' => 'item_id'
307
            ],
308
            'user' => [
309
                'model' => 'Users\User',
310
                'col' => 'user_id'
311
            ]
312
        ];
313
    }
314
315
    public function getPrice() {
316
        $offers = $this->offers(['key' => false]);
317
        $curPrice = null;
318
        if (!$offers) {
319
            return false;
320
        }
321
        foreach ($offers[0]->prices as $price) {
322
            if (!$price->type) {
323
                $curPrice = $price;
324
            } elseif (
325
                (!$price->type->roles && !$curPrice) ||
326
                ($price->type->roles && !$curPrice && strpos($price->type->roles, "|" . \Users\User::$cur->role_id . "|") !== false)
327
            ) {
328
                $curPrice = $price;
329
            }
330
        }
331
        return $curPrice;
332
    }
333
334
    public function name() {
335
        if (!empty(\App::$primary->ecommerce->config['item_option_as_name'])) {
336
            $param = Item\Param::get([['item_id', $this->id], ['item_option_id', \App::$primary->ecommerce->config['item_option_as_name']]]);
337
            if ($param && $param->value) {
338
                return $param->value;
339
            }
340
        }
341
        return $this->name;
342
    }
343
344
    public function beforeDelete() {
345
        if ($this->id) {
346
            if ($this->options) {
347
                foreach ($this->options as $option) {
348
                    $option->delete();
349
                }
350
            }
351
            if ($this->offers) {
352
                foreach ($this->offers as $offer) {
353
                    $offer->delete();
354
                }
355
            }
356
            if ($this->image) {
357
                $this->image->delete();
358
            }
359
        }
360
    }
361
362
    public function getHref() {
363
        return "/ecommerce/view/{$this->pk()}";
364
    }
365
366
    public function inFav() {
367
        if (\Users\User::$cur->id) {
368
            $fav = \Ecommerce\Favorite::get([['user_id', \Users\User::$cur->id], ['item_id', $this->id]]);
369
            return (bool)$fav;
370 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
371
            $favs = !empty($_COOKIE['ecommerce_favitems']) ? json_decode($_COOKIE['ecommerce_favitems'], true) : [];
372
            return in_array($this->id, $favs);
373
        }
374
    }
375
}