Test Failed
Push — master ( 61f61c...455e4b )
by Alexey
04:03
created

EcommerceController   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 0
Metric Value
wmc 72
lcom 1
cbo 19
dl 0
loc 90
rs 2.4812
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dashboardAction() 0 4 1
D configureAction() 0 36 9
A reBlockIndexAction() 0 8 2
A reSearchIndexAction() 0 17 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
        if (!empty($_POST['config'])) {
20
            $config = App::$cur->ecommerce->config;
21
            $config['view_empty_warehouse'] = empty($_POST['config']['view_empty_warehouse']) ? false : true;
22
            $config['view_empty_image'] = empty($_POST['config']['view_empty_image']) ? false : true;
23
            $config['sell_empty_warehouse'] = empty($_POST['config']['sell_empty_warehouse']) ? false : true;
24
            $config['sell_over_warehouse'] = empty($_POST['config']['sell_over_warehouse']) ? false : true;
25
            $config['notify_mail'] = $_POST['config']['notify_mail'];
26
            $config['defaultCategoryView'] = $_POST['config']['defaultCategoryView'];
27
            $config['defaultCurrency'] = $_POST['config']['defaultCurrency'];
28
            $config['orderPrefix'] = $_POST['config']['orderPrefix'];
29
            $config['show_zero_price'] = empty($_POST['config']['show_zero_price']) ? false : true;
30
            $config['show_without_price'] = empty($_POST['config']['show_without_price']) ? false : true;
31
            $config['filtersInLast'] = empty($_POST['config']['filtersInLast']) ? false : true;
32
            Config::save('module', $config, 'Ecommerce');
33
            Tools::redirect('/admin/ecommerce/configure', 'Настройки были изменены', 'success');
34
        }
35
        $managers = [
36
            'Ecommerce\Delivery',
37
            'Ecommerce\PayType',
38
            'Ecommerce\Warehouse',
39
            'Ecommerce\Unit',
40
            'Ecommerce\Card',
41
            'Ecommerce\Discount',
42
            'Ecommerce\Cart\Stage',
43
            'Ecommerce\Item\Type',
44
            'Ecommerce\Item\Option',
45
            'Ecommerce\Item\Offer\Option',
46
            'Ecommerce\Item\Offer\Price\Type',
47
            'Ecommerce\UserAdds\Field',
48
            'Ecommerce\Cart\Status',
49
            'Ecommerce\Delivery\Field',
50
        ];
51
        $this->view->setTitle('Настройки магазина');
52
        $this->view->page(['data' => compact('managers')]);
53
    }
54
55
    public function reBlockIndexAction() {
56
        set_time_limit(0);
57
        $carts = Cart::getList();
58
        foreach ($carts as $cart) {
59
            $cart->save();
60
        }
61
        Tools::redirect('/admin/ecommerce/configure', 'Данные о блокировках обновлены');
62
    }
63
64
    public function reSearchIndexAction($i = 0) {
65
        set_time_limit(0);
66
        $count = 100;
67
        $items = Ecommerce\Item::getList(['start' => $i * $count, 'limit' => $count]);
68
        if (!$items) {
69
            Tools::redirect('/admin/ecommerce/configure', 'Поисковый индекс обновлен');
70
        } else {
71
            $i++;
72
            foreach ($items as $key => $item) {
73
                $item->save();
74
                unset($items[$key]);
75
                unset($item);
76
            }
77
            echo 'Происходит процесс индексации: проиндексировано ' . $i * $count;
78
            Tools::redirect('/admin/ecommerce/reSearchIndex/' . $i);
79
        }
80
    }
81
82
    public function newOrdersSubscribeAction() {
83
        $this->Notifications->subscribe('Ecommerce-orders');
84
    }
85
86
    public function closeCartAction($cartId = 0) {
87
        $cart = Ecommerce\Cart::get((int) $cartId);
88
        $result = new Server\Result();
89
        if ($cart && $cart->cart_status_id != 5) {
90
            $cart->cart_status_id = 5;
91
            $cart->save();
92
            $result->successMsg = 'Заказ был завершен';
93
            $result->send();
94
        }
95
        $result->success = false;
96
        $result->content = 'Такая корзина не найдена';
97
        $result->send();
98
    }
99
100
}