Test Failed
Push — master ( b1d693...eecb8b )
by Alexey
04:33
created

EcommerceController   F

Complexity

Total Complexity 84

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 20

Importance

Changes 0
Metric Value
dl 0
loc 245
rs 1.5789
c 0
b 0
f 0
wmc 84
lcom 1
cbo 20

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dashboardAction() 0 4 1
B configureAction() 0 180 4
A reCalcCategoriesAction() 0 17 3
A reSearchIndexAction() 0 19 3
A newOrdersSubscribeAction() 0 3 1
A closeCartAction() 0 13 3

How to fix   Complexity   

Complex Class

Complex classes like EcommerceController 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 EcommerceController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Ecommerce admin controller
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
class EcommerceController extends adminController {
12
13
    public function dashboardAction() {
14
        $this->view->setTitle('Онлайн магазин');
15
        $this->view->page();
16
    }
17
18
    public function configureAction() {
19
        $options = [
20
            [
21
                'type' => 'checkbox',
22
                'name' => 'view_empty_warehouse',
23
                'label' => 'Продавать отсутствующие товары',
24
                'options' => [
25
                    'value' => Tools::defValue(App::$cur->ecommerce->config['sell_over_warehouse'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26
                ]
27
            ],
28
            [
29
                'type' => 'checkbox',
30
                'name' => 'view_empty_image',
31
                'label' => 'Показывать товары без изображения',
32
                'options' => [
33
                    'value' => Tools::defValue(App::$cur->ecommerce->config['view_empty_image'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34
                ]
35
            ],
36
            [
37
                'type' => 'checkbox',
38
                'name' => 'sell_empty_warehouse',
39
                'label' => 'Продавать без остатоков на складе',
40
                'options' => [
41
                    'value' => Tools::defValue(App::$cur->ecommerce->config['sell_empty_warehouse'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
                ]
43
            ],
44
            [
45
                'type' => 'checkbox',
46
                'name' => 'sell_over_warehouse',
47
                'label' => 'Продавать сверх остатоков на складе',
48
                'options' => [
49
                    'value' => Tools::defValue(App::$cur->ecommerce->config['sell_over_warehouse'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
                ]
51
            ],
52
            [
53
                'type' => 'checkbox',
54
                'label' => 'Показывать товары с нулевой ценой',
55
                'name' => 'show_zero_price',
56
                'options' => [
57
                    'value' => Tools::defValue(App::$cur->ecommerce->config['show_zero_price'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
                ]
59
            ],
60
            [
61
                'type' => 'checkbox',
62
                'name' => 'show_without_price',
63
                'label' => 'Показывать товары без цен',
64
                'options' => [
65
                    'value' => Tools::defValue(App::$cur->ecommerce->config['show_without_price'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
                ]
67
            ],
68
            [
69
                'type' => 'checkbox',
70
                'name' => 'filtersInLast',
71
                'label' => 'Показывать Фильтры только текущей и дочерних категорий',
72
                'options' => [
73
                    'value' => Tools::defValue(App::$cur->ecommerce->config['filtersInLast'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
                ]
75
            ],
76
            [
77
                'type' => 'checkbox',
78
                'name' => 'isset_sort',
79
                'label' => 'Возможность сортировки по наличию',
80
                'options' => [
81
                    'value' => Tools::defValue(App::$cur->ecommerce->config['isset_sort'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
                ]
83
            ],
84
            [
85
                'type' => 'checkbox',
86
                'name' => 'list_all',
87
                'label' => 'Возможность вывести все товары',
88
                'options' => [
89
                    'value' => Tools::defValue(App::$cur->ecommerce->config['list_all'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
                ]
91
            ],
92
            [
93
                'type' => 'checkbox',
94
                'name' => 'cartAddToggle',
95
                'label' => 'Удаление из корзины при повторном добавлении товара',
96
                'options' => [
97
                    'value' => Tools::defValue(App::$cur->ecommerce->config['cartAddToggle'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
                ]
99
            ],
100
            [
101
                'type' => 'checkbox',
102
                'name' => 'defaultNeedDelivery',
103
                'label' => 'По умолчанию для всех товаров доставка осуществляется',
104
                'options' => [
105
                    'value' => Tools::defValue(App::$cur->ecommerce->config['defaultNeedDelivery'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
                ]
107
            ],
108
            [
109
                'type' => 'checkbox',
110
                'name' => 'catalogPresentPage',
111
                'label' => 'Заглавная страница магазина',
112
                'options' => [
113
                    'value' => Tools::defValue(App::$cur->ecommerce->config['catalogPresentPage'], false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
                ]
115
            ],
116
            [
117
                'type' => 'text',
118
                'name' => 'notify_mail',
119
                'label' => 'E-mail оповещений о новых заказах',
120
                'options' => [
121
                    'value' => Tools::defValue(App::$cur->ecommerce->config['notify_mail'], '')
122
                ]
123
            ],
124
            [
125
                'type' => 'select',
126
                'name' => 'defaultCategoryView',
127
                'label' => 'Стандартный вид категории',
128
                'options' => [
129
                    'value' => Tools::defValue(App::$cur->ecommerce->config['defaultCategoryView'], 'itemList'),
130
                    'values' => App::$cur->ecommerce->viewsCategoryList()
131
                ]
132
            ],
133
            [
134
                'type' => 'text',
135
                'name' => 'orderPrefix',
136
                'label' => 'Префикс для номеров заказов',
137
                'options' => [
138
                    'value' => Tools::defValue(App::$cur->ecommerce->config['orderPrefix'], '')
139
                ]
140
            ],
141
            [
142
                'type' => 'number',
143
                'name' => 'default_limit',
144
                'label' => 'Товаров на страницу',
145
                'options' => [
146
                    'value' => Tools::defValue(App::$cur->ecommerce->config['default_limit'], 18)
147
                ]
148
            ],
149
            [
150
                'type' => 'text',
151
                'name' => 'defaultCategoryTemplate',
152
                'label' => 'Шаблон магазина',
153
                'options' => [
154
                    'value' => Tools::defValue(App::$cur->ecommerce->config['defaultCategoryTemplate'], 'current')
155
                ]
156
            ],
157
        ];
158
        if (App::$cur->money) {
159
            $options[] = [
160
                'type' => 'select',
161
                'name' => 'defaultCurrency',
162
                'label' => 'Валюта по умолчанию',
163
                'options' => [
164
                    'value' => Tools::defValue(App::$cur->ecommerce->config['defaultCategoryView'], ''),
165
                    'values' => ['' => 'Выберите'] + \Money\Currency::getList()
166
                ]
167
            ];
168
        }
169
        if (!empty($_POST['config'])) {
170
            $config = App::$cur->ecommerce->config;
171
            foreach ($options as $option) {
172
                $config[$option['name']] = Tools::defValue($_POST['config'][$option['name']], false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
173
            }
174
            Config::save('module', $config, 'Ecommerce');
175
            Tools::redirect('/admin/ecommerce/configure', 'Настройки были изменены', 'success');
176
        }
177
178
        $managers = [
179
            'Ecommerce\Delivery',
180
            'Ecommerce\PayType',
181
            'Ecommerce\Warehouse',
182
            'Ecommerce\Unit',
183
            'Ecommerce\Card',
184
            'Ecommerce\Discount',
185
            'Ecommerce\Cart\Stage',
186
            'Ecommerce\Item\Type',
187
            'Ecommerce\Item\Option',
188
            'Ecommerce\Item\Offer\Option',
189
            'Ecommerce\Item\Offer\Price\Type',
190
            'Ecommerce\UserAdds\Field',
191
            'Ecommerce\Cart\Status',
192
            'Ecommerce\Delivery\Field',
193
            'Ecommerce\Delivery\Provider',
194
        ];
195
        $this->view->setTitle('Настройки магазина');
196
        $this->view->page(['data' => compact('managers', 'options')]);
197
    }
198
199
    public function reCalcCategoriesAction() {
200
        set_time_limit(0);
201
        Model::$logging = false;
202
        ini_set('memory_limit', '-1');
203
        $categories = \Ecommerce\Category::getList();
204
        foreach ($categories as $category) {
205
            if (!$category->catalogs) {
206
                $time = microtime(true);
207
                echo $category->id . " start->";
208
                flush();
209
                $category->calcItemsCount(true, false);
210
                echo round(microtime(true) - $time, 2) . "<br />";
211
                flush();
212
            }
213
        }
214
        Tools::redirect('/admin/ecommerce/configure', 'Данные о кол-ве обновлены');
215
    }
216
217
    public function reSearchIndexAction($i = 0) {
218
        set_time_limit(0);
219
        Model::$logging = false;
220
        ini_set('memory_limit', '-1');
221
        $count = 100;
222
        $items = Ecommerce\Item::getList(['start' => $i * $count, 'limit' => $count]);
223
        if (!$items) {
224
            Tools::redirect('/admin/ecommerce/configure', 'Поисковый индекс обновлен');
225
        } else {
226
            $i++;
227
            foreach ($items as $key => $item) {
228
                $item->save();
229
                unset($items[$key]);
230
                unset($item);
231
            }
232
            echo 'Происходит процесс индексации: проиндексировано ' . $i * $count;
233
            echo '<script>setTimeout(function(){window.location="/admin/ecommerce/reSearchIndex/' . $i . '"},100)</script>';
234
        }
235
    }
236
237
    public function newOrdersSubscribeAction() {
238
        $this->Notifications->subscribe('Ecommerce-orders');
239
    }
240
241
    public function closeCartAction($cartId = 0) {
242
        $cart = Ecommerce\Cart::get((int) $cartId);
243
        $result = new Server\Result();
244
        if ($cart && $cart->cart_status_id != 5) {
245
            $cart->cart_status_id = 5;
246
            $cart->save();
247
            $result->successMsg = 'Заказ был завершен';
248
            $result->send();
249
        }
250
        $result->success = false;
251
        $result->content = 'Такая корзина не найдена';
252
        $result->send();
253
    }
254
255
}