Passed
Push — master ( d15464...d64dd2 )
by Alexey
05:25
created

EcommerceController   C

Complexity

Total Complexity 47

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 6.6654
c 0
b 0
f 0
wmc 47
lcom 1
cbo 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dashboardAction() 0 6 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
        $forms = \Ecommerce\Item::$magicForms;
0 ignored issues
show
Unused Code introduced by
$forms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
16
        \Ecommerce\Item::$forms['simpleItem']['handler']();
17
        $this->view->page();
18
    }
19
20
    public function configureAction() {
1 ignored issue
show
Coding Style introduced by
configureAction uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

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