Completed
Push — master ( c0ee0c...9768c0 )
by
unknown
33:12 queued 18:11
created

ControllerAntifraudMaxmind::order()   F

Complexity

Conditions 31
Paths > 20000

Size

Total Lines 228
Code Lines 147

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
eloc 147
nc 429496.7295
nop 0
dl 0
loc 228
rs 2
c 0
b 0
f 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
 * @package     Arastta eCommerce
4
 * @copyright   2015-2017 Arastta Association. All rights reserved.
5
 * @copyright   See CREDITS.txt for credits and other copyright notices.
6
 * @license     GNU GPL version 3; see LICENSE.txt
7
 * @link        https://arastta.org
8
 */
9
10
use Arastta\Component\Form\Form as AForm;
11
12
class ControllerAntifraudMaxmind extends Controller
13
{
14
15
    private $error = array();
16
17
    public function index()
18
    {
19
        $this->load->language('antifraud/maxmind');
20
21
        $this->document->setTitle($this->language->get('heading_title'));
22
23
        $this->load->model('setting/setting');
24
25
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
26
            $this->model_setting_setting->editSetting('maxmind_antifraud', $this->request->post);
27
28
            $this->session->data['success'] = $this->language->get('text_success');
29
30
            if (isset($this->request->post['button']) && $this->request->post['button'] == 'save') {
31
                $this->response->redirect($this->url->link($this->request->get['route'], 'token=' . $this->session->data['token'], 'SSL'));
32
            }
33
34
            $this->response->redirect($this->url->link('extension/extension', 'filter_type=antifraud&token=' . $this->session->data['token'], 'SSL'));
35
        }
36
37
        // Add all language text
38
        $data = $this->language->all();
39
40
        if (isset($this->error['warning'])) {
41
            $data['error_warning'] = $this->error['warning'];
42
        } else {
43
            $data['error_warning'] = '';
44
        }
45
46
        $data['action'] = $this->url->link('antifraud/maxmind', 'token=' . $this->session->data['token'], 'SSL');
47
48
        $data['cancel'] = $this->url->link('extension/extension', 'filter_type=antifraud&token=' . $this->session->data['token'], 'SSL');
49
50
        $data['form_fields'] = $this->getFormFields($data['action']);
51
52
        $data['header'] = $this->load->controller('common/header');
53
        $data['column_left'] = $this->load->controller('common/column_left');
54
        $data['footer'] = $this->load->controller('common/footer');
55
56
        $this->response->setOutput($this->load->output('antifraud/maxmind', $data));
57
    }
58
59
    public function order()
60
    {
61
        $maxmind_status = $this->config->get('maxmind_antifraud_status', 0);
62
63
        // Disable Maxmind Fraud extension continue
64
        if (!$maxmind_status) {
65
            return false;
66
        }
67
68
        $this->load->language('antifraud/maxmind');
69
70
        $this->load->model('sale/fraud');
71
72
        if (isset($this->request->get['order_id'])) {
73
            $order_id = $this->request->get['order_id'];
74
        } else {
75
            $order_id = 0;
76
        }
77
78
        $fraud_info = $this->model_sale_fraud->getFraud($order_id);
79
80
        // Add all language text
81
        $data = $this->language->all();
82
83
        if ($fraud_info) {
84
            $data['country_match'] = $fraud_info['country_match'];
85
86
            if ($fraud_info['country_code']) {
87
                $data['country_code'] = $fraud_info['country_code'];
88
            } else {
89
                $data['country_code'] = '';
90
            }
91
92
            $data['high_risk_country'] = $fraud_info['high_risk_country'];
93
            $data['distance'] = $fraud_info['distance'];
94
95
            if ($fraud_info['ip_region']) {
96
                $data['ip_region'] = $fraud_info['ip_region'];
97
            } else {
98
                $data['ip_region'] = '';
99
            }
100
101
            if ($fraud_info['ip_city']) {
102
                $data['ip_city'] = $fraud_info['ip_city'];
103
            } else {
104
                $data['ip_city'] = '';
105
            }
106
107
            $data['ip_latitude'] = $fraud_info['ip_latitude'];
108
            $data['ip_longitude'] = $fraud_info['ip_longitude'];
109
110
            if ($fraud_info['ip_isp']) {
111
                $data['ip_isp'] = $fraud_info['ip_isp'];
112
            } else {
113
                $data['ip_isp'] = '';
114
            }
115
116
            if ($fraud_info['ip_org']) {
117
                $data['ip_org'] = $fraud_info['ip_org'];
118
            } else {
119
                $data['ip_org'] = '';
120
            }
121
122
            $data['ip_asnum'] = $fraud_info['ip_asnum'];
123
124
            if ($fraud_info['ip_user_type']) {
125
                $data['ip_user_type'] = $fraud_info['ip_user_type'];
126
            } else {
127
                $data['ip_user_type'] = '';
128
            }
129
130
            if ($fraud_info['ip_country_confidence']) {
131
                $data['ip_country_confidence'] = $fraud_info['ip_country_confidence'];
132
            } else {
133
                $data['ip_country_confidence'] = '';
134
            }
135
136
            if ($fraud_info['ip_region_confidence']) {
137
                $data['ip_region_confidence'] = $fraud_info['ip_region_confidence'];
138
            } else {
139
                $data['ip_region_confidence'] = '';
140
            }
141
142
            if ($fraud_info['ip_city_confidence']) {
143
                $data['ip_city_confidence'] = $fraud_info['ip_city_confidence'];
144
            } else {
145
                $data['ip_city_confidence'] = '';
146
            }
147
148
            if ($fraud_info['ip_postal_confidence']) {
149
                $data['ip_postal_confidence'] = $fraud_info['ip_postal_confidence'];
150
            } else {
151
                $data['ip_postal_confidence'] = '';
152
            }
153
154
            if ($fraud_info['ip_postal_code']) {
155
                $data['ip_postal_code'] = $fraud_info['ip_postal_code'];
156
            } else {
157
                $data['ip_postal_code'] = '';
158
            }
159
160
            $data['ip_accuracy_radius'] = $fraud_info['ip_accuracy_radius'];
161
162
            if ($fraud_info['ip_net_speed_cell']) {
163
                $data['ip_net_speed_cell'] = $fraud_info['ip_net_speed_cell'];
164
            } else {
165
                $data['ip_net_speed_cell'] = '';
166
            }
167
168
            $data['ip_metro_code'] = $fraud_info['ip_metro_code'];
169
            $data['ip_area_code'] = $fraud_info['ip_area_code'];
170
171
            if ($fraud_info['ip_time_zone']) {
172
                $data['ip_time_zone'] = $fraud_info['ip_time_zone'];
173
            } else {
174
                $data['ip_time_zone'] = '';
175
            }
176
177
            if ($fraud_info['ip_region_name']) {
178
                $data['ip_region_name'] = $fraud_info['ip_region_name'];
179
            } else {
180
                $data['ip_region_name'] = '';
181
            }
182
183
            if ($fraud_info['ip_domain']) {
184
                $data['ip_domain'] = $fraud_info['ip_domain'];
185
            } else {
186
                $data['ip_domain'] = '';
187
            }
188
189
            if ($fraud_info['ip_country_name']) {
190
                $data['ip_country_name'] = $fraud_info['ip_country_name'];
191
            } else {
192
                $data['ip_country_name'] = '';
193
            }
194
195
            if ($fraud_info['ip_continent_code']) {
196
                $data['ip_continent_code'] = $fraud_info['ip_continent_code'];
197
            } else {
198
                $data['ip_continent_code'] = '';
199
            }
200
201
            if ($fraud_info['ip_corporate_proxy']) {
202
                $data['ip_corporate_proxy'] = $fraud_info['ip_corporate_proxy'];
203
            } else {
204
                $data['ip_corporate_proxy'] = '';
205
            }
206
207
            $data['anonymous_proxy'] = $fraud_info['anonymous_proxy'];
208
            $data['proxy_score'] = $fraud_info['proxy_score'];
209
210
            if ($fraud_info['is_trans_proxy']) {
211
                $data['is_trans_proxy'] = $fraud_info['is_trans_proxy'];
212
            } else {
213
                $data['is_trans_proxy'] = '';
214
            }
215
216
            $data['free_mail'] = $fraud_info['free_mail'];
217
            $data['carder_email'] = $fraud_info['carder_email'];
218
219
            if ($fraud_info['high_risk_username']) {
220
                $data['high_risk_username'] = $fraud_info['high_risk_username'];
221
            } else {
222
                $data['high_risk_username'] = '';
223
            }
224
225
            if ($fraud_info['high_risk_password']) {
226
                $data['high_risk_password'] = $fraud_info['high_risk_password'];
227
            } else {
228
                $data['high_risk_password'] = '';
229
            }
230
231
            $data['bin_match'] = $fraud_info['bin_match'];
232
233
            if ($fraud_info['bin_country']) {
234
                $data['bin_country'] = $fraud_info['bin_country'];
235
            } else {
236
                $data['bin_country'] = '';
237
            }
238
239
            $data['bin_name_match'] = $fraud_info['bin_name_match'];
240
241
            if ($fraud_info['bin_name']) {
242
                $data['bin_name'] = $fraud_info['bin_name'];
243
            } else {
244
                $data['bin_name'] = '';
245
            }
246
247
            $data['bin_phone_match'] = $fraud_info['bin_phone_match'];
248
249
            if ($fraud_info['bin_phone']) {
250
                $data['bin_phone'] = $fraud_info['bin_phone'];
251
            } else {
252
                $data['bin_phone'] = '';
253
            }
254
255
            if ($fraud_info['customer_phone_in_billing_location']) {
256
                $data['customer_phone_in_billing_location'] = $fraud_info['customer_phone_in_billing_location'];
257
            } else {
258
                $data['customer_phone_in_billing_location'] = '';
259
            }
260
261
            $data['ship_forward'] = $fraud_info['ship_forward'];
262
263
            if ($fraud_info['city_postal_match']) {
264
                $data['city_postal_match'] = $fraud_info['city_postal_match'];
265
            } else {
266
                $data['city_postal_match'] = '';
267
            }
268
269
            if ($fraud_info['ship_city_postal_match']) {
270
                $data['ship_city_postal_match'] = $fraud_info['ship_city_postal_match'];
271
            } else {
272
                $data['ship_city_postal_match'] = '';
273
            }
274
275
            $data['score'] = $fraud_info['score'];
276
            $data['explanation'] = $fraud_info['explanation'];
277
            $data['risk_score'] = $fraud_info['risk_score'];
278
            $data['queries_remaining'] = $fraud_info['queries_remaining'];
279
            $data['maxmind_id'] = $fraud_info['maxmind_id'];
280
            $data['error'] = $fraud_info['error'];
281
        } else {
282
            $data['maxmind_id'] = '';
283
        }
284
285
        return $this->load->view('antifraud/maxmind_info.tpl', $data);
286
    }
287
288
    protected function getFormFields($action)
289
    {
290
        $action = str_replace('amp;', '', $action);
291
292
        $key['value'] = $this->config->get('maxmind_antifraud_key', '');
293
        $key['placeholder'] = $this->language->get('entry_key');
294
        $key['required'] = $this->language->get('required');
295
296
        $score['value'] = $this->config->get('maxmind_antifraud_score', '');
297
        $score['placeholder'] = $this->language->get('entry_score');
298
        $score['required'] = $this->language->get('required');
299
300
        $this->load->model('localisation/order_status');
301
302
        $order_option = array();
303
        $order_statuses = $this->model_localisation_order_status->getOrderStatuses();
304
305
        foreach ($order_statuses as $order_status) {
306
            $order_option[$order_status['order_status_id']] = $order_status['name'];
307
        }
308
309
        $order_text = array(
310
            'value' => $this->config->get('maxmind_antifraud_order_status_id', 0),
311
            'selected'  => $this->config->get('maxmind_antifraud_order_status_id', 0)
312
        );
313
314
        $status_text = array(
315
            'yes' => $this->language->get('text_enabled'),
316
            'no'  => $this->language->get('text_disabled')
317
        );
318
319
        $status['value'] = $this->config->get('maxmind_antifraud_status', 0);
320
        $status['labelclass'] = 'radio-inline';
321
322
        $form = new AForm('form-maxmind-antifraud', $action);
323
324
        $form->addElement(new Arastta\Component\Form\Element\Textbox($this->language->get('entry_key'), 'maxmind_antifraud_key', $key));
325
        $form->addElement(new Arastta\Component\Form\Element\Textbox($this->language->get('entry_score'), 'maxmind_antifraud_score', $score));
326
        $form->addElement(new Arastta\Component\Form\Element\Select($this->language->get('entry_order'), 'maxmind_antifraud_order_status_id', $order_option, $order_text));
327
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_status'), 'maxmind_antifraud_status', $status, $status_text));
328
329
        return $form->render(true);
330
    }
331
332 View Code Duplication
    protected function validate()
333
    {
334
        if (!$this->user->hasPermission('modify', 'antifraud/maxmind')) {
335
            $this->error['warning'] = $this->language->get('error_permission');
336
        }
337
338
        if (empty($this->request->post['maxmind_antifraud_key'])) {
339
            $this->error['warning'] = $this->language->get('error_key');
340
        }
341
342
        if (empty($this->request->post['maxmind_antifraud_score'])) {
343
            $this->error['warning'] = $this->language->get('error_score');
344
        }
345
346
        return !$this->error;
347
    }
348
}
349