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 EE_PMT_Base 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 EE_PMT_Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class EE_PMT_Base |
||
22 | { |
||
23 | |||
24 | const onsite = 'on-site'; |
||
25 | const offsite = 'off-site'; |
||
26 | const offline = 'off-line'; |
||
27 | |||
28 | /** |
||
29 | * @var EE_Payment_Method |
||
30 | */ |
||
31 | protected $_pm_instance = NULL; |
||
32 | |||
33 | /** |
||
34 | * @var boolean |
||
35 | */ |
||
36 | protected $_requires_https = FALSE; |
||
37 | |||
38 | /** |
||
39 | * @var boolean |
||
40 | */ |
||
41 | protected $_has_billing_form; |
||
42 | |||
43 | /** |
||
44 | * @var EE_Gateway |
||
45 | */ |
||
46 | protected $_gateway = NULL; |
||
47 | |||
48 | /** |
||
49 | * @var EE_Payment_Method_Form |
||
50 | */ |
||
51 | protected $_settings_form = NULL; |
||
52 | |||
53 | /** |
||
54 | * @var EE_Form_Section_Proper |
||
55 | */ |
||
56 | protected $_billing_form = NULL; |
||
57 | |||
58 | /** |
||
59 | * @var boolean |
||
60 | */ |
||
61 | protected $_cache_billing_form = TRUE; |
||
62 | |||
63 | /** |
||
64 | * String of the absolute path to the folder containing this file, with a trailing slash. |
||
65 | * eg '/public_html/wp-site/wp-content/plugins/event-espresso/payment_methods/Invoice/' |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $_file_folder = NULL; |
||
69 | |||
70 | /** |
||
71 | * String to the absolute URL to this file (useful for getting its web-accessible resources |
||
72 | * like images, js, or css) |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $_file_url = NULL; |
||
76 | |||
77 | /** |
||
78 | * Pretty name for the payment method |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $_pretty_name = NULL; |
||
82 | |||
83 | /** |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $_default_button_url = NULL; |
||
88 | |||
89 | /** |
||
90 | * |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $_default_description = NULL; |
||
94 | |||
95 | |||
96 | /** |
||
97 | * |
||
98 | * @param EE_Payment_Method $pm_instance |
||
99 | * @throws EE_Error |
||
100 | * @return EE_PMT_Base |
||
|
|||
101 | */ |
||
102 | function __construct($pm_instance = NULL) |
||
103 | { |
||
104 | if ($pm_instance instanceof EE_Payment_Method) { |
||
105 | $this->set_instance($pm_instance); |
||
106 | } |
||
107 | if ($this->_gateway) { |
||
108 | $this->_gateway->set_payment_model(EEM_Payment::instance()); |
||
109 | $this->_gateway->set_payment_log(EEM_Change_Log::instance()); |
||
110 | $this->_gateway->set_template_helper(new EEH_Template()); |
||
111 | $this->_gateway->set_line_item_helper(new EEH_Line_Item()); |
||
112 | $this->_gateway->set_money_helper(new EEH_Money()); |
||
113 | $this->_gateway->set_gateway_data_formatter(new GatewayDataFormatter()); |
||
114 | $this->_gateway->set_unsupported_character_remover(new AsciiOnly()); |
||
115 | do_action('AHEE__EE_PMT_Base___construct__done_initializing_gateway_class', $this, $this->_gateway); |
||
116 | } |
||
117 | if (!isset($this->_has_billing_form)) { |
||
118 | // by default, On Site gateways have a billing form |
||
119 | if ($this->payment_occurs() == EE_PMT_Base::onsite) { |
||
120 | $this->set_has_billing_form(true); |
||
121 | } else { |
||
122 | $this->set_has_billing_form(false); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | if (!$this->_pretty_name) { |
||
127 | throw new EE_Error(sprintf(__("You must set the pretty name for the Payment Method Type in the constructor (_pretty_name), and please make it internationalized", "event_espresso"))); |
||
128 | } |
||
129 | //if the child didn't specify a default button, use the credit card one |
||
130 | if ($this->_default_button_url === NULL) { |
||
131 | $this->_default_button_url = EE_PLUGIN_DIR_URL . 'payment_methods' . DS . 'pay-by-credit-card.png'; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | |||
136 | /** |
||
137 | * @param boolean $has_billing_form |
||
138 | */ |
||
139 | public function set_has_billing_form($has_billing_form) |
||
140 | { |
||
141 | $this->_has_billing_form = filter_var($has_billing_form, FILTER_VALIDATE_BOOLEAN); |
||
142 | } |
||
143 | |||
144 | |||
145 | /** |
||
146 | * sets the file_folder property |
||
147 | */ |
||
148 | protected function _set_file_folder() |
||
149 | { |
||
150 | $reflector = new ReflectionClass(get_class($this)); |
||
151 | $fn = $reflector->getFileName(); |
||
152 | $this->_file_folder = dirname($fn) . DS; |
||
153 | } |
||
154 | |||
155 | |||
156 | /** |
||
157 | * sets the file URL with a trailing slash for this PMT |
||
158 | */ |
||
159 | protected function _set_file_url() |
||
160 | { |
||
161 | $plugins_dir_fixed = str_replace('\\', DS, WP_PLUGIN_DIR); |
||
162 | $file_folder_fixed = str_replace('\\', DS, $this->file_folder()); |
||
163 | $file_path = str_replace($plugins_dir_fixed, WP_PLUGIN_URL, $file_folder_fixed); |
||
164 | $this->_file_url = $file_path; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Gets the default description on all payment methods of this type |
||
169 | * @return string |
||
170 | */ |
||
171 | public function default_description() |
||
172 | { |
||
173 | return $this->_default_description; |
||
174 | } |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Returns the folder containing the PMT child class, with a trailing slash |
||
179 | * @return string |
||
180 | */ |
||
181 | public function file_folder() |
||
182 | { |
||
183 | if (!$this->_file_folder) { |
||
184 | $this->_set_file_folder(); |
||
185 | } |
||
186 | return $this->_file_folder; |
||
187 | } |
||
188 | |||
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | public function file_url() |
||
194 | { |
||
195 | if (!$this->_file_url) { |
||
196 | $this->_set_file_url(); |
||
197 | } |
||
198 | return $this->_file_url; |
||
199 | } |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Sets the payment method instance this payment method type is for. |
||
204 | * Its important teh payment method instance is set before |
||
205 | * @param EE_Payment_Method $payment_method_instance |
||
206 | */ |
||
207 | function set_instance($payment_method_instance) |
||
208 | { |
||
209 | $this->_pm_instance = $payment_method_instance; |
||
210 | //if they have already requested the settings form, make sure its |
||
211 | //data matches this model object |
||
212 | if ($this->_settings_form) { |
||
213 | $this->settings_form()->populate_model_obj($payment_method_instance); |
||
214 | } |
||
215 | if ($this->_gateway && $this->_gateway instanceof EE_Gateway) { |
||
216 | $this->_gateway->set_settings($payment_method_instance->settings_array()); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | |||
221 | /** |
||
222 | * Gets teh form for displaying to admins where they setup the payment method |
||
223 | * @return EE_Payment_Method_Form |
||
224 | */ |
||
225 | function settings_form() |
||
226 | { |
||
227 | if (!$this->_settings_form) { |
||
228 | $this->_settings_form = $this->generate_new_settings_form(); |
||
229 | $this->_settings_form->set_payment_method_type($this); |
||
230 | //if we have already assigned a model object to this pmt, make |
||
231 | //sure its reflected in teh form we just generated |
||
232 | if ($this->_pm_instance) { |
||
233 | $this->_settings_form->populate_model_obj($this->_pm_instance); |
||
234 | } |
||
235 | } |
||
236 | return $this->_settings_form; |
||
237 | } |
||
238 | |||
239 | |||
240 | /** |
||
241 | * Gets the form for all the settings related to this payment method type |
||
242 | * @return EE_Payment_Method_Form |
||
243 | */ |
||
244 | abstract function generate_new_settings_form(); |
||
245 | |||
246 | |||
247 | /** |
||
248 | * Sets the form for settings. This may be useful if we have already received |
||
249 | * a form submission and have form data it in, and want to use it anytime we're showing |
||
250 | * this payment method type's settings form later in the request |
||
251 | * @param EE_Payment_Method_Form $form |
||
252 | */ |
||
253 | public function set_settings_form($form) |
||
254 | { |
||
255 | $this->_settings_form = $form; |
||
256 | } |
||
257 | |||
258 | |||
259 | /** |
||
260 | * @return boolean |
||
261 | */ |
||
262 | public function has_billing_form() |
||
263 | { |
||
264 | return $this->_has_billing_form; |
||
265 | } |
||
266 | |||
267 | |||
268 | /** |
||
269 | * Gets the form for displaying to attendees where they can enter their billing info |
||
270 | * which will be sent to teh gateway (can be null) |
||
271 | * |
||
272 | * @param \EE_Transaction $transaction |
||
273 | * @param array $extra_args |
||
274 | * @return \EE_Billing_Attendee_Info_Form|\EE_Billing_Info_Form|null |
||
275 | */ |
||
276 | public function billing_form(EE_Transaction $transaction = NULL, $extra_args = array()) |
||
277 | { |
||
278 | // has billing form already been regenerated ? or overwrite cache? |
||
279 | if (!$this->_billing_form instanceof EE_Billing_Info_Form || !$this->_cache_billing_form) { |
||
280 | $this->_billing_form = $this->generate_new_billing_form($transaction, $extra_args); |
||
281 | } |
||
282 | //if we know who the attendee is, and this is a billing form |
||
283 | //that uses attendee info, populate it |
||
284 | if ( |
||
285 | apply_filters( |
||
286 | 'FHEE__populate_billing_form_fields_from_attendee', |
||
287 | ( |
||
288 | $this->_billing_form instanceof EE_Billing_Attendee_Info_Form |
||
289 | && $transaction instanceof EE_Transaction |
||
290 | && $transaction->primary_registration() instanceof EE_Registration |
||
291 | && $transaction->primary_registration()->attendee() instanceof EE_Attendee |
||
292 | ), |
||
293 | $this->_billing_form, |
||
294 | $transaction |
||
295 | ) |
||
296 | ) { |
||
297 | $this->_billing_form->populate_from_attendee($transaction->primary_registration()->attendee()); |
||
298 | } |
||
299 | return $this->_billing_form; |
||
300 | } |
||
301 | |||
302 | |||
303 | /** |
||
304 | * Creates the billing form for this payment method type |
||
305 | * @param \EE_Transaction $transaction |
||
306 | * @return \EE_Billing_Info_Form |
||
307 | */ |
||
308 | abstract function generate_new_billing_form(EE_Transaction $transaction = NULL); |
||
309 | |||
310 | |||
311 | /** |
||
312 | * apply_billing_form_debug_settings |
||
313 | * applies debug data to the form |
||
314 | * |
||
315 | * @param \EE_Billing_Info_Form $billing_form |
||
316 | * @return \EE_Billing_Info_Form |
||
317 | */ |
||
318 | public function apply_billing_form_debug_settings(EE_Billing_Info_Form $billing_form) |
||
319 | { |
||
320 | return $billing_form; |
||
321 | } |
||
322 | |||
323 | |||
324 | /** |
||
325 | * Sets the billing form for this payment method type. You may want to use this |
||
326 | * if you have form |
||
327 | * @param EE_Payment_Method $form |
||
328 | */ |
||
329 | public function set_billing_form($form) |
||
333 | |||
334 | |||
335 | /** |
||
336 | * Returns whether or not this payment method requires HTTPS to be used |
||
337 | * @return boolean |
||
338 | */ |
||
339 | function requires_https() |
||
343 | |||
344 | |||
345 | /** |
||
346 | * |
||
347 | * @param EE_Transaction $transaction |
||
348 | * @param float $amount |
||
349 | * @param EE_Billing_Info_Form $billing_info |
||
350 | * @param string $return_url |
||
351 | * @param string $fail_url |
||
352 | * @param string $method |
||
353 | * @param bool $by_admin |
||
354 | * @return EE_Payment |
||
355 | * @throws EE_Error |
||
356 | */ |
||
357 | function process_payment(EE_Transaction $transaction, $amount = null, $billing_info = null, $return_url = null, $fail_url = '', $method = 'CART', $by_admin = false) |
||
447 | |||
448 | /** |
||
449 | * Gets the values we want to pass onto the gateway. Normally these |
||
450 | * are just the 'pretty' values, but there may be times the data may need |
||
451 | * a little massaging. Proper subsections will become arrays of inputs |
||
452 | * @param EE_Billing_Info_Form $billing_form |
||
453 | * @return array |
||
454 | */ |
||
455 | protected function _get_billing_values_from_form($billing_form) |
||
463 | |||
464 | |||
465 | /** |
||
466 | * Handles an instant payment notification when the transaction is known (by default). |
||
467 | * @param array $req_data |
||
468 | * @param EE_Transaction $transaction |
||
469 | * @return EE_Payment |
||
470 | * @throws EE_Error |
||
471 | */ |
||
472 | public function handle_ipn($req_data, $transaction) |
||
482 | |||
483 | |||
484 | /** |
||
485 | * Saves the billing info onto the attendee of the primary registrant on this transaction, and |
||
486 | * cleans it first. |
||
487 | * @param EE_Billing_Attendee_Info_Form $billing_form |
||
488 | * @param EE_Transaction $transaction |
||
489 | * @return boolean success |
||
490 | */ |
||
491 | protected function _save_billing_info_to_attendee($billing_form, $transaction) |
||
492 | { |
||
493 | View Code Duplication | if (!$transaction || !$transaction instanceof EE_Transaction) { |
|
494 | EE_Error::add_error(__("Cannot save billing info because no transaction was specified", "event_espresso"), __FILE__, __FUNCTION__, __LINE__); |
||
495 | return false; |
||
496 | } |
||
497 | $primary_reg = $transaction->primary_registration(); |
||
498 | if (!$primary_reg) { |
||
499 | EE_Error::add_error(__("Cannot save billing info because the transaction has no primary registration", "event_espresso"), __FILE__, __FUNCTION__, __LINE__); |
||
500 | return false; |
||
501 | } |
||
502 | $attendee = $primary_reg->attendee(); |
||
503 | if (!$attendee) { |
||
504 | EE_Error::add_error(__("Cannot save billing info because the transaction's primary registration has no attendee!", "event_espresso"), __FILE__, __FUNCTION__, __LINE__); |
||
505 | return false; |
||
506 | } |
||
507 | return $attendee->save_and_clean_billing_info_for_payment_method($billing_form, $transaction->payment_method()); |
||
508 | |||
509 | } |
||
510 | |||
511 | |||
512 | /** |
||
513 | * Gets the payment this IPN is for. Children may often want to |
||
514 | * override this to inspect the request |
||
515 | * @param EE_Transaction $transaction |
||
516 | * @param array $req_data |
||
517 | * @return EE_Payment |
||
518 | */ |
||
519 | protected function find_payment_for_ipn(EE_Transaction $transaction, $req_data = array()) |
||
523 | |||
524 | |||
525 | /** |
||
526 | * In case generic code cannot provide the payment processor with a specific payment method |
||
527 | * and transaction, it will try calling this method on each activate payment method. |
||
528 | * If the payment method is able to identify the request as being for it, it should fetch |
||
529 | * the payment its for and return it. If not, it should throw an EE_Error to indicate it cannot |
||
530 | * handle the IPN |
||
531 | * @param array $req_data |
||
532 | * @return EE_Payment only if this payment method can find the info its needs from $req_data |
||
533 | * and identifies the IPN as being for this payment method (not just fo ra payment method of this type) |
||
534 | * @throws EE_Error |
||
535 | */ |
||
536 | public function handle_unclaimed_ipn($req_data = array()) |
||
540 | |||
541 | |||
542 | /** |
||
543 | * Logic to be accomplished when the payment attempt is complete. |
||
544 | * Most payment methods don't need to do anything at this point; but some, like Mijireh, do. |
||
545 | * (Mijireh is an offsite gateway which doesn't send an IPN. So when the user returns to EE from |
||
546 | * mijireh, this method needs to be called so the Mijireh PM can ping Mijireh to know the status |
||
547 | * of the payment). Fed a transaction because it's always assumed to be the last payment that |
||
548 | * we're dealing with. Returns that last payment (if there is one) |
||
549 | * |
||
550 | * @param EE_Transaction $transaction |
||
551 | * @return EE_Payment |
||
552 | */ |
||
553 | public function finalize_payment_for($transaction) |
||
557 | |||
558 | |||
559 | /** |
||
560 | * Whether or not this payment method's gateway supports sending refund requests |
||
561 | * @return boolean |
||
562 | */ |
||
563 | public function supports_sending_refunds() |
||
571 | |||
572 | |||
573 | /** |
||
574 | * |
||
575 | * @param EE_Payment $payment |
||
576 | * @param array $refund_info |
||
577 | * @throws EE_Error |
||
578 | * @return EE_Payment |
||
579 | */ |
||
580 | public function process_refund(EE_Payment $payment, $refund_info = array()) |
||
593 | |||
594 | |||
595 | /** |
||
596 | * Returns one the class's constants onsite,offsite, or offline, depending on this |
||
597 | * payment method's gateway. |
||
598 | * @return string |
||
599 | * @throws EE_Error |
||
600 | */ |
||
601 | public function payment_occurs() |
||
613 | |||
614 | |||
615 | /** |
||
616 | * For adding any html output ab ove the payment overview. |
||
617 | * Many gateways won't want ot display anything, so this function just returns an empty string. |
||
618 | * Other gateways may want to override this, such as offline gateways. |
||
619 | * @param EE_Payment $payment |
||
620 | * @return string |
||
621 | */ |
||
622 | public function payment_overview_content(EE_Payment $payment) |
||
626 | |||
627 | |||
628 | /** |
||
629 | * @return array where keys are the help tab name, |
||
630 | * values are: array { |
||
631 | * @type string $title i18n name for the help tab |
||
632 | * @type string $filename name of the file located in ./help_tabs/ (ie, in a folder next to this file) |
||
633 | * @type array $template_args any arguments you want passed to the template file while rendering. |
||
634 | * Keys will be variable names and values with be their values. |
||
635 | */ |
||
636 | public function help_tabs_config() |
||
640 | |||
641 | |||
642 | /** |
||
643 | * The system name for this PMT (eg AIM, Paypal_Pro, Invoice... what gets put into |
||
644 | * the payment method's table's PMT_type column) |
||
645 | * @return string |
||
646 | */ |
||
647 | public function system_name() |
||
652 | |||
653 | |||
654 | /** |
||
655 | * A pretty i18n version of the PMT name |
||
656 | * @return string |
||
657 | */ |
||
658 | public function pretty_name() |
||
662 | |||
663 | |||
664 | /** |
||
665 | * Gets the default absolute URL to the payment method type's button |
||
666 | * @return string |
||
667 | */ |
||
668 | public function default_button_url() |
||
672 | |||
673 | |||
674 | /** |
||
675 | * Gets the gateway used by this payment method (if any) |
||
676 | * @return EE_Gateway |
||
677 | */ |
||
678 | public function get_gateway() |
||
682 | |||
683 | |||
684 | /** |
||
685 | * @return string html for the link to a help tab |
||
686 | */ |
||
687 | public function get_help_tab_link() |
||
691 | |||
692 | |||
693 | /** |
||
694 | * Returns the name of the help tab for this PMT |
||
695 | * @return string |
||
696 | */ |
||
697 | public function get_help_tab_name() |
||
701 | |||
702 | /** |
||
703 | * The name of the wp capability that should be associated with the usage of |
||
704 | * this PMT by an admin |
||
705 | * @return string |
||
706 | */ |
||
707 | public function cap_name() |
||
711 | |||
712 | /** |
||
713 | * Called by client code to tell the gateway that if it wants to change |
||
714 | * the transaction or line items or registrations related to teh payment it already |
||
715 | * processed (we think, but possibly not) that now's the time to do it. |
||
716 | * It is expected that gateways will store any info they need for this on the PAY_details, |
||
717 | * or maybe an extra meta value |
||
718 | * @param EE_Payment $payment |
||
719 | * @return void |
||
720 | */ |
||
721 | public function update_txn_based_on_payment($payment) |
||
727 | |||
728 | /** |
||
729 | * Returns a string of HTML describing this payment method type for an admin, |
||
730 | * primarily intended for them to read before activating it. |
||
731 | * The easiest way to set this is to create a folder 'templates' alongside |
||
732 | * your EE_PMT_{System_Name} file, and in it create a file named "{system_name}_intro.template.php". |
||
733 | * Eg, if your payment method file is named "EE_PMT_Foo_Bar.pm.php", |
||
734 | * then you'd create a file named "templates" in the same folder as it, and name the file |
||
735 | * "foo_bar_intro.template.php", and its content will be returned by this method |
||
736 | * @return string |
||
737 | */ |
||
738 | public function introductory_html() |
||
742 | |||
743 | |||
744 | } |
||
745 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.