ControllerCheckoutGuest   F
last analyzed

Complexity

Total Complexity 72

Size/Duplication

Total Lines 362
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 72
eloc 236
c 0
b 0
f 0
dl 0
loc 362
rs 2.64

2 Methods

Rating   Name   Duplication   Size   Complexity  
F index() 0 163 24
F save() 0 195 48

How to fix   Complexity   

Complex Class

Complex classes like ControllerCheckoutGuest 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.

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 ControllerCheckoutGuest, and based on these observations, apply Extract Interface, too.

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 ControllerCheckoutGuest 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_your_details'] = $this->language->get('text_your_details');
32
        $data['text_your_account'] = $this->language->get('text_your_account');
33
        $data['text_your_address'] = $this->language->get('text_your_address');
34
        $data['text_loading'] = $this->language->get('text_loading');
35
36
        $data['entry_firstname'] = $this->language->get('entry_firstname');
37
        $data['entry_lastname'] = $this->language->get('entry_lastname');
38
        $data['entry_email'] = $this->language->get('entry_email');
39
        $data['entry_telephone'] = $this->language->get('entry_telephone');
40
        $data['entry_fax'] = $this->language->get('entry_fax');
41
        $data['entry_company'] = $this->language->get('entry_company');
42
        $data['entry_customer_group'] = $this->language->get('entry_customer_group');
43
        $data['entry_address_1'] = $this->language->get('entry_address_1');
44
        $data['entry_address_2'] = $this->language->get('entry_address_2');
45
        $data['entry_postcode'] = $this->language->get('entry_postcode');
46
        $data['entry_city'] = $this->language->get('entry_city');
47
        $data['entry_country'] = $this->language->get('entry_country');
48
        $data['entry_zone'] = $this->language->get('entry_zone');
49
        $data['entry_shipping'] = $this->language->get('entry_shipping');
50
51
        $data['button_continue'] = $this->language->get('button_continue');
52
        $data['button_upload'] = $this->language->get('button_upload');
53
54
        $data['customer_groups'] = array();
55
56
        if (is_array($this->config->get('config_customer_group_display'))) {
57
            $this->load->model('account/customer_group');
58
59
            $customer_groups = $this->model_account_customer_group->getCustomerGroups();
60
61
            foreach ($customer_groups as $customer_group) {
62
                if (in_array($customer_group['customer_group_id'], $this->config->get('config_customer_group_display'))) {
63
                    $data['customer_groups'][] = $customer_group;
64
                }
65
            }
66
        }
67
68
        if (isset($this->session->data['guest']['customer_group_id'])) {
69
            $data['customer_group_id'] = $this->session->data['guest']['customer_group_id'];
70
        } else {
71
            $data['customer_group_id'] = $this->config->get('config_customer_group_id');
72
        }
73
74
        if (isset($this->session->data['guest']['firstname'])) {
75
            $data['firstname'] = $this->session->data['guest']['firstname'];
76
        } else {
77
            $data['firstname'] = '';
78
        }
79
80
        if (isset($this->session->data['guest']['lastname'])) {
81
            $data['lastname'] = $this->session->data['guest']['lastname'];
82
        } else {
83
            $data['lastname'] = '';
84
        }
85
86
        if (isset($this->session->data['guest']['email'])) {
87
            $data['email'] = $this->session->data['guest']['email'];
88
        } else {
89
            $data['email'] = '';
90
        }
91
92
        if (isset($this->session->data['guest']['telephone'])) {
93
            $data['telephone'] = $this->session->data['guest']['telephone'];
94
        } else {
95
            $data['telephone'] = '';
96
        }
97
98
        if (isset($this->session->data['guest']['fax'])) {
99
            $data['fax'] = $this->session->data['guest']['fax'];
100
        } else {
101
            $data['fax'] = '';
102
        }
103
104
        if (isset($this->session->data['payment_address']['company'])) {
105
            $data['company'] = $this->session->data['payment_address']['company'];
106
        } else {
107
            $data['company'] = '';
108
        }
109
110
        if (isset($this->session->data['payment_address']['address_1'])) {
111
            $data['address_1'] = $this->session->data['payment_address']['address_1'];
112
        } else {
113
            $data['address_1'] = '';
114
        }
115
116
        if (isset($this->session->data['payment_address']['address_2'])) {
117
            $data['address_2'] = $this->session->data['payment_address']['address_2'];
118
        } else {
119
            $data['address_2'] = '';
120
        }
121
122
        if (isset($this->session->data['payment_address']['postcode'])) {
123
            $data['postcode'] = $this->session->data['payment_address']['postcode'];
124
        } elseif (isset($this->session->data['shipping_address']['postcode'])) {
125
            $data['postcode'] = $this->session->data['shipping_address']['postcode'];
126
        } else {
127
            $data['postcode'] = '';
128
        }
129
130
        if (isset($this->session->data['payment_address']['city'])) {
131
            $data['city'] = $this->session->data['payment_address']['city'];
132
        } else {
133
            $data['city'] = '';
134
        }
135
136
        if (isset($this->session->data['payment_address']['country_id'])) {
137
            $data['country_id'] = $this->session->data['payment_address']['country_id'];
138
        } elseif (isset($this->session->data['shipping_address']['country_id'])) {
139
            $data['country_id'] = $this->session->data['shipping_address']['country_id'];
140
        } else {
141
            $data['country_id'] = $this->config->get('config_country_id');
142
        }
143
144
        if (isset($this->session->data['payment_address']['zone_id'])) {
145
            $data['zone_id'] = $this->session->data['payment_address']['zone_id'];
146
        } elseif (isset($this->session->data['shipping_address']['zone_id'])) {
147
            $data['zone_id'] = $this->session->data['shipping_address']['zone_id'];
148
        } else {
149
            $data['zone_id'] = '';
150
        }
151
152
        $this->load->model('localisation/country');
153
154
        $data['countries'] = $this->model_localisation_country->getCountries();
155
156
        // Custom Fields
157
        $this->load->model('account/custom_field');
158
159
        $data['custom_fields'] = $this->model_account_custom_field->getCustomFields();
160
161
        if (isset($this->session->data['guest']['custom_field'])) {
162
            if (isset($this->session->data['guest']['custom_field'])) {
163
                $guest_custom_field = $this->session->data['guest']['custom_field'];
164
            } else {
165
                $guest_custom_field = array();
166
            }
167
168
            if (isset($this->session->data['payment_address']['custom_field'])) {
169
                $address_custom_field = $this->session->data['payment_address']['custom_field'];
170
            } else {
171
                $address_custom_field = array();
172
            }
173
174
            $data['guest_custom_field'] = $guest_custom_field + $address_custom_field;
175
        } else {
176
            $data['guest_custom_field'] = array();
177
        }
178
179
        $data['shipping_required'] = $this->cart->hasShipping();
180
181
        if (isset($this->session->data['guest']['shipping_address'])) {
182
            $data['shipping_address'] = $this->session->data['guest']['shipping_address'];
183
        } else {
184
            $data['shipping_address'] = true;
185
        }
186
187
        $this->response->setOutput($this->load->view('checkout/guest', $data));
188
    }
189
190
    public function save()
191
    {
192
        $this->load->language('checkout/checkout');
193
194
        $json = array();
195
196
        // Validate if customer is logged in.
197
        if ($this->customer->isLogged()) {
198
            $json['redirect'] = $this->url->link('checkout/checkout', '', true);
199
        }
200
201
        // Validate cart has products and has stock.
202
        if ((!$this->cart->hasProducts()) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
203
            $json['redirect'] = $this->url->link('checkout/cart');
204
        }
205
206
        // Check if guest checkout is available.
207
        if (!$this->config->get('config_checkout_guest') || $this->config->get('config_customer_price') || $this->cart->hasDownload()) {
208
            $json['redirect'] = $this->url->link('checkout/checkout', '', true);
209
        }
210
211
        if (!$json) {
212
            if ((\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) > 32)) {
213
                $json['error']['firstname'] = $this->language->get('error_firstname');
214
            }
215
216
            if ((\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) > 32)) {
217
                $json['error']['lastname'] = $this->language->get('error_lastname');
218
            }
219
220
            if ((\voku\helper\UTF8::strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
221
                $json['error']['email'] = $this->language->get('error_email');
222
            }
223
224
            if ((\voku\helper\UTF8::strlen($this->request->post['telephone']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['telephone']) > 32)) {
225
                $json['error']['telephone'] = $this->language->get('error_telephone');
226
            }
227
228
            if ((\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) < 3) || (\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) > 128)) {
229
                $json['error']['address_1'] = $this->language->get('error_address_1');
230
            }
231
232
            if ((\voku\helper\UTF8::strlen(trim($this->request->post['city'])) < 2) || (\voku\helper\UTF8::strlen(trim($this->request->post['city'])) > 128)) {
233
                $json['error']['city'] = $this->language->get('error_city');
234
            }
235
236
            $this->load->model('localisation/country');
237
238
            $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
239
240
            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)) {
241
                $json['error']['postcode'] = $this->language->get('error_postcode');
242
            }
243
244
            if ($this->request->post['country_id'] == '') {
245
                $json['error']['country'] = $this->language->get('error_country');
246
            }
247
248
            if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '' || !is_numeric($this->request->post['zone_id'])) {
249
                $json['error']['zone'] = $this->language->get('error_zone');
250
            }
251
252
            // Customer Group
253
            if (isset($this->request->post['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) {
254
                $customer_group_id = $this->request->post['customer_group_id'];
255
            } else {
256
                $customer_group_id = $this->config->get('config_customer_group_id');
257
            }
258
259
            // Custom field validation
260
            $this->load->model('account/custom_field');
261
262
            $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
263
264
            foreach ($custom_fields as $custom_field) {
265
                if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) {
266
                    $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
267
                } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
268
                    $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
269
                }
270
            }
271
        }
272
273
        if (!$json) {
274
            $this->session->data['account'] = 'guest';
275
276
            $this->session->data['guest']['customer_group_id'] = $customer_group_id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $customer_group_id does not seem to be defined for all execution paths leading up to this point.
Loading history...
277
            $this->session->data['guest']['firstname'] = $this->request->post['firstname'];
278
            $this->session->data['guest']['lastname'] = $this->request->post['lastname'];
279
            $this->session->data['guest']['email'] = $this->request->post['email'];
280
            $this->session->data['guest']['telephone'] = $this->request->post['telephone'];
281
            $this->session->data['guest']['fax'] = $this->request->post['fax'];
282
283
            if (isset($this->request->post['custom_field']['account'])) {
284
                $this->session->data['guest']['custom_field'] = $this->request->post['custom_field']['account'];
285
            } else {
286
                $this->session->data['guest']['custom_field'] = array();
287
            }
288
289
            $this->session->data['payment_address']['firstname'] = $this->request->post['firstname'];
290
            $this->session->data['payment_address']['lastname'] = $this->request->post['lastname'];
291
            $this->session->data['payment_address']['company'] = $this->request->post['company'];
292
            $this->session->data['payment_address']['address_1'] = $this->request->post['address_1'];
293
            $this->session->data['payment_address']['address_2'] = $this->request->post['address_2'];
294
            $this->session->data['payment_address']['postcode'] = $this->request->post['postcode'];
295
            $this->session->data['payment_address']['city'] = $this->request->post['city'];
296
            $this->session->data['payment_address']['country_id'] = $this->request->post['country_id'];
297
            $this->session->data['payment_address']['zone_id'] = $this->request->post['zone_id'];
298
299
            $this->load->model('localisation/country');
300
301
            $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
302
303
            if ($country_info) {
304
                $this->session->data['payment_address']['country'] = $country_info['name'];
305
                $this->session->data['payment_address']['iso_code_2'] = $country_info['iso_code_2'];
306
                $this->session->data['payment_address']['iso_code_3'] = $country_info['iso_code_3'];
307
                $this->session->data['payment_address']['address_format'] = $country_info['address_format'];
308
            } else {
309
                $this->session->data['payment_address']['country'] = '';
310
                $this->session->data['payment_address']['iso_code_2'] = '';
311
                $this->session->data['payment_address']['iso_code_3'] = '';
312
                $this->session->data['payment_address']['address_format'] = '';
313
            }
314
315
            if (isset($this->request->post['custom_field']['address'])) {
316
                $this->session->data['payment_address']['custom_field'] = $this->request->post['custom_field']['address'];
317
            } else {
318
                $this->session->data['payment_address']['custom_field'] = array();
319
            }
320
321
            $this->load->model('localisation/zone');
322
323
            $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
324
325
            if ($zone_info) {
326
                $this->session->data['payment_address']['zone'] = $zone_info['name'];
327
                $this->session->data['payment_address']['zone_code'] = $zone_info['code'];
328
            } else {
329
                $this->session->data['payment_address']['zone'] = '';
330
                $this->session->data['payment_address']['zone_code'] = '';
331
            }
332
333
            if (!empty($this->request->post['shipping_address'])) {
334
                $this->session->data['guest']['shipping_address'] = $this->request->post['shipping_address'];
335
            } else {
336
                $this->session->data['guest']['shipping_address'] = false;
337
            }
338
339
            if ($this->session->data['guest']['shipping_address']) {
340
                $this->session->data['shipping_address']['firstname'] = $this->request->post['firstname'];
341
                $this->session->data['shipping_address']['lastname'] = $this->request->post['lastname'];
342
                $this->session->data['shipping_address']['company'] = $this->request->post['company'];
343
                $this->session->data['shipping_address']['address_1'] = $this->request->post['address_1'];
344
                $this->session->data['shipping_address']['address_2'] = $this->request->post['address_2'];
345
                $this->session->data['shipping_address']['postcode'] = $this->request->post['postcode'];
346
                $this->session->data['shipping_address']['city'] = $this->request->post['city'];
347
                $this->session->data['shipping_address']['country_id'] = $this->request->post['country_id'];
348
                $this->session->data['shipping_address']['zone_id'] = $this->request->post['zone_id'];
349
350
                if ($country_info) {
351
                    $this->session->data['shipping_address']['country'] = $country_info['name'];
352
                    $this->session->data['shipping_address']['iso_code_2'] = $country_info['iso_code_2'];
353
                    $this->session->data['shipping_address']['iso_code_3'] = $country_info['iso_code_3'];
354
                    $this->session->data['shipping_address']['address_format'] = $country_info['address_format'];
355
                } else {
356
                    $this->session->data['shipping_address']['country'] = '';
357
                    $this->session->data['shipping_address']['iso_code_2'] = '';
358
                    $this->session->data['shipping_address']['iso_code_3'] = '';
359
                    $this->session->data['shipping_address']['address_format'] = '';
360
                }
361
362
                if ($zone_info) {
363
                    $this->session->data['shipping_address']['zone'] = $zone_info['name'];
364
                    $this->session->data['shipping_address']['zone_code'] = $zone_info['code'];
365
                } else {
366
                    $this->session->data['shipping_address']['zone'] = '';
367
                    $this->session->data['shipping_address']['zone_code'] = '';
368
                }
369
370
                if (isset($this->request->post['custom_field']['address'])) {
371
                    $this->session->data['shipping_address']['custom_field'] = $this->request->post['custom_field']['address'];
372
                } else {
373
                    $this->session->data['shipping_address']['custom_field'] = array();
374
                }
375
            }
376
377
            unset($this->session->data['shipping_method']);
378
            unset($this->session->data['shipping_methods']);
379
            unset($this->session->data['payment_method']);
380
            unset($this->session->data['payment_methods']);
381
        }
382
383
        $this->response->addHeader('Content-Type: application/json');
384
        $this->response->setOutput(json_encode($json));
385
    }
386
}
387