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_Payment 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_Payment, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
||
12 | class EE_Payment extends EE_Base_Class implements EEI_Payment { |
||
13 | |||
14 | /** |
||
15 | * @param array $props_n_values incoming values |
||
16 | * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
||
17 | * used.) |
||
18 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
19 | * date_format and the second value is the time format |
||
20 | * @return EE_Payment |
||
21 | * @throws \EE_Error |
||
22 | */ |
||
23 | public static function new_instance( $props_n_values = array(), $timezone = null, $date_formats = array() ) { |
||
24 | $has_object = parent::_check_for_object( $props_n_values, __CLASS__, $timezone, $date_formats ); |
||
25 | return $has_object ? $has_object : new self( $props_n_values, false, $timezone, $date_formats ); |
||
26 | } |
||
27 | |||
28 | |||
29 | |||
30 | /** |
||
31 | * @param array $props_n_values incoming values from the database |
||
32 | * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
||
33 | * the website will be used. |
||
34 | * @return EE_Payment |
||
35 | * @throws \EE_Error |
||
36 | */ |
||
37 | public static function new_instance_from_db( $props_n_values = array(), $timezone = null ) { |
||
38 | return new self( $props_n_values, true, $timezone ); |
||
39 | } |
||
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | * Set Transaction ID |
||
45 | * |
||
46 | * @access public |
||
47 | * @param int $TXN_ID |
||
48 | * @throws \EE_Error |
||
49 | */ |
||
50 | public function set_transaction_id( $TXN_ID = 0 ) { |
||
51 | $this->set( 'TXN_ID', $TXN_ID ); |
||
52 | } |
||
53 | |||
54 | |||
55 | |||
56 | /** |
||
57 | * Gets the transaction related to this payment |
||
58 | * |
||
59 | * @return EE_Transaction |
||
60 | * @throws \EE_Error |
||
61 | */ |
||
62 | public function transaction() { |
||
63 | return $this->get_first_related( 'Transaction' ); |
||
64 | } |
||
65 | |||
66 | |||
67 | |||
68 | /** |
||
69 | * Set Status |
||
70 | * |
||
71 | * @access public |
||
72 | * @param string $STS_ID |
||
73 | * @throws \EE_Error |
||
74 | */ |
||
75 | public function set_status( $STS_ID = '' ) { |
||
76 | $this->set( 'STS_ID', $STS_ID ); |
||
77 | } |
||
78 | |||
79 | |||
80 | |||
81 | /** |
||
82 | * Set Payment Timestamp |
||
83 | * |
||
84 | * @access public |
||
85 | * @param int $timestamp |
||
86 | * @throws \EE_Error |
||
87 | */ |
||
88 | public function set_timestamp( $timestamp = 0 ) { |
||
89 | $this->set( 'PAY_timestamp', $timestamp ); |
||
90 | } |
||
91 | |||
92 | |||
93 | |||
94 | /** |
||
95 | * Set Payment Method |
||
96 | * |
||
97 | * @access public |
||
98 | * @param string $PAY_source |
||
99 | * @throws \EE_Error |
||
100 | */ |
||
101 | public function set_source( $PAY_source = '' ) { |
||
102 | $this->set( 'PAY_source', $PAY_source ); |
||
103 | } |
||
104 | |||
105 | |||
106 | |||
107 | /** |
||
108 | * Set Payment Amount |
||
109 | * |
||
110 | * @access public |
||
111 | * @param float $amount |
||
112 | * @throws \EE_Error |
||
113 | */ |
||
114 | public function set_amount( $amount = 0.00 ) { |
||
117 | |||
118 | |||
119 | |||
120 | /** |
||
121 | * Set Payment Gateway Response |
||
122 | * |
||
123 | * @access public |
||
124 | * @param string $gateway_response |
||
125 | * @throws \EE_Error |
||
126 | */ |
||
127 | public function set_gateway_response( $gateway_response = '' ) { |
||
130 | |||
131 | |||
132 | |||
133 | /** |
||
134 | * Returns the name of the payment method used on this payment (previously known merely as 'gateway') |
||
135 | * but since 4.6.0, payment methods are models and the payment keeps a foreign key to the payment method |
||
136 | * used on it |
||
137 | * |
||
138 | * @deprecated |
||
139 | * @return string |
||
140 | * @throws \EE_Error |
||
141 | */ |
||
142 | public function gateway() { |
||
153 | |||
154 | |||
155 | |||
156 | /** |
||
157 | * Set Gateway Transaction ID |
||
158 | * |
||
159 | * @access public |
||
160 | * @param string $txn_id_chq_nmbr |
||
161 | * @throws \EE_Error |
||
162 | */ |
||
163 | public function set_txn_id_chq_nmbr( $txn_id_chq_nmbr = '' ) { |
||
166 | |||
167 | |||
168 | |||
169 | /** |
||
170 | * Set Purchase Order Number |
||
171 | * |
||
172 | * @access public |
||
173 | * @param string $po_number |
||
174 | * @throws \EE_Error |
||
175 | */ |
||
176 | public function set_po_number( $po_number = '' ) { |
||
179 | |||
180 | |||
181 | |||
182 | /** |
||
183 | * Set Extra Accounting Field |
||
184 | * |
||
185 | * @access public |
||
186 | * @param string $extra_accntng |
||
187 | * @throws \EE_Error |
||
188 | */ |
||
189 | public function set_extra_accntng( $extra_accntng = '' ) { |
||
192 | |||
193 | |||
194 | |||
195 | /** |
||
196 | * Set Payment made via admin flag |
||
197 | * |
||
198 | * @access public |
||
199 | * @param bool $via_admin |
||
200 | * @throws \EE_Error |
||
201 | */ |
||
202 | public function set_payment_made_via_admin( $via_admin = false ) { |
||
209 | |||
210 | |||
211 | |||
212 | /** |
||
213 | * Set Payment Details |
||
214 | * |
||
215 | * @access public |
||
216 | * @param string|array $details |
||
217 | * @throws \EE_Error |
||
218 | */ |
||
219 | public function set_details( $details = '' ) { |
||
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * Sets redirect_url |
||
232 | * |
||
233 | * @param string $redirect_url |
||
234 | * @throws \EE_Error |
||
235 | */ |
||
236 | public function set_redirect_url( $redirect_url ) { |
||
239 | |||
240 | |||
241 | |||
242 | /** |
||
243 | * Sets redirect_args |
||
244 | * |
||
245 | * @param array $redirect_args |
||
246 | * @throws \EE_Error |
||
247 | */ |
||
248 | public function set_redirect_args( $redirect_args ) { |
||
251 | |||
252 | |||
253 | |||
254 | /** |
||
255 | * get Payment Transaction ID |
||
256 | * |
||
257 | * @access public |
||
258 | * @throws \EE_Error |
||
259 | */ |
||
260 | public function TXN_ID() { |
||
263 | |||
264 | |||
265 | |||
266 | /** |
||
267 | * get Payment Status |
||
268 | * |
||
269 | * @access public |
||
270 | * @throws \EE_Error |
||
271 | */ |
||
272 | public function status() { |
||
275 | |||
276 | |||
277 | |||
278 | /** |
||
279 | * get Payment Status |
||
280 | * |
||
281 | * @access public |
||
282 | * @throws \EE_Error |
||
283 | */ |
||
284 | public function STS_ID() { |
||
287 | |||
288 | |||
289 | |||
290 | /** |
||
291 | * get Payment Timestamp |
||
292 | * |
||
293 | * @access public |
||
294 | * @param string $dt_frmt |
||
295 | * @param string $tm_frmt |
||
296 | * @return string |
||
297 | * @throws \EE_Error |
||
298 | */ |
||
299 | public function timestamp( $dt_frmt = '', $tm_frmt = '' ) { |
||
302 | |||
303 | |||
304 | |||
305 | /** |
||
306 | * get Payment Source |
||
307 | * |
||
308 | * @access public |
||
309 | * @throws \EE_Error |
||
310 | */ |
||
311 | public function source() { |
||
314 | |||
315 | |||
316 | |||
317 | /** |
||
318 | * get Payment Amount |
||
319 | * |
||
320 | * @access public |
||
321 | * @return float |
||
322 | * @throws \EE_Error |
||
323 | */ |
||
324 | public function amount() { |
||
327 | |||
328 | |||
329 | |||
330 | /** |
||
331 | * @return mixed |
||
332 | * @throws \EE_Error |
||
333 | */ |
||
334 | public function amount_no_code() { |
||
337 | |||
338 | |||
339 | |||
340 | /** |
||
341 | * get Payment Gateway Response |
||
342 | * |
||
343 | * @access public |
||
344 | * @throws \EE_Error |
||
345 | */ |
||
346 | public function gateway_response() { |
||
349 | |||
350 | |||
351 | |||
352 | /** |
||
353 | * get Payment Gateway Transaction ID |
||
354 | * |
||
355 | * @access public |
||
356 | * @throws \EE_Error |
||
357 | */ |
||
358 | public function txn_id_chq_nmbr() { |
||
361 | |||
362 | |||
363 | |||
364 | /** |
||
365 | * get Purchase Order Number |
||
366 | * |
||
367 | * @access public |
||
368 | * @throws \EE_Error |
||
369 | */ |
||
370 | public function po_number() { |
||
373 | |||
374 | |||
375 | |||
376 | /** |
||
377 | * get Extra Accounting Field |
||
378 | * |
||
379 | * @access public |
||
380 | * @throws \EE_Error |
||
381 | */ |
||
382 | public function extra_accntng() { |
||
385 | |||
386 | |||
387 | |||
388 | /** |
||
389 | * get Payment made via admin source |
||
390 | * |
||
391 | * @access public |
||
392 | * @throws \EE_Error |
||
393 | */ |
||
394 | public function payment_made_via_admin() { |
||
397 | |||
398 | |||
399 | |||
400 | /** |
||
401 | * get Payment Details |
||
402 | * |
||
403 | * @access public |
||
404 | * @throws \EE_Error |
||
405 | */ |
||
406 | public function details() { |
||
409 | |||
410 | |||
411 | |||
412 | /** |
||
413 | * Gets redirect_url |
||
414 | * |
||
415 | * @return string |
||
416 | * @throws \EE_Error |
||
417 | */ |
||
418 | public function redirect_url() { |
||
421 | |||
422 | |||
423 | |||
424 | /** |
||
425 | * Gets redirect_args |
||
426 | * |
||
427 | * @return array |
||
428 | * @throws \EE_Error |
||
429 | */ |
||
430 | public function redirect_args() { |
||
433 | |||
434 | |||
435 | |||
436 | /** |
||
437 | * echoes $this->pretty_status() |
||
438 | * |
||
439 | * @param bool $show_icons |
||
440 | * @return void |
||
441 | * @throws \EE_Error |
||
442 | */ |
||
443 | public function e_pretty_status( $show_icons = false ) { |
||
446 | |||
447 | |||
448 | |||
449 | /** |
||
450 | * returns a pretty version of the status, good for displaying to users |
||
451 | * |
||
452 | * @param bool $show_icons |
||
453 | * @return string |
||
454 | * @throws \EE_Error |
||
455 | */ |
||
456 | public function pretty_status( $show_icons = false ) { |
||
487 | |||
488 | |||
489 | |||
490 | /** |
||
491 | * For determining the status of the payment |
||
492 | * |
||
493 | * @return boolean whether the payment is approved or not |
||
494 | * @throws \EE_Error |
||
495 | */ |
||
496 | public function is_approved() { |
||
499 | |||
500 | |||
501 | |||
502 | /** |
||
503 | * Generally determines if the status of this payment equals |
||
504 | * the $STS_ID string |
||
505 | * |
||
506 | * @param string $STS_ID an ID from the esp_status table/ |
||
507 | * one of the status_id_* on the EEM_Payment model |
||
508 | * @return boolean whether the status of this payment equals the status id |
||
509 | * @throws \EE_Error |
||
510 | */ |
||
511 | protected function status_is( $STS_ID ) { |
||
514 | |||
515 | |||
516 | |||
517 | /** |
||
518 | * For determining the status of the payment |
||
519 | * |
||
520 | * @return boolean whether the payment is pending or not |
||
521 | * @throws \EE_Error |
||
522 | */ |
||
523 | public function is_pending() { |
||
526 | |||
527 | |||
528 | |||
529 | /** |
||
530 | * For determining the status of the payment |
||
531 | * |
||
532 | * @return boolean |
||
533 | * @throws \EE_Error |
||
534 | */ |
||
535 | public function is_cancelled() { |
||
538 | |||
539 | |||
540 | |||
541 | /** |
||
542 | * For determining the status of the payment |
||
543 | * |
||
544 | * @return boolean |
||
545 | * @throws \EE_Error |
||
546 | */ |
||
547 | public function is_declined() { |
||
550 | |||
551 | |||
552 | |||
553 | /** |
||
554 | * For determining the status of the payment |
||
555 | * |
||
556 | * @return boolean |
||
557 | * @throws \EE_Error |
||
558 | */ |
||
559 | public function is_failed() { |
||
562 | |||
563 | |||
564 | |||
565 | /** |
||
566 | * For determining if the payment is actually a refund ( ie: has a negative value ) |
||
567 | * |
||
568 | * @return boolean |
||
569 | * @throws \EE_Error |
||
570 | */ |
||
571 | public function is_a_refund() { |
||
574 | |||
575 | |||
576 | |||
577 | /** |
||
578 | * Get the status object of this object |
||
579 | * |
||
580 | * @return EE_Status |
||
581 | * @throws \EE_Error |
||
582 | */ |
||
583 | public function status_obj() { |
||
586 | |||
587 | |||
588 | |||
589 | /** |
||
590 | * Gets all the extra meta info on this payment |
||
591 | * |
||
592 | * @param array $query_params like EEM_Base::get_all |
||
593 | * @return EE_Extra_Meta |
||
594 | * @throws \EE_Error |
||
595 | */ |
||
596 | public function extra_meta( $query_params = array() ) { |
||
599 | |||
600 | |||
601 | |||
602 | /** |
||
603 | * Gets the last-used payment method on this transaction |
||
604 | * (we COULD just use the last-made payment, but some payment methods, namely |
||
605 | * offline ones, dont' create payments) |
||
606 | * |
||
607 | * @return EE_Payment_Method |
||
608 | * @throws \EE_Error |
||
609 | */ |
||
610 | public function payment_method() { |
||
613 | |||
614 | |||
615 | |||
616 | /** |
||
617 | * Gets the HTML for redirecting the user to an offsite gateway |
||
618 | * You can pass it special content to put inside the form, or use |
||
619 | * the default inner content (or possibly generate this all yourself using |
||
620 | * redirect_url() and redirect_args() or redirect_args_as_inputs()). |
||
621 | * Creates a POST request by default, but if no redirect args are specified, creates a GET request instead |
||
622 | * (and any querystring variables in the redirect_url are converted into html inputs |
||
623 | * so browsers submit them properly) |
||
624 | * |
||
625 | * @param string $inside_form_html |
||
626 | * @return string html |
||
627 | * @throws \EE_Error |
||
628 | */ |
||
629 | public function redirect_form( $inside_form_html = null ) { |
||
677 | |||
678 | |||
679 | |||
680 | /** |
||
681 | * Changes all the name-value pairs of the redirect args into html inputs |
||
682 | * and returns the html as a string |
||
683 | * |
||
684 | * @return string |
||
685 | * @throws \EE_Error |
||
686 | */ |
||
687 | public function redirect_args_as_inputs() { |
||
690 | |||
691 | |||
692 | |||
693 | /** |
||
694 | * Converts a 1d array of key-value pairs into html hidden inputs |
||
695 | * and returns the string of html |
||
696 | * |
||
697 | * @param array $args key-value pairs |
||
698 | * @return string |
||
699 | */ |
||
700 | protected function _args_as_inputs( $args ) { |
||
714 | |||
715 | |||
716 | |||
717 | /** |
||
718 | * Returns the currency of the payment. |
||
719 | * (At the time of writing, this will always be the currency in the configuration; |
||
720 | * however in the future it is anticipated that this will be stored on the payment |
||
721 | * object itself) |
||
722 | * |
||
723 | * @return string for the currency code |
||
724 | */ |
||
725 | public function currency_code() { |
||
728 | |||
729 | |||
730 | |||
731 | /** |
||
732 | * apply wp_strip_all_tags to all elements within an array |
||
733 | * |
||
734 | * @access private |
||
735 | * @param mixed $item |
||
736 | */ |
||
737 | private function _strip_all_tags_within_array( &$item ) { |
||
747 | |||
748 | |||
749 | |||
750 | /** |
||
751 | * Returns TRUE is this payment was set to approved during this request (or |
||
752 | * is approved and was created during this request). False otherwise. |
||
753 | * |
||
754 | * @return boolean |
||
755 | * @throws \EE_Error |
||
756 | */ |
||
757 | public function just_approved() { |
||
773 | |||
774 | |||
775 | |||
776 | /** |
||
777 | * Overrides parents' get_pretty() function just for legacy reasons |
||
778 | * (to allow ticket https://events.codebasehq.com/projects/event-espresso/tickets/7420) |
||
779 | * |
||
780 | * @param string $field_name |
||
781 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
782 | * (in cases where the same property may be used for different outputs |
||
783 | * - i.e. datetime, money etc.) |
||
784 | * @return mixed |
||
785 | * @throws \EE_Error |
||
786 | */ |
||
787 | public function get_pretty( $field_name, $extra_cache_ref = null ) { |
||
793 | |||
794 | |||
795 | |||
796 | /** |
||
797 | * Gets details regarding which registrations this payment was applied to |
||
798 | * |
||
799 | * @param array $query_params like EEM_Base::get_all |
||
800 | * @return EE_Registration_Payment[] |
||
801 | * @throws \EE_Error |
||
802 | */ |
||
803 | public function registration_payments( $query_params = array() ) { |
||
806 | |||
807 | |||
808 | |||
809 | /** |
||
810 | * Gets the first event for this payment (it's possible that it could be for multiple) |
||
811 | * |
||
812 | * @return EE_Event|null |
||
813 | */ |
||
814 | public function get_first_event() |
||
825 | |||
826 | |||
827 | |||
828 | /** |
||
829 | * Gets the name of the first event for which is being paid |
||
830 | * |
||
831 | * @return string |
||
832 | */ |
||
833 | public function get_first_event_name() |
||
838 | |||
839 | |||
840 | |||
841 | /** |
||
842 | * Returns the payment's transaction's primary registration |
||
843 | * @return EE_Registration|null |
||
844 | */ |
||
845 | public function get_primary_registration() |
||
852 | |||
853 | |||
854 | |||
855 | /** |
||
856 | * Gets the payment's transaction's primary registration's attendee, or null |
||
857 | * @return EE_Attendee|null |
||
858 | */ |
||
859 | public function get_primary_attendee() |
||
867 | } |
||
868 | /* End of file EE_Payment.class.php */ |
||
870 |