Passed
Push — develop ( b40d4d...aa81c9 )
by Mykola
04:21
created

ControllerCheckoutShippingMethod::index()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 71
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 71
rs 7.6666
cc 10
nc 32
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* 	Sunrise CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Sunrise CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ControllerCheckoutShippingMethod extends \Sunrise\Engine\Core\Controller
24
{
25
    public function index()
26
    {
27
        $this->load->language('checkout/checkout');
0 ignored issues
show
Bug Best Practice introduced by
The property load does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
28
29
        if (isset($this->session->data['shipping_address'])) {
0 ignored issues
show
Bug Best Practice introduced by
The property session does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
30
            // Shipping Methods
31
            $method_data = array();
32
33
            $this->load->model('extension/extension');
34
35
            $results = $this->model_extension_extension->getExtensions('shipping');
0 ignored issues
show
Bug Best Practice introduced by
The property model_extension_extension does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
36
37
            foreach ($results as $result) {
38
                if ($this->config->get($result['code'] . '_status')) {
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
39
                    $this->load->model('extension/shipping/' . $result['code']);
40
41
                    $quote = $this->{'model_extension_shipping_' . $result['code']}->getQuote($this->session->data['shipping_address']);
42
43
                    if ($quote) {
44
                        $method_data[$result['code']] = array(
45
                            'title'      => $quote['title'],
46
                            'quote'      => $quote['quote'],
47
                            'sort_order' => $quote['sort_order'],
48
                            'error'      => $quote['error']
49
                        );
50
                    }
51
                }
52
            }
53
54
            $sort_order = array();
55
56
            foreach ($method_data as $key => $value) {
57
                $sort_order[$key] = $value['sort_order'];
58
            }
59
60
            array_multisort($sort_order, SORT_ASC, $method_data);
61
62
            $this->session->data['shipping_methods'] = $method_data;
63
        }
64
65
        $data['text_shipping_method'] = $this->language->get('text_shipping_method');
0 ignored issues
show
Bug Best Practice introduced by
The property language does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
        $data['text_comments'] = $this->language->get('text_comments');
67
        $data['text_loading'] = $this->language->get('text_loading');
68
69
        $data['button_continue'] = $this->language->get('button_continue');
70
71
        if (empty($this->session->data['shipping_methods'])) {
72
            $data['error_warning'] = sprintf($this->language->get('error_no_shipping'), $this->url->link('information/contact'));
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
73
        } else {
74
            $data['error_warning'] = '';
75
        }
76
77
        if (isset($this->session->data['shipping_methods'])) {
78
            $data['shipping_methods'] = $this->session->data['shipping_methods'];
79
        } else {
80
            $data['shipping_methods'] = array();
81
        }
82
83
        if (isset($this->session->data['shipping_method']['code'])) {
84
            $data['code'] = $this->session->data['shipping_method']['code'];
85
        } else {
86
            $data['code'] = '';
87
        }
88
89
        if (isset($this->session->data['comment'])) {
90
            $data['comment'] = $this->session->data['comment'];
91
        } else {
92
            $data['comment'] = '';
93
        }
94
95
        $this->response->setOutput($this->load->view('checkout/shipping_method', $data));
0 ignored issues
show
Bug Best Practice introduced by
The property response does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
96
    }
97
98
    public function save()
99
    {
100
        $this->load->language('checkout/checkout');
0 ignored issues
show
Bug Best Practice introduced by
The property load does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
101
102
        $json = array();
103
104
        // Validate if shipping is required. If not the customer should not have reached this page.
105
        if (!$this->cart->hasShipping()) {
0 ignored issues
show
Bug Best Practice introduced by
The property cart does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
106
            $json['redirect'] = $this->url->link('checkout/checkout', '', true);
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
107
        }
108
109
        // Validate if shipping address has been set.
110
        if (!isset($this->session->data['shipping_address'])) {
0 ignored issues
show
Bug Best Practice introduced by
The property session does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
111
            $json['redirect'] = $this->url->link('checkout/checkout', '', true);
112
        }
113
114
        // Validate cart has products and has stock.
115
        if ((!$this->cart->hasProducts()) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
116
            $json['redirect'] = $this->url->link('checkout/cart');
117
        }
118
119
        // Validate minimum quantity requirements.
120
        $products = $this->cart->getProducts();
121
122
        foreach ($products as $product) {
123
            $product_total = 0;
124
125
            foreach ($products as $product_2) {
126
                if ($product_2['product_id'] == $product['product_id']) {
127
                    $product_total += $product_2['quantity'];
128
                }
129
            }
130
131
            if ($product['minimum'] > $product_total) {
132
                $json['redirect'] = $this->url->link('checkout/cart');
133
134
                break;
135
            }
136
        }
137
138
        if (!isset($this->request->post['shipping_method'])) {
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
139
            $json['error']['warning'] = $this->language->get('error_shipping');
0 ignored issues
show
Bug Best Practice introduced by
The property language does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
140
        } else {
141
            $shipping = explode('.', $this->request->post['shipping_method']);
142
143
            if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
144
                $json['error']['warning'] = $this->language->get('error_shipping');
145
            }
146
        }
147
148
        if (!$json) {
149
            $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
150
151
            $this->session->data['comment'] = strip_tags($this->request->post['comment']);
152
        }
153
154
        $this->response->addHeader('Content-Type: application/json');
0 ignored issues
show
Bug Best Practice introduced by
The property response does not exist on ControllerCheckoutShippingMethod. Since you implemented __get, consider adding a @property annotation.
Loading history...
155
        $this->response->setOutput(json_encode($json));
156
    }
157
}
158