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 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $_file_folder = null; |
||
70 | |||
71 | /** |
||
72 | * String to the absolute URL to this file (useful for getting its web-accessible resources |
||
73 | * like images, js, or css) |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $_file_url = null; |
||
78 | |||
79 | /** |
||
80 | * Pretty name for the payment method |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $_pretty_name = null; |
||
85 | |||
86 | /** |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $_default_button_url = null; |
||
91 | |||
92 | /** |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $_default_description = null; |
||
97 | |||
98 | |||
99 | /** |
||
100 | * |
||
101 | * @param EE_Payment_Method $pm_instance |
||
102 | * @throws EE_Error |
||
103 | * @return EE_PMT_Base |
||
|
|||
104 | */ |
||
105 | public function __construct($pm_instance = null) |
||
106 | { |
||
107 | if ($pm_instance instanceof EE_Payment_Method) { |
||
108 | $this->set_instance($pm_instance); |
||
109 | } |
||
110 | if ($this->_gateway) { |
||
111 | $this->_gateway->set_payment_model(EEM_Payment::instance()); |
||
112 | $this->_gateway->set_payment_log(EEM_Change_Log::instance()); |
||
113 | $this->_gateway->set_template_helper(new EEH_Template()); |
||
114 | $this->_gateway->set_line_item_helper(new EEH_Line_Item()); |
||
115 | $this->_gateway->set_money_helper(new EEH_Money()); |
||
116 | $this->_gateway->set_gateway_data_formatter(new GatewayDataFormatter()); |
||
117 | $this->_gateway->set_unsupported_character_remover(new AsciiOnly()); |
||
118 | do_action('AHEE__EE_PMT_Base___construct__done_initializing_gateway_class', $this, $this->_gateway); |
||
119 | } |
||
120 | if (! isset($this->_has_billing_form)) { |
||
121 | // by default, On Site gateways have a billing form |
||
122 | if ($this->payment_occurs() == EE_PMT_Base::onsite) { |
||
123 | $this->set_has_billing_form(true); |
||
124 | } else { |
||
125 | $this->set_has_billing_form(false); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | if (! $this->_pretty_name) { |
||
130 | throw new EE_Error( |
||
131 | sprintf( |
||
132 | __( |
||
133 | "You must set the pretty name for the Payment Method Type in the constructor (_pretty_name), and please make it internationalized", |
||
134 | "event_espresso" |
||
135 | ) |
||
136 | ) |
||
137 | ); |
||
138 | } |
||
139 | // if the child didn't specify a default button, use the credit card one |
||
140 | if ($this->_default_button_url === null) { |
||
141 | $this->_default_button_url = EE_PLUGIN_DIR_URL . 'payment_methods/pay-by-credit-card.png'; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | |||
146 | /** |
||
147 | * @param boolean $has_billing_form |
||
148 | */ |
||
149 | public function set_has_billing_form($has_billing_form) |
||
150 | { |
||
151 | $this->_has_billing_form = filter_var($has_billing_form, FILTER_VALIDATE_BOOLEAN); |
||
152 | } |
||
153 | |||
154 | |||
155 | /** |
||
156 | * sets the file_folder property |
||
157 | */ |
||
158 | protected function _set_file_folder() |
||
159 | { |
||
160 | $reflector = new ReflectionClass(get_class($this)); |
||
161 | $fn = $reflector->getFileName(); |
||
162 | $this->_file_folder = dirname($fn) . '/'; |
||
163 | } |
||
164 | |||
165 | |||
166 | /** |
||
167 | * sets the file URL with a trailing slash for this PMT |
||
168 | */ |
||
169 | protected function _set_file_url() |
||
170 | { |
||
171 | $plugins_dir_fixed = str_replace('\\', '/', WP_PLUGIN_DIR); |
||
172 | $file_folder_fixed = str_replace('\\', '/', $this->file_folder()); |
||
173 | $file_path = str_replace($plugins_dir_fixed, WP_PLUGIN_URL, $file_folder_fixed); |
||
174 | $this->_file_url = set_url_scheme($file_path); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Gets the default description on all payment methods of this type |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function default_description() |
||
183 | { |
||
184 | return $this->_default_description; |
||
185 | } |
||
186 | |||
187 | |||
188 | /** |
||
189 | * Returns the folder containing the PMT child class, with a trailing slash |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function file_folder() |
||
194 | { |
||
195 | if (! $this->_file_folder) { |
||
196 | $this->_set_file_folder(); |
||
197 | } |
||
198 | return $this->_file_folder; |
||
199 | } |
||
200 | |||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | public function file_url() |
||
206 | { |
||
207 | if (! $this->_file_url) { |
||
208 | $this->_set_file_url(); |
||
209 | } |
||
210 | return $this->_file_url; |
||
211 | } |
||
212 | |||
213 | |||
214 | /** |
||
215 | * Sets the payment method instance this payment method type is for. |
||
216 | * Its important teh payment method instance is set before |
||
217 | * |
||
218 | * @param EE_Payment_Method $payment_method_instance |
||
219 | */ |
||
220 | public function set_instance($payment_method_instance) |
||
221 | { |
||
222 | $this->_pm_instance = $payment_method_instance; |
||
223 | // if they have already requested the settings form, make sure its |
||
224 | // data matches this model object |
||
225 | if ($this->_settings_form) { |
||
226 | $this->settings_form()->populate_model_obj($payment_method_instance); |
||
227 | } |
||
228 | if ($this->_gateway && $this->_gateway instanceof EE_Gateway) { |
||
229 | $this->_gateway->set_settings($payment_method_instance->settings_array()); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | |||
234 | /** |
||
235 | * Gets teh form for displaying to admins where they setup the payment method |
||
236 | * |
||
237 | * @return EE_Payment_Method_Form |
||
238 | */ |
||
239 | public function settings_form() |
||
240 | { |
||
241 | if (! $this->_settings_form) { |
||
242 | $this->_settings_form = $this->generate_new_settings_form(); |
||
243 | $this->_settings_form->set_payment_method_type($this); |
||
244 | // if we have already assigned a model object to this pmt, make |
||
245 | // sure its reflected in teh form we just generated |
||
246 | if ($this->_pm_instance) { |
||
247 | $this->_settings_form->populate_model_obj($this->_pm_instance); |
||
248 | } |
||
249 | } |
||
250 | return $this->_settings_form; |
||
251 | } |
||
252 | |||
253 | |||
254 | /** |
||
255 | * Gets the form for all the settings related to this payment method type |
||
256 | * |
||
257 | * @return EE_Payment_Method_Form |
||
258 | */ |
||
259 | abstract public function generate_new_settings_form(); |
||
260 | |||
261 | |||
262 | /** |
||
263 | * Sets the form for settings. This may be useful if we have already received |
||
264 | * a form submission and have form data it in, and want to use it anytime we're showing |
||
265 | * this payment method type's settings form later in the request |
||
266 | * |
||
267 | * @param EE_Payment_Method_Form $form |
||
268 | */ |
||
269 | public function set_settings_form($form) |
||
270 | { |
||
271 | $this->_settings_form = $form; |
||
272 | } |
||
273 | |||
274 | |||
275 | /** |
||
276 | * @return boolean |
||
277 | */ |
||
278 | public function has_billing_form() |
||
279 | { |
||
280 | return $this->_has_billing_form; |
||
281 | } |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Gets the form for displaying to attendees where they can enter their billing info |
||
286 | * which will be sent to teh gateway (can be null) |
||
287 | * |
||
288 | * @param \EE_Transaction $transaction |
||
289 | * @param array $extra_args |
||
290 | * @return \EE_Billing_Attendee_Info_Form|\EE_Billing_Info_Form|null |
||
291 | */ |
||
292 | public function billing_form(EE_Transaction $transaction = null, $extra_args = array()) |
||
293 | { |
||
294 | // has billing form already been regenerated ? or overwrite cache? |
||
295 | if (! $this->_billing_form instanceof EE_Billing_Info_Form || ! $this->_cache_billing_form) { |
||
296 | $this->_billing_form = $this->generate_new_billing_form($transaction, $extra_args); |
||
297 | } |
||
298 | // if we know who the attendee is, and this is a billing form |
||
299 | // that uses attendee info, populate it |
||
300 | if (apply_filters( |
||
301 | 'FHEE__populate_billing_form_fields_from_attendee', |
||
302 | ($this->_billing_form instanceof EE_Billing_Attendee_Info_Form |
||
303 | && $transaction instanceof EE_Transaction |
||
304 | && $transaction->primary_registration() instanceof EE_Registration |
||
305 | && $transaction->primary_registration()->attendee() instanceof EE_Attendee |
||
306 | ), |
||
307 | $this->_billing_form, |
||
308 | $transaction |
||
309 | )) { |
||
310 | $this->_billing_form->populate_from_attendee($transaction->primary_registration()->attendee()); |
||
311 | } |
||
312 | return $this->_billing_form; |
||
313 | } |
||
314 | |||
315 | |||
316 | /** |
||
317 | * Creates the billing form for this payment method type |
||
318 | * |
||
319 | * @param \EE_Transaction $transaction |
||
320 | * @return \EE_Billing_Info_Form |
||
321 | */ |
||
322 | abstract public function generate_new_billing_form(EE_Transaction $transaction = null); |
||
323 | |||
324 | |||
325 | /** |
||
326 | * apply_billing_form_debug_settings |
||
327 | * applies debug data to the form |
||
328 | * |
||
329 | * @param \EE_Billing_Info_Form $billing_form |
||
330 | * @return \EE_Billing_Info_Form |
||
331 | */ |
||
332 | public function apply_billing_form_debug_settings(EE_Billing_Info_Form $billing_form) |
||
333 | { |
||
334 | return $billing_form; |
||
335 | } |
||
336 | |||
337 | |||
338 | /** |
||
339 | * Sets the billing form for this payment method type. You may want to use this |
||
340 | * if you have form |
||
341 | * |
||
342 | * @param EE_Payment_Method $form |
||
343 | */ |
||
344 | public function set_billing_form($form) |
||
348 | |||
349 | |||
350 | /** |
||
351 | * Returns whether or not this payment method requires HTTPS to be used |
||
352 | * |
||
353 | * @return boolean |
||
354 | */ |
||
355 | public function requires_https() |
||
359 | |||
360 | |||
361 | /** |
||
362 | * |
||
363 | * @param EE_Transaction $transaction |
||
364 | * @param float $amount |
||
365 | * @param EE_Billing_Info_Form $billing_info |
||
366 | * @param string $return_url |
||
367 | * @param string $fail_url |
||
368 | * @param string $method |
||
369 | * @param bool $by_admin |
||
370 | * @return EE_Payment |
||
371 | * @throws EE_Error |
||
372 | */ |
||
373 | public function process_payment( |
||
468 | |||
469 | /** |
||
470 | * Gets the values we want to pass onto the gateway. Normally these |
||
471 | * are just the 'pretty' values, but there may be times the data may need |
||
472 | * a little massaging. Proper subsections will become arrays of inputs |
||
473 | * |
||
474 | * @param EE_Billing_Info_Form $billing_form |
||
475 | * @return array |
||
476 | */ |
||
477 | protected function _get_billing_values_from_form($billing_form) |
||
485 | |||
486 | |||
487 | /** |
||
488 | * Handles an instant payment notification when the transaction is known (by default). |
||
489 | * |
||
490 | * @param array $req_data |
||
491 | * @param EE_Transaction $transaction |
||
492 | * @return EE_Payment |
||
493 | * @throws EE_Error |
||
494 | */ |
||
495 | public function handle_ipn($req_data, $transaction) |
||
509 | |||
510 | |||
511 | /** |
||
512 | * Saves the billing info onto the attendee of the primary registrant on this transaction, and |
||
513 | * cleans it first. |
||
514 | * |
||
515 | * @param EE_Billing_Attendee_Info_Form $billing_form |
||
516 | * @param EE_Transaction $transaction |
||
517 | * @return boolean success |
||
518 | */ |
||
519 | protected function _save_billing_info_to_attendee($billing_form, $transaction) |
||
555 | |||
556 | |||
557 | /** |
||
558 | * Gets the payment this IPN is for. Children may often want to |
||
559 | * override this to inspect the request |
||
560 | * |
||
561 | * @param EE_Transaction $transaction |
||
562 | * @param array $req_data |
||
563 | * @return EE_Payment |
||
564 | */ |
||
565 | protected function find_payment_for_ipn(EE_Transaction $transaction, $req_data = array()) |
||
569 | |||
570 | |||
571 | /** |
||
572 | * In case generic code cannot provide the payment processor with a specific payment method |
||
573 | * and transaction, it will try calling this method on each activate payment method. |
||
574 | * If the payment method is able to identify the request as being for it, it should fetch |
||
575 | * the payment its for and return it. If not, it should throw an EE_Error to indicate it cannot |
||
576 | * handle the IPN |
||
577 | * |
||
578 | * @param array $req_data |
||
579 | * @return EE_Payment only if this payment method can find the info its needs from $req_data |
||
580 | * and identifies the IPN as being for this payment method (not just fo ra payment method of this type) |
||
581 | * @throws EE_Error |
||
582 | */ |
||
583 | public function handle_unclaimed_ipn($req_data = array()) |
||
589 | |||
590 | |||
591 | /** |
||
592 | * Logic to be accomplished when the payment attempt is complete. |
||
593 | * Most payment methods don't need to do anything at this point; but some, like Mijireh, do. |
||
594 | * (Mijireh is an offsite gateway which doesn't send an IPN. So when the user returns to EE from |
||
595 | * mijireh, this method needs to be called so the Mijireh PM can ping Mijireh to know the status |
||
596 | * of the payment). Fed a transaction because it's always assumed to be the last payment that |
||
597 | * we're dealing with. Returns that last payment (if there is one) |
||
598 | * |
||
599 | * @param EE_Transaction $transaction |
||
600 | * @return EE_Payment |
||
601 | */ |
||
602 | public function finalize_payment_for($transaction) |
||
606 | |||
607 | |||
608 | /** |
||
609 | * Whether or not this payment method's gateway supports sending refund requests |
||
610 | * |
||
611 | * @return boolean |
||
612 | */ |
||
613 | public function supports_sending_refunds() |
||
621 | |||
622 | |||
623 | /** |
||
624 | * |
||
625 | * @param EE_Payment $payment |
||
626 | * @param array $refund_info |
||
627 | * @throws EE_Error |
||
628 | * @return EE_Payment |
||
629 | */ |
||
630 | public function process_refund(EE_Payment $payment, $refund_info = array()) |
||
643 | |||
644 | |||
645 | /** |
||
646 | * Returns one the class's constants onsite,offsite, or offline, depending on this |
||
647 | * payment method's gateway. |
||
648 | * |
||
649 | * @return string |
||
650 | * @throws EE_Error |
||
651 | */ |
||
652 | public function payment_occurs() |
||
672 | |||
673 | |||
674 | /** |
||
675 | * For adding any html output ab ove the payment overview. |
||
676 | * Many gateways won't want ot display anything, so this function just returns an empty string. |
||
677 | * Other gateways may want to override this, such as offline gateways. |
||
678 | * |
||
679 | * @param EE_Payment $payment |
||
680 | * @return string |
||
681 | */ |
||
682 | public function payment_overview_content(EE_Payment $payment) |
||
690 | |||
691 | |||
692 | /** |
||
693 | * @return array where keys are the help tab name, |
||
694 | * values are: array { |
||
695 | * @type string $title i18n name for the help tab |
||
696 | * @type string $filename name of the file located in ./help_tabs/ (ie, in a folder next to this file) |
||
697 | * @type array $template_args any arguments you want passed to the template file while rendering. |
||
698 | * Keys will be variable names and values with be their values. |
||
699 | */ |
||
700 | public function help_tabs_config() |
||
704 | |||
705 | |||
706 | /** |
||
707 | * The system name for this PMT (eg AIM, Paypal_Pro, Invoice... what gets put into |
||
708 | * the payment method's table's PMT_type column) |
||
709 | * |
||
710 | * @return string |
||
711 | */ |
||
712 | public function system_name() |
||
717 | |||
718 | |||
719 | /** |
||
720 | * A pretty i18n version of the PMT name. Often the same as the "pretty_name", but you can change it by overriding |
||
721 | * this method. |
||
722 | * @return string |
||
723 | */ |
||
724 | public function defaultFrontendName() |
||
728 | |||
729 | |||
730 | /** |
||
731 | * A pretty i18n version of the PMT name |
||
732 | * |
||
733 | * @return string |
||
734 | */ |
||
735 | public function pretty_name() |
||
739 | |||
740 | |||
741 | /** |
||
742 | * Gets the default absolute URL to the payment method type's button |
||
743 | * |
||
744 | * @return string |
||
745 | */ |
||
746 | public function default_button_url() |
||
750 | |||
751 | |||
752 | /** |
||
753 | * Gets the gateway used by this payment method (if any) |
||
754 | * |
||
755 | * @return EE_Gateway |
||
756 | */ |
||
757 | public function get_gateway() |
||
761 | |||
762 | |||
763 | /** |
||
764 | * @return string html for the link to a help tab |
||
765 | */ |
||
766 | public function get_help_tab_link() |
||
774 | |||
775 | |||
776 | /** |
||
777 | * Returns the name of the help tab for this PMT |
||
778 | * |
||
779 | * @return string |
||
780 | */ |
||
781 | public function get_help_tab_name() |
||
785 | |||
786 | /** |
||
787 | * The name of the wp capability that should be associated with the usage of |
||
788 | * this PMT by an admin |
||
789 | * |
||
790 | * @return string |
||
791 | */ |
||
792 | public function cap_name() |
||
796 | |||
797 | /** |
||
798 | * Called by client code to tell the gateway that if it wants to change |
||
799 | * the transaction or line items or registrations related to teh payment it already |
||
800 | * processed (we think, but possibly not) that now's the time to do it. |
||
801 | * It is expected that gateways will store any info they need for this on the PAY_details, |
||
802 | * or maybe an extra meta value |
||
803 | * |
||
804 | * @param EE_Payment $payment |
||
805 | * @return void |
||
806 | */ |
||
807 | public function update_txn_based_on_payment($payment) |
||
813 | |||
814 | /** |
||
815 | * Returns a string of HTML describing this payment method type for an admin, |
||
816 | * primarily intended for them to read before activating it. |
||
817 | * The easiest way to set this is to create a folder 'templates' alongside |
||
818 | * your EE_PMT_{System_Name} file, and in it create a file named "{system_name}_intro.template.php". |
||
819 | * Eg, if your payment method file is named "EE_PMT_Foo_Bar.pm.php", |
||
820 | * then you'd create a file named "templates" in the same folder as it, and name the file |
||
821 | * "foo_bar_intro.template.php", and its content will be returned by this method |
||
822 | * |
||
823 | * @return string |
||
824 | */ |
||
825 | public function introductory_html() |
||
832 | } |
||
833 |
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.