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_Cron_Tasks 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_Cron_Tasks, and based on these observations, apply Extract Interface, too.
1 | <?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
||
14 | class EE_Cron_Tasks extends EE_Base |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * WordPress doesn't allow duplicate crons within 10 minutes of the original, |
||
19 | * so we'll set our retry time for just over 10 minutes to avoid that |
||
20 | */ |
||
21 | const reschedule_timeout = 605; |
||
22 | |||
23 | |||
24 | /** |
||
25 | * @var EE_Cron_Tasks |
||
26 | */ |
||
27 | private static $_instance; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * @return EE_Cron_Tasks |
||
32 | * @throws \ReflectionException |
||
33 | * @throws \EE_Error |
||
34 | * @throws \InvalidArgumentException |
||
35 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
36 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
37 | */ |
||
38 | public static function instance() |
||
45 | |||
46 | |||
47 | /** |
||
48 | * @access private |
||
49 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
50 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
51 | * @throws \InvalidArgumentException |
||
52 | * @throws \EE_Error |
||
53 | * @throws \ReflectionException |
||
54 | */ |
||
55 | private function __construct() |
||
120 | |||
121 | |||
122 | /** |
||
123 | * @access protected |
||
124 | * @return void |
||
125 | */ |
||
126 | public static function log_scheduled_ee_crons() |
||
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * reschedule_cron_for_transactions_if_maintenance_mode |
||
161 | * if Maintenance Mode is active, this will reschedule a cron to run again in 10 minutes |
||
162 | * |
||
163 | * @param string $cron_task |
||
164 | * @param array $TXN_IDs |
||
165 | * @return bool |
||
166 | * @throws \DomainException |
||
167 | */ |
||
168 | public static function reschedule_cron_for_transactions_if_maintenance_mode($cron_task, array $TXN_IDs) |
||
199 | |||
200 | |||
201 | |||
202 | |||
203 | /**************** UPDATE TRANSACTION WITH PAYMENT ****************/ |
||
204 | /** |
||
205 | * array of TXN IDs and the payment |
||
206 | * |
||
207 | * @var array |
||
208 | */ |
||
209 | protected static $_update_transactions_with_payment = array(); |
||
210 | |||
211 | |||
212 | /** |
||
213 | * schedule_update_transaction_with_payment |
||
214 | * sets a wp_schedule_single_event() for updating any TXNs that may |
||
215 | * require updating due to recently received payments |
||
216 | * |
||
217 | * @param int $timestamp |
||
218 | * @param int $TXN_ID |
||
219 | * @param int $PAY_ID |
||
220 | */ |
||
221 | View Code Duplication | public static function schedule_update_transaction_with_payment( |
|
238 | |||
239 | |||
240 | /** |
||
241 | * setup_update_for_transaction_with_payment |
||
242 | * this is the callback for the action hook: |
||
243 | * 'AHEE__EE_Cron_Tasks__update_transaction_with_payment' |
||
244 | * which is setup by EE_Cron_Tasks::schedule_update_transaction_with_payment(). |
||
245 | * The passed TXN_ID and associated payment gets added to an array, and then |
||
246 | * the EE_Cron_Tasks::update_transaction_with_payment() function is hooked into |
||
247 | * 'shutdown' which will actually handle the processing of any |
||
248 | * transactions requiring updating, because doing so now would be too early |
||
249 | * and the required resources may not be available |
||
250 | * |
||
251 | * @param int $TXN_ID |
||
252 | * @param int $PAY_ID |
||
253 | */ |
||
254 | View Code Duplication | public static function setup_update_for_transaction_with_payment($TXN_ID = 0, $PAY_ID = 0) |
|
266 | |||
267 | |||
268 | /** |
||
269 | * update_transaction_with_payment |
||
270 | * loops through the self::$_abandoned_transactions array |
||
271 | * and attempts to finalize any TXNs that have not been completed |
||
272 | * but have had their sessions expired, most likely due to a user not |
||
273 | * returning from an off-site payment gateway |
||
274 | * |
||
275 | * @throws \EE_Error |
||
276 | * @throws \DomainException |
||
277 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
278 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
279 | * @throws \InvalidArgumentException |
||
280 | * @throws \ReflectionException |
||
281 | */ |
||
282 | public static function update_transaction_with_payment() |
||
323 | |||
324 | |||
325 | |||
326 | /************ END OF UPDATE TRANSACTION WITH PAYMENT ************/ |
||
327 | /***************** FINALIZE ABANDONED TRANSACTIONS *****************/ |
||
328 | /** |
||
329 | * array of TXN IDs |
||
330 | * |
||
331 | * @var array |
||
332 | */ |
||
333 | protected static $_abandoned_transactions = array(); |
||
334 | |||
335 | |||
336 | /** |
||
337 | * schedule_finalize_abandoned_transactions_check |
||
338 | * sets a wp_schedule_single_event() for finalizing any TXNs that may |
||
339 | * have been abandoned during the registration process |
||
340 | * |
||
341 | * @param int $timestamp |
||
342 | * @param int $TXN_ID |
||
343 | */ |
||
344 | View Code Duplication | public static function schedule_finalize_abandoned_transactions_check( |
|
360 | |||
361 | |||
362 | /** |
||
363 | * check_for_abandoned_transactions |
||
364 | * this is the callback for the action hook: |
||
365 | * 'AHEE__EE_Cron_Tasks__espresso_finalize_abandoned_transactions' |
||
366 | * which is utilized by wp_schedule_single_event() |
||
367 | * in EE_SPCO_Reg_Step_Payment_Options::_post_payment_processing(). |
||
368 | * The passed TXN_ID gets added to an array, and then the |
||
369 | * espresso_finalize_abandoned_transactions() function is hooked into |
||
370 | * 'AHEE__EE_System__core_loaded_and_ready' which will actually handle the |
||
371 | * processing of any abandoned transactions, because doing so now would be |
||
372 | * too early and the required resources may not be available |
||
373 | * |
||
374 | * @param int $TXN_ID |
||
375 | */ |
||
376 | View Code Duplication | public static function check_for_abandoned_transactions($TXN_ID = 0) |
|
388 | |||
389 | |||
390 | /** |
||
391 | * finalize_abandoned_transactions |
||
392 | * loops through the self::$_abandoned_transactions array |
||
393 | * and attempts to finalize any TXNs that have not been completed |
||
394 | * but have had their sessions expired, most likely due to a user not |
||
395 | * returning from an off-site payment gateway |
||
396 | * |
||
397 | * @throws \EE_Error |
||
398 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
399 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
400 | * @throws \InvalidArgumentException |
||
401 | * @throws \ReflectionException |
||
402 | * @throws \DomainException |
||
403 | */ |
||
404 | public static function finalize_abandoned_transactions() |
||
451 | |||
452 | |||
453 | |||
454 | /************* END OF FINALIZE ABANDONED TRANSACTIONS *************/ |
||
455 | /***************** EXPIRED TRANSACTION CHECK *****************/ |
||
456 | /** |
||
457 | * array of TXN IDs |
||
458 | * |
||
459 | * @var array |
||
460 | */ |
||
461 | protected static $_expired_transactions = array(); |
||
462 | |||
463 | |||
464 | |||
465 | /** |
||
466 | * schedule_expired_transaction_check |
||
467 | * sets a wp_schedule_single_event() for following up on TXNs after their session has expired |
||
468 | * |
||
469 | * @param int $timestamp |
||
470 | * @param int $TXN_ID |
||
471 | */ |
||
472 | public static function schedule_expired_transaction_check( |
||
487 | |||
488 | |||
489 | |||
490 | /** |
||
491 | * expired_transaction_check |
||
492 | * this is the callback for the action hook: |
||
493 | * 'AHEE__EE_Cron_Tasks__transaction_session_expiration_check' |
||
494 | * which is utilized by wp_schedule_single_event() |
||
495 | * in \EED_Single_Page_Checkout::_initialize_transaction(). |
||
496 | * The passed TXN_ID gets added to an array, and then the |
||
497 | * process_expired_transactions() function is hooked into |
||
498 | * 'AHEE__EE_System__core_loaded_and_ready' which will actually handle the |
||
499 | * processing of any failed transactions, because doing so now would be |
||
500 | * too early and the required resources may not be available |
||
501 | * |
||
502 | * @param int $TXN_ID |
||
503 | */ |
||
504 | public static function expired_transaction_check($TXN_ID = 0) |
||
515 | |||
516 | |||
517 | |||
518 | /** |
||
519 | * process_expired_transactions |
||
520 | * loops through the self::$_expired_transactions array and processes any failed TXNs |
||
521 | * |
||
522 | * @throws \EE_Error |
||
523 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
524 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
525 | * @throws \InvalidArgumentException |
||
526 | * @throws \ReflectionException |
||
527 | * @throws \DomainException |
||
528 | */ |
||
529 | public static function process_expired_transactions() |
||
604 | |||
605 | |||
606 | |||
607 | /************* END OF EXPIRED TRANSACTION CHECK *************/ |
||
608 | /************* START CLEAN UP BOT TRANSACTIONS **********************/ |
||
609 | |||
610 | |||
611 | |||
612 | /** |
||
613 | * when a transaction is initially made, schedule this check. |
||
614 | * if it has NO REG data by the time it has expired, forget about it |
||
615 | * |
||
616 | * @throws EE_Error |
||
617 | * @throws InvalidArgumentException |
||
618 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
619 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
620 | */ |
||
621 | public static function clean_out_junk_transactions() |
||
629 | |||
630 | |||
631 | |||
632 | /** |
||
633 | * Deletes old gateway logs. After about a week we usually don't need them for debugging. But folks can filter that. |
||
634 | * |
||
635 | * @throws \EE_Error |
||
636 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
637 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
638 | * @throws \InvalidArgumentException |
||
639 | */ |
||
640 | public static function clean_out_old_gateway_logs() |
||
650 | |||
651 | |||
652 | } |
||
653 | // End of file EE_Cron_Tasks.core.php |
||
655 |