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 |
||
15 | class EE_Cron_Tasks extends EE_Base |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * WordPress doesn't allow duplicate crons within 10 minutes of the original, |
||
20 | * so we'll set our retry time for just over 10 minutes to avoid that |
||
21 | */ |
||
22 | const reschedule_timeout = 605; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * @var EE_Cron_Tasks |
||
27 | */ |
||
28 | private static $_instance; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * @return EE_Cron_Tasks |
||
33 | * @throws ReflectionException |
||
34 | * @throws EE_Error |
||
35 | * @throws InvalidArgumentException |
||
36 | * @throws InvalidInterfaceException |
||
37 | * @throws InvalidDataTypeException |
||
38 | */ |
||
39 | public static function instance() |
||
46 | |||
47 | |||
48 | /** |
||
49 | * @access private |
||
50 | * @throws InvalidDataTypeException |
||
51 | * @throws InvalidInterfaceException |
||
52 | * @throws InvalidArgumentException |
||
53 | * @throws EE_Error |
||
54 | * @throws ReflectionException |
||
55 | */ |
||
56 | private function __construct() |
||
112 | |||
113 | |||
114 | /** |
||
115 | * @access protected |
||
116 | * @return void |
||
117 | */ |
||
118 | public static function log_scheduled_ee_crons() |
||
149 | |||
150 | |||
151 | /** |
||
152 | * reschedule_cron_for_transactions_if_maintenance_mode |
||
153 | * if Maintenance Mode is active, this will reschedule a cron to run again in 10 minutes |
||
154 | * |
||
155 | * @param string $cron_task |
||
156 | * @param array $TXN_IDs |
||
157 | * @return bool |
||
158 | * @throws DomainException |
||
159 | */ |
||
160 | public static function reschedule_cron_for_transactions_if_maintenance_mode($cron_task, array $TXN_IDs) |
||
191 | |||
192 | |||
193 | |||
194 | |||
195 | /**************** UPDATE TRANSACTION WITH PAYMENT ****************/ |
||
196 | |||
197 | |||
198 | /** |
||
199 | * array of TXN IDs and the payment |
||
200 | * |
||
201 | * @var array |
||
202 | */ |
||
203 | protected static $_update_transactions_with_payment = array(); |
||
204 | |||
205 | |||
206 | /** |
||
207 | * schedule_update_transaction_with_payment |
||
208 | * sets a wp_schedule_single_event() for updating any TXNs that may |
||
209 | * require updating due to recently received payments |
||
210 | * |
||
211 | * @param int $timestamp |
||
212 | * @param int $TXN_ID |
||
213 | * @param int $PAY_ID |
||
214 | */ |
||
215 | public static function schedule_update_transaction_with_payment( |
||
232 | |||
233 | |||
234 | /** |
||
235 | * setup_update_for_transaction_with_payment |
||
236 | * this is the callback for the action hook: |
||
237 | * 'AHEE__EE_Cron_Tasks__update_transaction_with_payment' |
||
238 | * which is setup by EE_Cron_Tasks::schedule_update_transaction_with_payment(). |
||
239 | * The passed TXN_ID and associated payment gets added to an array, and then |
||
240 | * the EE_Cron_Tasks::update_transaction_with_payment() function is hooked into |
||
241 | * 'shutdown' which will actually handle the processing of any |
||
242 | * transactions requiring updating, because doing so now would be too early |
||
243 | * and the required resources may not be available |
||
244 | * |
||
245 | * @param int $TXN_ID |
||
246 | * @param int $PAY_ID |
||
247 | */ |
||
248 | public static function setup_update_for_transaction_with_payment($TXN_ID = 0, $PAY_ID = 0) |
||
260 | |||
261 | |||
262 | /** |
||
263 | * update_transaction_with_payment |
||
264 | * loops through the self::$_abandoned_transactions array |
||
265 | * and attempts to finalize any TXNs that have not been completed |
||
266 | * but have had their sessions expired, most likely due to a user not |
||
267 | * returning from an off-site payment gateway |
||
268 | * |
||
269 | * @throws EE_Error |
||
270 | * @throws DomainException |
||
271 | * @throws InvalidDataTypeException |
||
272 | * @throws InvalidInterfaceException |
||
273 | * @throws InvalidArgumentException |
||
274 | * @throws ReflectionException |
||
275 | * @throws RuntimeException |
||
276 | */ |
||
277 | public static function update_transaction_with_payment() |
||
317 | |||
318 | |||
319 | |||
320 | /************ END OF UPDATE TRANSACTION WITH PAYMENT ************/ |
||
321 | |||
322 | |||
323 | /***************** EXPIRED TRANSACTION CHECK *****************/ |
||
324 | |||
325 | |||
326 | /** |
||
327 | * array of TXN IDs |
||
328 | * |
||
329 | * @var array |
||
330 | */ |
||
331 | protected static $_expired_transactions = array(); |
||
332 | |||
333 | |||
334 | /** |
||
335 | * schedule_expired_transaction_check |
||
336 | * sets a wp_schedule_single_event() for following up on TXNs after their session has expired |
||
337 | * |
||
338 | * @param int $timestamp |
||
339 | * @param int $TXN_ID |
||
340 | */ |
||
341 | public static function schedule_expired_transaction_check( |
||
356 | |||
357 | |||
358 | /** |
||
359 | * expired_transaction_check |
||
360 | * this is the callback for the action hook: |
||
361 | * 'AHEE__EE_Cron_Tasks__transaction_session_expiration_check' |
||
362 | * which is utilized by wp_schedule_single_event() |
||
363 | * in \EED_Single_Page_Checkout::_initialize_transaction(). |
||
364 | * The passed TXN_ID gets added to an array, and then the |
||
365 | * process_expired_transactions() function is hooked into |
||
366 | * 'AHEE__EE_System__core_loaded_and_ready' which will actually handle the |
||
367 | * processing of any failed transactions, because doing so now would be |
||
368 | * too early and the required resources may not be available |
||
369 | * |
||
370 | * @param int $TXN_ID |
||
371 | */ |
||
372 | public static function expired_transaction_check($TXN_ID = 0) |
||
383 | |||
384 | |||
385 | /** |
||
386 | * process_expired_transactions |
||
387 | * loops through the self::$_expired_transactions array and processes any failed TXNs |
||
388 | * |
||
389 | * @throws EE_Error |
||
390 | * @throws InvalidDataTypeException |
||
391 | * @throws InvalidInterfaceException |
||
392 | * @throws InvalidArgumentException |
||
393 | * @throws ReflectionException |
||
394 | * @throws DomainException |
||
395 | * @throws RuntimeException |
||
396 | */ |
||
397 | public static function process_expired_transactions() |
||
492 | |||
493 | |||
494 | |||
495 | /************* END OF EXPIRED TRANSACTION CHECK *************/ |
||
496 | |||
497 | |||
498 | /************* START CLEAN UP BOT TRANSACTIONS **********************/ |
||
499 | |||
500 | |||
501 | /** |
||
502 | * callback for 'AHEE__EE_Cron_Tasks__clean_up_junk_transactions' |
||
503 | * which is setup during activation to run on an hourly cron |
||
504 | * |
||
505 | * @throws EE_Error |
||
506 | * @throws InvalidArgumentException |
||
507 | * @throws InvalidDataTypeException |
||
508 | * @throws InvalidInterfaceException |
||
509 | * @throws DomainException |
||
510 | */ |
||
511 | public static function clean_out_junk_transactions() |
||
520 | |||
521 | |||
522 | /** |
||
523 | * Deletes old gateway logs. After about a week we usually don't need them for debugging. But folks can filter that. |
||
524 | * |
||
525 | * @throws EE_Error |
||
526 | * @throws InvalidDataTypeException |
||
527 | * @throws InvalidInterfaceException |
||
528 | * @throws InvalidArgumentException |
||
529 | */ |
||
530 | public static function clean_out_old_gateway_logs() |
||
541 | |||
542 | |||
543 | /***************** FINALIZE ABANDONED TRANSACTIONS *****************/ |
||
544 | |||
545 | |||
546 | /** |
||
547 | * @var array |
||
548 | */ |
||
549 | protected static $_abandoned_transactions = array(); |
||
550 | |||
551 | |||
552 | /** |
||
553 | * @deprecated |
||
554 | * @param int $timestamp |
||
555 | * @param int $TXN_ID |
||
556 | */ |
||
557 | public static function schedule_finalize_abandoned_transactions_check($timestamp, $TXN_ID) |
||
561 | |||
562 | |||
563 | /** |
||
564 | * @deprecated |
||
565 | * @param int $TXN_ID |
||
566 | */ |
||
567 | public static function check_for_abandoned_transactions($TXN_ID = 0) |
||
571 | |||
572 | |||
573 | /** |
||
574 | * @deprecated |
||
575 | * @throws EE_Error |
||
576 | * @throws DomainException |
||
577 | * @throws InvalidDataTypeException |
||
578 | * @throws InvalidInterfaceException |
||
579 | * @throws InvalidArgumentException |
||
580 | * @throws ReflectionException |
||
581 | * @throws RuntimeException |
||
582 | */ |
||
583 | public static function finalize_abandoned_transactions() |
||
601 | |||
602 | |||
603 | /************* END OF FINALIZE ABANDONED TRANSACTIONS *************/ |
||
604 | } |
||
605 |