ControllerCheckoutGuestShipping::index()   F
last analyzed

Complexity

Conditions 11
Paths 1024

Size

Total Lines 91
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
c 1
b 0
f 0
dl 0
loc 91
rs 3.3927
cc 11
nc 1024
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
/* 	Divine 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 Divine 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 ControllerCheckoutGuestShipping extends \Divine\Core\Engine\Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Bug introduced by
The type Divine\Core\Engine\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
{
25
    public function index()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
26
    {
27
        $this->load->language('checkout/checkout');
28
29
        $data['text_select'] = $this->language->get('text_select');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
30
        $data['text_none'] = $this->language->get('text_none');
31
        $data['text_loading'] = $this->language->get('text_loading');
32
33
        $data['entry_firstname'] = $this->language->get('entry_firstname');
34
        $data['entry_lastname'] = $this->language->get('entry_lastname');
35
        $data['entry_company'] = $this->language->get('entry_company');
36
        $data['entry_address_1'] = $this->language->get('entry_address_1');
37
        $data['entry_address_2'] = $this->language->get('entry_address_2');
38
        $data['entry_postcode'] = $this->language->get('entry_postcode');
39
        $data['entry_city'] = $this->language->get('entry_city');
40
        $data['entry_country'] = $this->language->get('entry_country');
41
        $data['entry_zone'] = $this->language->get('entry_zone');
42
43
        $data['button_continue'] = $this->language->get('button_continue');
44
        $data['button_upload'] = $this->language->get('button_upload');
45
46
        if (isset($this->session->data['shipping_address']['firstname'])) {
47
            $data['firstname'] = $this->session->data['shipping_address']['firstname'];
48
        } else {
49
            $data['firstname'] = '';
50
        }
51
52
        if (isset($this->session->data['shipping_address']['lastname'])) {
53
            $data['lastname'] = $this->session->data['shipping_address']['lastname'];
54
        } else {
55
            $data['lastname'] = '';
56
        }
57
58
        if (isset($this->session->data['shipping_address']['company'])) {
59
            $data['company'] = $this->session->data['shipping_address']['company'];
60
        } else {
61
            $data['company'] = '';
62
        }
63
64
        if (isset($this->session->data['shipping_address']['address_1'])) {
65
            $data['address_1'] = $this->session->data['shipping_address']['address_1'];
66
        } else {
67
            $data['address_1'] = '';
68
        }
69
70
        if (isset($this->session->data['shipping_address']['address_2'])) {
71
            $data['address_2'] = $this->session->data['shipping_address']['address_2'];
72
        } else {
73
            $data['address_2'] = '';
74
        }
75
76
        if (isset($this->session->data['shipping_address']['postcode'])) {
77
            $data['postcode'] = $this->session->data['shipping_address']['postcode'];
78
        } else {
79
            $data['postcode'] = '';
80
        }
81
82
        if (isset($this->session->data['shipping_address']['city'])) {
83
            $data['city'] = $this->session->data['shipping_address']['city'];
84
        } else {
85
            $data['city'] = '';
86
        }
87
88
        if (isset($this->session->data['shipping_address']['country_id'])) {
89
            $data['country_id'] = $this->session->data['shipping_address']['country_id'];
90
        } else {
91
            $data['country_id'] = $this->config->get('config_country_id');
92
        }
93
94
        if (isset($this->session->data['shipping_address']['zone_id'])) {
95
            $data['zone_id'] = $this->session->data['shipping_address']['zone_id'];
96
        } else {
97
            $data['zone_id'] = '';
98
        }
99
100
        $this->load->model('localisation/country');
101
102
        $data['countries'] = $this->model_localisation_country->getCountries();
103
104
        // Custom Fields
105
        $this->load->model('account/custom_field');
106
107
        $data['custom_fields'] = $this->model_account_custom_field->getCustomFields($this->session->data['guest']['customer_group_id']);
108
109
        if (isset($this->session->data['shipping_address']['custom_field'])) {
110
            $data['address_custom_field'] = $this->session->data['shipping_address']['custom_field'];
111
        } else {
112
            $data['address_custom_field'] = array();
113
        }
114
115
        $this->response->setOutput($this->load->view('checkout/guest_shipping', $data));
116
    }
117
118
    public function save()
119
    {
120
        $this->load->language('checkout/checkout');
121
122
        $json = array();
123
124
        // Validate if customer is logged in.
125
        if ($this->customer->isLogged()) {
126
            $json['redirect'] = $this->url->link('checkout/checkout', '', true);
127
        }
128
129
        // Validate cart has products and has stock.
130
        if ((!$this->cart->hasProducts()) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
131
            $json['redirect'] = $this->url->link('checkout/cart');
132
        }
133
134
        // Check if guest checkout is available.
135
        if (!$this->config->get('config_checkout_guest') || $this->config->get('config_customer_price') || $this->cart->hasDownload()) {
136
            $json['redirect'] = $this->url->link('checkout/checkout', '', true);
137
        }
138
139
        if (!$json) {
140
            if ((\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) > 32)) {
141
                $json['error']['firstname'] = $this->language->get('error_firstname');
142
            }
143
144
            if ((\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) > 32)) {
145
                $json['error']['lastname'] = $this->language->get('error_lastname');
146
            }
147
148
            if ((\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) < 3) || (\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) > 128)) {
149
                $json['error']['address_1'] = $this->language->get('error_address_1');
150
            }
151
152
            if ((\voku\helper\UTF8::strlen(trim($this->request->post['city'])) < 2) || (\voku\helper\UTF8::strlen(trim($this->request->post['city'])) > 128)) {
153
                $json['error']['city'] = $this->language->get('error_city');
154
            }
155
156
            $this->load->model('localisation/country');
157
158
            $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
159
160
            if ($country_info && $country_info['postcode_required'] && (\voku\helper\UTF8::strlen(trim($this->request->post['postcode'])) < 2 || \voku\helper\UTF8::strlen(trim($this->request->post['postcode'])) > 10)) {
161
                $json['error']['postcode'] = $this->language->get('error_postcode');
162
            }
163
164
            if ($this->request->post['country_id'] == '') {
165
                $json['error']['country'] = $this->language->get('error_country');
166
            }
167
168
            if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '' || !is_numeric($this->request->post['zone_id'])) {
169
                $json['error']['zone'] = $this->language->get('error_zone');
170
            }
171
172
            // Custom field validation
173
            $this->load->model('account/custom_field');
174
175
            $custom_fields = $this->model_account_custom_field->getCustomFields($this->session->data['guest']['customer_group_id']);
176
177
            foreach ($custom_fields as $custom_field) {
178
                if (($custom_field['location'] == 'address') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
179
                    $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
180
                } elseif (($custom_field['location'] == 'address') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
181
                    $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
182
                }
183
            }
184
        }
185
186
        if (!$json) {
187
            $this->session->data['shipping_address']['firstname'] = $this->request->post['firstname'];
188
            $this->session->data['shipping_address']['lastname'] = $this->request->post['lastname'];
189
            $this->session->data['shipping_address']['company'] = $this->request->post['company'];
190
            $this->session->data['shipping_address']['address_1'] = $this->request->post['address_1'];
191
            $this->session->data['shipping_address']['address_2'] = $this->request->post['address_2'];
192
            $this->session->data['shipping_address']['postcode'] = $this->request->post['postcode'];
193
            $this->session->data['shipping_address']['city'] = $this->request->post['city'];
194
            $this->session->data['shipping_address']['country_id'] = $this->request->post['country_id'];
195
            $this->session->data['shipping_address']['zone_id'] = $this->request->post['zone_id'];
196
197
            $this->load->model('localisation/country');
198
199
            $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
200
201
            if ($country_info) {
202
                $this->session->data['shipping_address']['country'] = $country_info['name'];
203
                $this->session->data['shipping_address']['iso_code_2'] = $country_info['iso_code_2'];
204
                $this->session->data['shipping_address']['iso_code_3'] = $country_info['iso_code_3'];
205
                $this->session->data['shipping_address']['address_format'] = $country_info['address_format'];
206
            } else {
207
                $this->session->data['shipping_address']['country'] = '';
208
                $this->session->data['shipping_address']['iso_code_2'] = '';
209
                $this->session->data['shipping_address']['iso_code_3'] = '';
210
                $this->session->data['shipping_address']['address_format'] = '';
211
            }
212
213
            $this->load->model('localisation/zone');
214
215
            $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
216
217
            if ($zone_info) {
218
                $this->session->data['shipping_address']['zone'] = $zone_info['name'];
219
                $this->session->data['shipping_address']['zone_code'] = $zone_info['code'];
220
            } else {
221
                $this->session->data['shipping_address']['zone'] = '';
222
                $this->session->data['shipping_address']['zone_code'] = '';
223
            }
224
225
            if (isset($this->request->post['custom_field'])) {
226
                $this->session->data['shipping_address']['custom_field'] = $this->request->post['custom_field'];
227
            } else {
228
                $this->session->data['shipping_address']['custom_field'] = array();
229
            }
230
231
            unset($this->session->data['shipping_method']);
232
            unset($this->session->data['shipping_methods']);
233
        }
234
235
        $this->response->addHeader('Content-Type: application/json');
236
        $this->response->setOutput(json_encode($json));
237
    }
238
}
239