| Conditions | 12 |
| Paths | 156 |
| Total Lines | 62 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 45 | public function migrate( $source_action_id ) { |
||
| 46 | try { |
||
| 47 | $action = $this->source->fetch_action( $source_action_id ); |
||
| 48 | $status = $this->source->get_status( $source_action_id ); |
||
| 49 | } catch ( \Exception $e ) { |
||
| 50 | $action = null; |
||
| 51 | $status = ''; |
||
| 52 | } |
||
| 53 | |||
| 54 | if ( is_null( $action ) || empty( $status ) || ! $action->get_schedule()->get_date() ) { |
||
| 55 | // null action or empty status means the fetch operation failed or the action didn't exist |
||
| 56 | // null schedule means it's missing vital data |
||
| 57 | // delete it and move on |
||
| 58 | try { |
||
| 59 | $this->source->delete_action( $source_action_id ); |
||
| 60 | } catch ( \Exception $e ) { |
||
| 61 | // nothing to do, it didn't exist in the first place |
||
| 62 | } |
||
| 63 | do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); |
||
| 64 | |||
| 65 | return 0; |
||
| 66 | } |
||
| 67 | |||
| 68 | try { |
||
| 69 | |||
| 70 | // Make sure the last attempt date is set correctly for completed and failed actions |
||
| 71 | $last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null; |
||
| 72 | |||
| 73 | $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date ); |
||
| 74 | } catch ( \Exception $e ) { |
||
| 75 | do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); |
||
| 76 | |||
| 77 | return 0; // could not save the action in the new store |
||
| 78 | } |
||
| 79 | |||
| 80 | try { |
||
| 81 | switch ( $status ) { |
||
| 82 | case \ActionScheduler_Store::STATUS_FAILED : |
||
| 83 | $this->destination->mark_failure( $destination_action_id ); |
||
| 84 | break; |
||
| 85 | case \ActionScheduler_Store::STATUS_CANCELED : |
||
| 86 | $this->destination->cancel_action( $destination_action_id ); |
||
| 87 | break; |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->log_migrator->migrate( $source_action_id, $destination_action_id ); |
||
| 91 | $this->source->delete_action( $source_action_id ); |
||
| 92 | |||
| 93 | $test_action = $this->source->fetch_action( $source_action_id ); |
||
| 94 | if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) { |
||
| 95 | throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) ); |
||
| 96 | } |
||
| 97 | do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
||
| 98 | |||
| 99 | return $destination_action_id; |
||
| 100 | } catch ( \Exception $e ) { |
||
| 101 | // could not delete from the old store |
||
| 102 | $this->source->mark_migrated( $source_action_id ); |
||
| 103 | do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
||
| 104 | do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
||
| 105 | |||
| 106 | return $destination_action_id; |
||
| 107 | } |
||
| 110 |