Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like main_controller 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 main_controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class main_controller |
||
16 | { |
||
17 | protected $auth; |
||
18 | protected $config; |
||
19 | protected $container; |
||
20 | protected $extension_manager; |
||
21 | protected $helper; |
||
22 | protected $ppde_entity_currency; |
||
23 | protected $ppde_entity_donation_pages; |
||
24 | protected $ppde_entity_transactions; |
||
25 | protected $ppde_operator_currency; |
||
26 | protected $ppde_operator_donation_pages; |
||
27 | protected $ppde_operator_transactions; |
||
28 | protected $request; |
||
29 | protected $template; |
||
30 | protected $user; |
||
31 | protected $root_path; |
||
32 | protected $php_ext; |
||
33 | /** @var array */ |
||
34 | protected $ext_meta = array(); |
||
35 | /** @var string */ |
||
36 | protected $ext_name; |
||
37 | /** @var string */ |
||
38 | private $donation_body; |
||
39 | /** @var array */ |
||
40 | private $donation_content_data; |
||
41 | /** @var string */ |
||
42 | private $return_args_url; |
||
43 | /** @var string */ |
||
44 | private $u_action; |
||
45 | |||
46 | /** |
||
47 | * Constructor |
||
48 | * |
||
49 | * @param \phpbb\auth\auth $auth Auth object |
||
50 | * @param \phpbb\config\config $config Config object |
||
51 | * @param ContainerInterface $container Service container interface |
||
52 | * @param \phpbb\extension\manager $extension_manager An instance of the phpBB extension |
||
53 | * manager |
||
54 | * @param \phpbb\controller\helper $helper Controller helper object |
||
55 | * @param \skouat\ppde\entity\currency $ppde_entity_currency Currency entity object |
||
56 | * @param \skouat\ppde\entity\donation_pages $ppde_entity_donation_pages Donation pages entity object |
||
57 | * @param \skouat\ppde\entity\transactions $ppde_entity_transactions Transactions log entity object |
||
58 | * @param \skouat\ppde\operators\currency $ppde_operator_currency Currency operator object |
||
59 | * @param \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages Donation pages operator object |
||
60 | * @param \skouat\ppde\operators\transactions $ppde_operator_transactions Transactions log operator object |
||
61 | * @param \phpbb\request\request $request Request object |
||
62 | * @param \phpbb\template\template $template Template object |
||
63 | * @param \phpbb\user $user User object |
||
64 | * @param string $root_path phpBB root path |
||
65 | * @param string $php_ext phpEx |
||
66 | * |
||
67 | * @return \skouat\ppde\controller\main_controller |
||
68 | * @access public |
||
69 | */ |
||
70 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, ContainerInterface $container, \phpbb\extension\manager $extension_manager, \phpbb\controller\helper $helper, \skouat\ppde\entity\currency $ppde_entity_currency, \skouat\ppde\entity\donation_pages $ppde_entity_donation_pages, \skouat\ppde\entity\transactions $ppde_entity_transactions, \skouat\ppde\operators\currency $ppde_operator_currency, \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages, \skouat\ppde\operators\transactions $ppde_operator_transactions, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $root_path, $php_ext) |
||
71 | { |
||
72 | $this->auth = $auth; |
||
73 | $this->config = $config; |
||
74 | $this->container = $container; |
||
75 | $this->extension_manager = $extension_manager; |
||
76 | $this->helper = $helper; |
||
77 | $this->ppde_entity_currency = $ppde_entity_currency; |
||
78 | $this->ppde_entity_donation_pages = $ppde_entity_donation_pages; |
||
79 | $this->ppde_entity_transactions = $ppde_entity_transactions; |
||
80 | $this->ppde_operator_currency = $ppde_operator_currency; |
||
81 | $this->ppde_operator_donation_pages = $ppde_operator_donation_pages; |
||
82 | $this->ppde_operator_transactions = $ppde_operator_transactions; |
||
83 | $this->request = $request; |
||
84 | $this->template = $template; |
||
85 | $this->user = $user; |
||
86 | $this->root_path = $root_path; |
||
87 | $this->php_ext = $php_ext; |
||
88 | } |
||
89 | |||
90 | public function handle() |
||
91 | { |
||
92 | // When this extension is disabled, redirect users back to the forum index |
||
93 | // Else if user is not allowed to use it, disallow access to the extension main page |
||
94 | if (empty($this->config['ppde_enable'])) |
||
95 | { |
||
96 | redirect(append_sid("{$this->root_path}index.{$this->php_ext}")); |
||
97 | } |
||
98 | else if (!$this->can_use_ppde()) |
||
99 | { |
||
100 | trigger_error('NOT_AUTHORISED'); |
||
101 | } |
||
102 | |||
103 | $entity = $this->container->get('skouat.ppde.entity.donation_pages'); |
||
104 | $this->set_return_args_url($this->request->variable('return', 'body')); |
||
105 | |||
106 | // Prepare message for display |
||
107 | if ($this->get_donation_content_data($this->return_args_url)) |
||
108 | { |
||
109 | $entity->get_vars(); |
||
110 | $this->donation_body = $entity->replace_template_vars($entity->get_message_for_display( |
||
111 | $this->donation_content_data[0]['page_content'], |
||
112 | $this->donation_content_data[0]['page_content_bbcode_uid'], |
||
113 | $this->donation_content_data[0]['page_content_bbcode_bitfield'], |
||
114 | $this->donation_content_data[0]['page_content_bbcode_options'] |
||
115 | )); |
||
116 | } |
||
117 | |||
118 | $this->template->assign_vars(array( |
||
119 | 'DEFAULT_CURRENCY' => $this->build_currency_select_menu($this->config['ppde_default_currency']), |
||
120 | 'DONATION_BODY' => $this->donation_body, |
||
121 | 'IMG_LOADER' => '<img src="' . $this->root_path . '../ext/skouat/ppde/images/loader.gif' . '" />', |
||
122 | 'PPDE_DEFAULT_VALUE' => $this->config['ppde_default_value'] ? $this->config['ppde_default_value'] : 0, |
||
123 | 'PPDE_LIST_VALUE' => $this->build_currency_value_select_menu(), |
||
124 | |||
125 | 'S_HIDDEN_FIELDS' => $this->paypal_hidden_fields(), |
||
126 | 'S_PPDE_FORM_ACTION' => $this->get_paypal_url(), |
||
127 | 'S_RETURN_ARGS' => $this->return_args_url, |
||
128 | 'S_SANDBOX' => $this->use_sandbox(), |
||
129 | )); |
||
130 | |||
131 | $this->display_stats(); |
||
132 | |||
133 | // Send all data to the template file |
||
134 | return $this->send_data_to_template(); |
||
135 | } |
||
136 | |||
137 | public function donorlist_handle() |
||
138 | { |
||
139 | // When this extension is disabled, redirect users back to the forum index |
||
140 | // Else if user is not allowed to view the donors list, disallow access to the extension page |
||
141 | if (!$this->use_ipn()) |
||
142 | { |
||
143 | redirect(append_sid($this->root_path . 'index.' . $this->php_ext)); |
||
144 | } |
||
145 | else if (!$this->can_view_ppde_donorlist()) |
||
146 | { |
||
147 | trigger_error('NOT_AUTHORISED'); |
||
148 | } |
||
149 | |||
150 | // Get needed container |
||
151 | /** @type \phpbb\pagination $pagination */ |
||
152 | $pagination = $this->container->get('pagination'); |
||
153 | /** @type \phpbb\path_helper $path_helper */ |
||
154 | $path_helper = $this->container->get('path_helper'); |
||
155 | |||
156 | // Set up general vars |
||
157 | $default_key = 'd'; |
||
158 | $sort_key = $this->request->variable('sk', $default_key); |
||
159 | $sort_dir = $this->request->variable('sd', 'd'); |
||
160 | $start = $this->request->variable('start', 0); |
||
161 | |||
162 | // Sorting and order |
||
163 | $sort_key_sql = array('a' => 'amount', 'd' => 'txn.payment_date', 'u' => 'u.username_clean'); |
||
164 | |||
165 | if (!isset($sort_key_sql[$sort_key])) |
||
166 | { |
||
167 | $sort_key = $default_key; |
||
168 | } |
||
169 | |||
170 | $order_by = $sort_key_sql[$sort_key] . ' ' . (($sort_dir == 'a') ? 'ASC' : 'DESC'); |
||
171 | |||
172 | // Build a relevant pagination_url and sort_url |
||
173 | // We do not use request_var() here directly to save some calls (not all variables are set) |
||
174 | $check_params = array( |
||
175 | 'sk' => array('sk', $default_key), |
||
176 | 'sd' => array('sd', 'a'), |
||
177 | 'start' => array('start', 0), |
||
178 | ); |
||
179 | |||
180 | $params = $this->check_params($check_params, array('start')); |
||
181 | $sort_params = $this->check_params($check_params, array('sk', 'sd')); |
||
182 | |||
183 | // Set '$this->u_action' |
||
184 | $use_page = ($this->u_action) ? $this->u_action : $this->user->page['page_name']; |
||
185 | $this->u_action = reapply_sid($path_helper->get_valid_page($use_page, $this->config['enable_mod_rewrite'])); |
||
186 | |||
187 | $pagination_url = append_sid($this->u_action, implode('&', $params), true, false, true); |
||
188 | $sort_url = $this->set_url_delim(append_sid($this->u_action, implode('&', $sort_params), true, false, true), $sort_params); |
||
189 | |||
190 | $get_donorlist_sql_ary = $this->ppde_operator_transactions->get_sql_donorlist_ary(false, $order_by); |
||
191 | $total_donors = $this->ppde_operator_transactions->query_sql_count($get_donorlist_sql_ary, 'txn.user_id'); |
||
192 | $start = $pagination->validate_start($start, $this->config['topics_per_page'], $total_donors); |
||
193 | |||
194 | $pagination->generate_template_pagination($pagination_url, 'pagination', 'start', $total_donors, $this->config['topics_per_page'], $start); |
||
195 | |||
196 | // adds fields to the table schema needed by entity->import() |
||
197 | $additional_table_schema = array( |
||
198 | 'item_username' => array('name' => 'username', 'type' => 'string'), |
||
199 | 'item_user_colour' => array('name' => 'user_colour', 'type' => 'string'), |
||
200 | 'item_amount' => array('name' => 'amount', 'type' => 'float'), |
||
201 | 'item_max_txn_id' => array('name' => 'max_txn_id', 'type' => 'integer'), |
||
202 | ); |
||
203 | |||
204 | $data_ary = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($get_donorlist_sql_ary), $additional_table_schema, $this->config['topics_per_page'], $start); |
||
205 | |||
206 | // Get default currency data from the database |
||
207 | $default_currency_data = $this->get_default_currency_data($this->config['ppde_default_currency']); |
||
208 | $this->template->assign_vars(array( |
||
209 | 'TOTAL_DONORS' => $this->user->lang('PPDE_DONORS', $total_donors), |
||
210 | 'U_SORT_AMOUNT' => $sort_url . 'sk=a&sd=' . $this->set_sort_key($sort_key, 'a', $sort_dir), |
||
211 | 'U_SORT_DONATED' => $sort_url . 'sk=d&sd=' . $this->set_sort_key($sort_key, 'd', $sort_dir), |
||
212 | 'U_SORT_USERNAME' => $sort_url . 'sk=u&sd=' . $this->set_sort_key($sort_key, 'u', $sort_dir), |
||
213 | )); |
||
214 | |||
215 | foreach ($data_ary as $data) |
||
216 | { |
||
217 | $get_last_transaction_sql_ary = $this->ppde_operator_transactions->get_sql_donorlist_ary($data['max_txn_id']); |
||
218 | $last_donation_data = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($get_last_transaction_sql_ary)); |
||
219 | $this->template->assign_block_vars('donorrow', array( |
||
220 | 'PPDE_DONOR_USERNAME' => get_username_string('full', $data['user_id'], $data['username'], $data['user_colour']), |
||
221 | 'PPDE_LAST_DONATED_AMOUNT' => $this->get_amount(number_format($last_donation_data[0]['mc_gross'], 2), $default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']), |
||
222 | 'PPDE_LAST_PAYMENT_DATE' => $this->user->format_date($last_donation_data[0]['payment_date']), |
||
223 | 'PPDE_TOTAL_DONATED_AMOUNT' => $this->get_amount(number_format($data['amount'], 2), $default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']), |
||
224 | )); |
||
225 | } |
||
226 | |||
227 | // Set "return_args_url" object before sending data to template |
||
228 | $this->set_return_args_url('donorlist'); |
||
229 | |||
230 | // Send all data to the template file |
||
231 | return $this->send_data_to_template(); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Set the sort key value |
||
236 | * |
||
237 | * @param string $sk |
||
238 | * @param string $sk_comp |
||
239 | * @param string $sd |
||
240 | * |
||
241 | * @return string |
||
242 | * @access private |
||
243 | */ |
||
244 | private function set_sort_key($sk, $sk_comp, $sd) |
||
248 | |||
249 | /** |
||
250 | * Simply adds an url or & delimiter to the url when params is empty |
||
251 | * |
||
252 | * @param $url |
||
253 | * @param $params |
||
254 | * |
||
255 | * @return string |
||
256 | * @access private |
||
257 | */ |
||
258 | private function set_url_delim($url, $params) |
||
262 | |||
263 | /** |
||
264 | * @param array $params_ary |
||
265 | * @param string[] $excluded_keys |
||
266 | * |
||
267 | * @return array |
||
268 | * @access private |
||
269 | */ |
||
270 | private function check_params($params_ary, $excluded_keys) |
||
292 | |||
293 | /** |
||
294 | * @return bool |
||
295 | * @access public |
||
296 | */ |
||
297 | public function can_use_ppde() |
||
301 | |||
302 | /** |
||
303 | * @return bool |
||
304 | * @access public |
||
305 | */ |
||
306 | public function can_view_ppde_donorlist() |
||
310 | |||
311 | /** |
||
312 | * @param string $set_return_args_url |
||
313 | * |
||
314 | * @return null |
||
315 | * @access private |
||
316 | */ |
||
317 | private function set_return_args_url($set_return_args_url) |
||
339 | |||
340 | /** |
||
341 | * Get content of current donation pages |
||
342 | * |
||
343 | * @param string $return_args_url |
||
344 | * |
||
345 | * @return array |
||
346 | * @access private |
||
347 | */ |
||
348 | private function get_donation_content_data($return_args_url) |
||
354 | |||
355 | /** |
||
356 | * Build pull down menu options of available currency |
||
357 | * |
||
358 | * @param int $config_value Currency identifier; default: 0 |
||
359 | * |
||
360 | * @return null |
||
361 | * @access public |
||
362 | */ |
||
363 | public function build_currency_select_menu($config_value = 0) |
||
383 | |||
384 | /** |
||
385 | * Build pull down menu options of available currency value |
||
386 | * |
||
387 | * @return string List of currency value set in ACP for dropdown menu |
||
388 | * @access private |
||
389 | */ |
||
390 | private function build_currency_value_select_menu() |
||
408 | |||
409 | /** |
||
410 | * Get dropbox config value |
||
411 | * |
||
412 | * @return bool |
||
413 | * @access private |
||
414 | */ |
||
415 | private function get_dropbox_status() |
||
419 | |||
420 | /** |
||
421 | * Force dropbox value to integer |
||
422 | * |
||
423 | * @param int $value |
||
424 | * |
||
425 | * @return int |
||
426 | */ |
||
427 | private function settype_dropbox_int_value($value = 0) |
||
436 | |||
437 | /** |
||
438 | * Build PayPal hidden fields |
||
439 | * |
||
440 | * @return string PayPal hidden field needed to fill PayPal forms |
||
441 | * @access private |
||
442 | */ |
||
443 | private function paypal_hidden_fields() |
||
459 | |||
460 | /** |
||
461 | * Get PayPal account id |
||
462 | * |
||
463 | * @return string $this Paypal account Identifier |
||
464 | * @access private |
||
465 | */ |
||
466 | private function get_account_id() |
||
470 | |||
471 | /** |
||
472 | * Check if Sandbox is enabled |
||
473 | * |
||
474 | * @return bool |
||
475 | * @access public |
||
476 | */ |
||
477 | public function use_sandbox() |
||
481 | |||
482 | /** |
||
483 | * Check if IPN is enabled |
||
484 | * |
||
485 | * @return bool |
||
486 | * @access public |
||
487 | */ |
||
488 | public function use_ipn() |
||
492 | |||
493 | /** |
||
494 | * Generate PayPal return URL |
||
495 | * |
||
496 | * @param string $arg |
||
497 | * |
||
498 | * @return string |
||
499 | * @access private |
||
500 | */ |
||
501 | private function generate_paypal_return_url($arg) |
||
505 | |||
506 | /** |
||
507 | * Generate PayPal return notify URL |
||
508 | * |
||
509 | * @return string |
||
510 | * @access private |
||
511 | */ |
||
512 | private function generate_paypal_notify_return_url() |
||
516 | |||
517 | /** |
||
518 | * Get PayPal URL |
||
519 | * Used in form and in IPN process |
||
520 | * |
||
521 | * @param bool $is_test_ipn |
||
522 | * |
||
523 | * @return string |
||
524 | * @access public |
||
525 | */ |
||
526 | public function get_paypal_url($is_test_ipn = false) |
||
530 | |||
531 | /** |
||
532 | * Assign statistics vars to the template |
||
533 | * |
||
534 | * @return null |
||
535 | * @access public |
||
536 | */ |
||
537 | public function display_stats() |
||
558 | |||
559 | /** |
||
560 | * Get default currency symbol |
||
561 | * |
||
562 | * @param int $id |
||
563 | * |
||
564 | * @return array |
||
565 | * @access public |
||
566 | */ |
||
567 | public function get_default_currency_data($id = 0) |
||
571 | |||
572 | /** |
||
573 | * Retrieve the language key for donation goal |
||
574 | * |
||
575 | * @param string $currency_symbol Currency symbol |
||
576 | * @param bool $on_left Symbol position |
||
577 | * |
||
578 | * @return string |
||
579 | * @access public |
||
580 | */ |
||
581 | public function get_ppde_goal_langkey($currency_symbol, $on_left = true) |
||
598 | |||
599 | /** |
||
600 | * Put the currency on the left or on the right of the amount |
||
601 | * |
||
602 | * @param int $value |
||
603 | * @param string $currency |
||
604 | * @param bool $on_left |
||
605 | * |
||
606 | * @return string |
||
607 | */ |
||
608 | private function get_amount($value, $currency, $on_left = true) |
||
612 | |||
613 | /** |
||
614 | * Retrieve the language key for donation raised |
||
615 | * |
||
616 | * @param string $currency_symbol Currency symbol |
||
617 | * @param bool $on_left Symbol position |
||
618 | * |
||
619 | * @return string |
||
620 | * @access public |
||
621 | */ |
||
622 | public function get_ppde_raised_langkey($currency_symbol, $on_left = true) |
||
635 | |||
636 | /** |
||
637 | * Retrieve the language key for donation used |
||
638 | * |
||
639 | * @param string $currency_symbol Currency symbol |
||
640 | * @param bool $on_left Symbol position |
||
641 | * |
||
642 | * @return string |
||
643 | * @access public |
||
644 | */ |
||
645 | public function get_ppde_used_langkey($currency_symbol, $on_left = true) |
||
662 | |||
663 | /** |
||
664 | * Generate statistics percent for display |
||
665 | * |
||
666 | * @return null |
||
667 | * @access private |
||
668 | */ |
||
669 | private function generate_stats_percent() |
||
681 | |||
682 | /** |
||
683 | * Assign statistics percent vars to template |
||
684 | * |
||
685 | * @param integer $multiplicand |
||
686 | * @param integer $dividend |
||
687 | * @param string $type |
||
688 | * |
||
689 | * @return null |
||
690 | * @access public |
||
691 | */ |
||
692 | private function assign_vars_stats_percent($multiplicand, $dividend, $type = '') |
||
699 | |||
700 | /** |
||
701 | * Send data to the template file |
||
702 | * |
||
703 | * @return \Symfony\Component\HttpFoundation\Response |
||
704 | * @access private |
||
705 | */ |
||
706 | private function send_data_to_template() |
||
719 | |||
720 | /** |
||
721 | * Check if cURL is available |
||
722 | * |
||
723 | * @return bool |
||
724 | * @access public |
||
725 | */ |
||
726 | public function check_curl() |
||
746 | |||
747 | /** |
||
748 | * Get extension metadata |
||
749 | * |
||
750 | * @return null |
||
751 | * @access protected |
||
752 | */ |
||
753 | protected function get_ext_meta() |
||
760 | |||
761 | /** |
||
762 | * Load metadata for this extension |
||
763 | * |
||
764 | * @return array |
||
765 | * @access public |
||
766 | */ |
||
767 | public function load_metadata() |
||
789 | |||
790 | /** |
||
791 | * Retrieve the extension name |
||
792 | * |
||
793 | * @return null |
||
794 | * @access protected |
||
795 | */ |
||
796 | protected function retrieve_ext_name() |
||
801 | |||
802 | /** |
||
803 | * Check if fsockopen is available |
||
804 | * |
||
805 | * @return bool |
||
806 | * @access public |
||
807 | */ |
||
808 | public function check_fsockopen() |
||
823 | } |
||
824 |