Total Complexity | 91 |
Total Lines | 507 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ControllerAccountAddress 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 ControllerAccountAddress, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ControllerAccountAddress extends \Divine\Engine\Core\Controller |
||
|
|||
24 | { |
||
25 | private $error = array(); |
||
26 | |||
27 | public function index() |
||
28 | { |
||
29 | if (!$this->customer->isLogged()) { |
||
30 | $this->session->data['redirect'] = $this->url->link('account/address', '', true); |
||
31 | |||
32 | $this->response->redirect($this->url->link('account/login', '', true)); |
||
33 | } |
||
34 | |||
35 | $this->load->language('account/address'); |
||
36 | |||
37 | $this->document->setTitle($this->language->get('heading_title')); |
||
38 | |||
39 | $this->load->model('account/address'); |
||
40 | |||
41 | $this->getList(); |
||
42 | } |
||
43 | |||
44 | public function add() |
||
67 | } |
||
68 | |||
69 | public function edit() |
||
70 | { |
||
71 | if (!$this->customer->isLogged()) { |
||
72 | $this->session->data['redirect'] = $this->url->link('account/address', '', true); |
||
73 | |||
74 | $this->response->redirect($this->url->link('account/login', '', true)); |
||
75 | } |
||
76 | |||
77 | $this->load->language('account/address'); |
||
78 | |||
79 | $this->document->setTitle($this->language->get('heading_title')); |
||
80 | |||
81 | $this->load->model('account/address'); |
||
82 | |||
83 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
84 | $this->model_account_address->editAddress($this->request->get['address_id'], $this->request->post); |
||
85 | |||
86 | // Default Shipping Address |
||
87 | if (isset($this->session->data['shipping_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['shipping_address']['address_id'])) { |
||
88 | $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->request->get['address_id']); |
||
89 | |||
90 | unset($this->session->data['shipping_method']); |
||
91 | unset($this->session->data['shipping_methods']); |
||
92 | } |
||
93 | |||
94 | // Default Payment Address |
||
95 | if (isset($this->session->data['payment_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['payment_address']['address_id'])) { |
||
96 | $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->request->get['address_id']); |
||
97 | |||
98 | unset($this->session->data['payment_method']); |
||
99 | unset($this->session->data['payment_methods']); |
||
100 | } |
||
101 | |||
102 | $this->session->data['success'] = $this->language->get('text_edit'); |
||
103 | |||
104 | $this->response->redirect($this->url->link('account/address', '', true)); |
||
105 | } |
||
106 | |||
107 | $this->getForm(); |
||
108 | } |
||
109 | |||
110 | public function delete() |
||
111 | { |
||
112 | if (!$this->customer->isLogged()) { |
||
113 | $this->session->data['redirect'] = $this->url->link('account/address', '', true); |
||
114 | |||
115 | $this->response->redirect($this->url->link('account/login', '', true)); |
||
116 | } |
||
117 | |||
118 | $this->load->language('account/address'); |
||
119 | |||
120 | $this->document->setTitle($this->language->get('heading_title')); |
||
121 | |||
122 | $this->load->model('account/address'); |
||
123 | |||
124 | if (isset($this->request->get['address_id']) && $this->validateDelete()) { |
||
125 | $this->model_account_address->deleteAddress($this->request->get['address_id']); |
||
126 | |||
127 | // Default Shipping Address |
||
128 | if (isset($this->session->data['shipping_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['shipping_address']['address_id'])) { |
||
129 | unset($this->session->data['shipping_address']); |
||
130 | unset($this->session->data['shipping_method']); |
||
131 | unset($this->session->data['shipping_methods']); |
||
132 | } |
||
133 | |||
134 | // Default Payment Address |
||
135 | if (isset($this->session->data['payment_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['payment_address']['address_id'])) { |
||
136 | unset($this->session->data['payment_address']); |
||
137 | unset($this->session->data['payment_method']); |
||
138 | unset($this->session->data['payment_methods']); |
||
139 | } |
||
140 | |||
141 | $this->session->data['success'] = $this->language->get('text_delete'); |
||
142 | |||
143 | $this->response->redirect($this->url->link('account/address', '', true)); |
||
144 | } |
||
145 | |||
146 | $this->getList(); |
||
147 | } |
||
148 | |||
149 | protected function getList() |
||
150 | { |
||
151 | $data['breadcrumbs'][] = array( |
||
152 | 'text' => $this->language->get('text_home'), |
||
153 | 'href' => $this->url->link('common/home') |
||
154 | ); |
||
155 | |||
156 | $data['breadcrumbs'][] = array( |
||
157 | 'text' => $this->language->get('text_account'), |
||
158 | 'href' => $this->url->link('account/account', '', true) |
||
159 | ); |
||
160 | |||
161 | $data['breadcrumbs'][] = array( |
||
162 | 'text' => $this->language->get('heading_title'), |
||
163 | 'href' => $this->url->link('account/address', '', true) |
||
164 | ); |
||
165 | |||
166 | $data['heading_title'] = $this->language->get('heading_title'); |
||
167 | |||
168 | $data['text_address_book'] = $this->language->get('text_address_book'); |
||
169 | $data['text_empty'] = $this->language->get('text_empty'); |
||
170 | |||
171 | $data['button_new_address'] = $this->language->get('button_new_address'); |
||
172 | $data['button_edit'] = $this->language->get('button_edit'); |
||
173 | $data['button_delete'] = $this->language->get('button_delete'); |
||
174 | $data['button_back'] = $this->language->get('button_back'); |
||
175 | |||
176 | if (isset($this->error['warning'])) { |
||
177 | $data['error_warning'] = $this->error['warning']; |
||
178 | } else { |
||
179 | $data['error_warning'] = ''; |
||
180 | } |
||
181 | |||
182 | if (isset($this->session->data['success'])) { |
||
183 | $data['success'] = $this->session->data['success']; |
||
184 | |||
185 | unset($this->session->data['success']); |
||
186 | } else { |
||
187 | $data['success'] = ''; |
||
188 | } |
||
189 | |||
190 | $data['addresses'] = array(); |
||
191 | |||
192 | $results = $this->model_account_address->getAddresses(); |
||
193 | |||
194 | foreach ($results as $result) { |
||
195 | if ($result['address_format']) { |
||
196 | $format = $result['address_format']; |
||
197 | } else { |
||
198 | $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}'; |
||
199 | } |
||
200 | |||
201 | $find = array( |
||
202 | '{firstname}', |
||
203 | '{lastname}', |
||
204 | '{company}', |
||
205 | '{address_1}', |
||
206 | '{address_2}', |
||
207 | '{city}', |
||
208 | '{postcode}', |
||
209 | '{zone}', |
||
210 | '{zone_code}', |
||
211 | '{country}' |
||
212 | ); |
||
213 | |||
214 | $replace = array( |
||
215 | 'firstname' => $result['firstname'], |
||
216 | 'lastname' => $result['lastname'], |
||
217 | 'company' => $result['company'], |
||
218 | 'address_1' => $result['address_1'], |
||
219 | 'address_2' => $result['address_2'], |
||
220 | 'city' => $result['city'], |
||
221 | 'postcode' => $result['postcode'], |
||
222 | 'zone' => $result['zone'], |
||
223 | 'zone_code' => $result['zone_code'], |
||
224 | 'country' => $result['country'] |
||
225 | ); |
||
226 | |||
227 | $data['addresses'][] = array( |
||
228 | 'address_id' => $result['address_id'], |
||
229 | 'address' => str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format)))), |
||
230 | 'update' => $this->url->link('account/address/edit', 'address_id=' . $result['address_id'], true), |
||
231 | 'delete' => $this->url->link('account/address/delete', 'address_id=' . $result['address_id'], true) |
||
232 | ); |
||
233 | } |
||
234 | |||
235 | $data['add'] = $this->url->link('account/address/add', '', true); |
||
236 | $data['back'] = $this->url->link('account/account', '', true); |
||
237 | |||
238 | $data['column'] = $this->load->controller('common/column'); |
||
239 | $data['content_top'] = $this->load->controller('common/content_top'); |
||
240 | $data['content_bottom'] = $this->load->controller('common/content_bottom'); |
||
241 | $data['footer'] = $this->load->controller('common/footer'); |
||
242 | $data['header'] = $this->load->controller('common/header'); |
||
243 | |||
244 | $this->response->setOutput($this->load->view('account/address_list', $data)); |
||
245 | } |
||
246 | |||
247 | protected function getForm() |
||
248 | { |
||
249 | $data['breadcrumbs'] = array(); |
||
250 | |||
251 | $data['breadcrumbs'][] = array( |
||
252 | 'text' => $this->language->get('text_home'), |
||
253 | 'href' => $this->url->link('common/home') |
||
254 | ); |
||
255 | |||
256 | $data['breadcrumbs'][] = array( |
||
257 | 'text' => $this->language->get('text_account'), |
||
258 | 'href' => $this->url->link('account/account', '', true) |
||
259 | ); |
||
260 | |||
261 | $data['breadcrumbs'][] = array( |
||
262 | 'text' => $this->language->get('heading_title'), |
||
263 | 'href' => $this->url->link('account/address', '', true) |
||
264 | ); |
||
265 | |||
266 | if (!isset($this->request->get['address_id'])) { |
||
267 | $data['breadcrumbs'][] = array( |
||
268 | 'text' => $this->language->get('text_edit_address'), |
||
269 | 'href' => $this->url->link('account/address/add', '', true) |
||
270 | ); |
||
271 | } else { |
||
272 | $data['breadcrumbs'][] = array( |
||
273 | 'text' => $this->language->get('text_edit_address'), |
||
274 | 'href' => $this->url->link('account/address/edit', 'address_id=' . $this->request->get['address_id'], true) |
||
275 | ); |
||
276 | } |
||
277 | |||
278 | $data['heading_title'] = $this->language->get('heading_title'); |
||
279 | |||
280 | $data['text_edit_address'] = $this->language->get('text_edit_address'); |
||
281 | $data['text_yes'] = $this->language->get('text_yes'); |
||
282 | $data['text_no'] = $this->language->get('text_no'); |
||
283 | $data['text_select'] = $this->language->get('text_select'); |
||
284 | $data['text_none'] = $this->language->get('text_none'); |
||
285 | $data['text_loading'] = $this->language->get('text_loading'); |
||
286 | |||
287 | $data['entry_firstname'] = $this->language->get('entry_firstname'); |
||
288 | $data['entry_lastname'] = $this->language->get('entry_lastname'); |
||
289 | $data['entry_company'] = $this->language->get('entry_company'); |
||
290 | $data['entry_address_1'] = $this->language->get('entry_address_1'); |
||
291 | $data['entry_address_2'] = $this->language->get('entry_address_2'); |
||
292 | $data['entry_postcode'] = $this->language->get('entry_postcode'); |
||
293 | $data['entry_city'] = $this->language->get('entry_city'); |
||
294 | $data['entry_country'] = $this->language->get('entry_country'); |
||
295 | $data['entry_zone'] = $this->language->get('entry_zone'); |
||
296 | $data['entry_default'] = $this->language->get('entry_default'); |
||
297 | |||
298 | $data['button_continue'] = $this->language->get('button_continue'); |
||
299 | $data['button_back'] = $this->language->get('button_back'); |
||
300 | $data['button_upload'] = $this->language->get('button_upload'); |
||
301 | |||
302 | if (isset($this->error['firstname'])) { |
||
303 | $data['error_firstname'] = $this->error['firstname']; |
||
304 | } else { |
||
305 | $data['error_firstname'] = ''; |
||
306 | } |
||
307 | |||
308 | if (isset($this->error['lastname'])) { |
||
309 | $data['error_lastname'] = $this->error['lastname']; |
||
310 | } else { |
||
311 | $data['error_lastname'] = ''; |
||
312 | } |
||
313 | |||
314 | if (isset($this->error['address_1'])) { |
||
315 | $data['error_address_1'] = $this->error['address_1']; |
||
316 | } else { |
||
317 | $data['error_address_1'] = ''; |
||
318 | } |
||
319 | |||
320 | if (isset($this->error['city'])) { |
||
321 | $data['error_city'] = $this->error['city']; |
||
322 | } else { |
||
323 | $data['error_city'] = ''; |
||
324 | } |
||
325 | |||
326 | if (isset($this->error['postcode'])) { |
||
327 | $data['error_postcode'] = $this->error['postcode']; |
||
328 | } else { |
||
329 | $data['error_postcode'] = ''; |
||
330 | } |
||
331 | |||
332 | if (isset($this->error['country'])) { |
||
333 | $data['error_country'] = $this->error['country']; |
||
334 | } else { |
||
335 | $data['error_country'] = ''; |
||
336 | } |
||
337 | |||
338 | if (isset($this->error['zone'])) { |
||
339 | $data['error_zone'] = $this->error['zone']; |
||
340 | } else { |
||
341 | $data['error_zone'] = ''; |
||
342 | } |
||
343 | |||
344 | if (isset($this->error['custom_field'])) { |
||
345 | $data['error_custom_field'] = $this->error['custom_field']; |
||
346 | } else { |
||
347 | $data['error_custom_field'] = array(); |
||
348 | } |
||
349 | |||
350 | if (!isset($this->request->get['address_id'])) { |
||
351 | $data['action'] = $this->url->link('account/address/add', '', true); |
||
352 | } else { |
||
353 | $data['action'] = $this->url->link('account/address/edit', 'address_id=' . $this->request->get['address_id'], true); |
||
354 | } |
||
355 | |||
356 | if (isset($this->request->get['address_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { |
||
357 | $address_info = $this->model_account_address->getAddress($this->request->get['address_id']); |
||
358 | } |
||
359 | |||
360 | if (isset($this->request->post['firstname'])) { |
||
361 | $data['firstname'] = $this->request->post['firstname']; |
||
362 | } elseif (!empty($address_info)) { |
||
363 | $data['firstname'] = $address_info['firstname']; |
||
364 | } else { |
||
365 | $data['firstname'] = ''; |
||
366 | } |
||
367 | |||
368 | if (isset($this->request->post['lastname'])) { |
||
369 | $data['lastname'] = $this->request->post['lastname']; |
||
370 | } elseif (!empty($address_info)) { |
||
371 | $data['lastname'] = $address_info['lastname']; |
||
372 | } else { |
||
373 | $data['lastname'] = ''; |
||
374 | } |
||
375 | |||
376 | if (isset($this->request->post['company'])) { |
||
377 | $data['company'] = $this->request->post['company']; |
||
378 | } elseif (!empty($address_info)) { |
||
379 | $data['company'] = $address_info['company']; |
||
380 | } else { |
||
381 | $data['company'] = ''; |
||
382 | } |
||
383 | |||
384 | if (isset($this->request->post['address_1'])) { |
||
385 | $data['address_1'] = $this->request->post['address_1']; |
||
386 | } elseif (!empty($address_info)) { |
||
387 | $data['address_1'] = $address_info['address_1']; |
||
388 | } else { |
||
389 | $data['address_1'] = ''; |
||
390 | } |
||
391 | |||
392 | if (isset($this->request->post['address_2'])) { |
||
393 | $data['address_2'] = $this->request->post['address_2']; |
||
394 | } elseif (!empty($address_info)) { |
||
395 | $data['address_2'] = $address_info['address_2']; |
||
396 | } else { |
||
397 | $data['address_2'] = ''; |
||
398 | } |
||
399 | |||
400 | if (isset($this->request->post['postcode'])) { |
||
401 | $data['postcode'] = $this->request->post['postcode']; |
||
402 | } elseif (!empty($address_info)) { |
||
403 | $data['postcode'] = $address_info['postcode']; |
||
404 | } else { |
||
405 | $data['postcode'] = ''; |
||
406 | } |
||
407 | |||
408 | if (isset($this->request->post['city'])) { |
||
409 | $data['city'] = $this->request->post['city']; |
||
410 | } elseif (!empty($address_info)) { |
||
411 | $data['city'] = $address_info['city']; |
||
412 | } else { |
||
413 | $data['city'] = ''; |
||
414 | } |
||
415 | |||
416 | if (isset($this->request->post['country_id'])) { |
||
417 | $data['country_id'] = (int)$this->request->post['country_id']; |
||
418 | } elseif (!empty($address_info)) { |
||
419 | $data['country_id'] = $address_info['country_id']; |
||
420 | } else { |
||
421 | $data['country_id'] = $this->config->get('config_country_id'); |
||
422 | } |
||
423 | |||
424 | if (isset($this->request->post['zone_id'])) { |
||
425 | $data['zone_id'] = (int)$this->request->post['zone_id']; |
||
426 | } elseif (!empty($address_info)) { |
||
427 | $data['zone_id'] = $address_info['zone_id']; |
||
428 | } else { |
||
429 | $data['zone_id'] = ''; |
||
430 | } |
||
431 | |||
432 | $this->load->model('localisation/country'); |
||
433 | |||
434 | $data['countries'] = $this->model_localisation_country->getCountries(); |
||
435 | |||
436 | // Custom fields |
||
437 | $this->load->model('account/custom_field'); |
||
438 | |||
439 | $data['custom_fields'] = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); |
||
440 | |||
441 | if (isset($this->request->post['custom_field'])) { |
||
442 | $data['address_custom_field'] = $this->request->post['custom_field']; |
||
443 | } elseif (isset($address_info)) { |
||
444 | $data['address_custom_field'] = $address_info['custom_field']; |
||
445 | } else { |
||
446 | $data['address_custom_field'] = array(); |
||
447 | } |
||
448 | |||
449 | if (isset($this->request->post['default'])) { |
||
450 | $data['default'] = $this->request->post['default']; |
||
451 | } elseif (isset($this->request->get['address_id'])) { |
||
452 | $data['default'] = $this->customer->getAddressId() == $this->request->get['address_id']; |
||
453 | } else { |
||
454 | $data['default'] = false; |
||
455 | } |
||
456 | |||
457 | $data['back'] = $this->url->link('account/address', '', true); |
||
458 | |||
459 | $data['column'] = $this->load->controller('common/column'); |
||
460 | $data['content_top'] = $this->load->controller('common/content_top'); |
||
461 | $data['content_bottom'] = $this->load->controller('common/content_bottom'); |
||
462 | $data['footer'] = $this->load->controller('common/footer'); |
||
463 | $data['header'] = $this->load->controller('common/header'); |
||
464 | |||
465 | |||
466 | $this->response->setOutput($this->load->view('account/address_form', $data)); |
||
467 | } |
||
468 | |||
469 | protected function validateForm() |
||
517 | } |
||
518 | |||
519 | protected function validateDelete() |
||
520 | { |
||
521 | if ($this->model_account_address->getTotalAddresses() == 1) { |
||
522 | $this->error['warning'] = $this->language->get('error_delete'); |
||
532 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.