ActionMigrator.php
1 year ago
ActionScheduler_DBStoreMigrator.php
1 year ago
BatchFetcher.php
1 year ago
Config.php
1 year ago
Controller.php
1 year ago
DryRun_ActionMigrator.php
1 year ago
DryRun_LogMigrator.php
1 year ago
LogMigrator.php
1 year ago
Runner.php
1 year ago
Scheduler.php
1 year ago
ActionMigrator.php
127 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Action_Scheduler\Migration; |
| 5 | |
| 6 | /** |
| 7 | * Class ActionMigrator |
| 8 | * |
| 9 | * @package Action_Scheduler\Migration |
| 10 | * |
| 11 | * @since 3.0.0 |
| 12 | * |
| 13 | * @codeCoverageIgnore |
| 14 | */ |
| 15 | class ActionMigrator { |
| 16 | /** |
| 17 | * Source store instance. |
| 18 | * |
| 19 | * @var ActionScheduler_Store |
| 20 | */ |
| 21 | private $source; |
| 22 | |
| 23 | /** |
| 24 | * Destination store instance. |
| 25 | * |
| 26 | * @var ActionScheduler_Store |
| 27 | */ |
| 28 | private $destination; |
| 29 | |
| 30 | /** |
| 31 | * LogMigrator instance. |
| 32 | * |
| 33 | * @var LogMigrator |
| 34 | */ |
| 35 | private $log_migrator; |
| 36 | |
| 37 | /** |
| 38 | * ActionMigrator constructor. |
| 39 | * |
| 40 | * @param \ActionScheduler_Store $source_store Source store object. |
| 41 | * @param \ActionScheduler_Store $destination_store Destination store object. |
| 42 | * @param LogMigrator $log_migrator Log migrator object. |
| 43 | */ |
| 44 | public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) { |
| 45 | $this->source = $source_store; |
| 46 | $this->destination = $destination_store; |
| 47 | $this->log_migrator = $log_migrator; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Migrate an action. |
| 52 | * |
| 53 | * @param int $source_action_id Action ID. |
| 54 | * |
| 55 | * @return int 0|new action ID |
| 56 | * @throws \RuntimeException When unable to delete action from the source store. |
| 57 | */ |
| 58 | public function migrate( $source_action_id ) { |
| 59 | try { |
| 60 | $action = $this->source->fetch_action( $source_action_id ); |
| 61 | $status = $this->source->get_status( $source_action_id ); |
| 62 | } catch ( \Exception $e ) { |
| 63 | $action = null; |
| 64 | $status = ''; |
| 65 | } |
| 66 | |
| 67 | if ( is_null( $action ) || empty( $status ) || ! $action->get_schedule()->get_date() ) { |
| 68 | // null action or empty status means the fetch operation failed or the action didn't exist. |
| 69 | // null schedule means it's missing vital data. |
| 70 | // delete it and move on. |
| 71 | try { |
| 72 | $this->source->delete_action( $source_action_id ); |
| 73 | } catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 74 | // nothing to do, it didn't exist in the first place. |
| 75 | } |
| 76 | do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | try { |
| 82 | |
| 83 | // Make sure the last attempt date is set correctly for completed and failed actions. |
| 84 | $last_attempt_date = ( \ActionScheduler_Store::STATUS_PENDING !== $status ) ? $this->source->get_date( $source_action_id ) : null; |
| 85 | |
| 86 | $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date ); |
| 87 | } catch ( \Exception $e ) { |
| 88 | do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 89 | |
| 90 | return 0; // could not save the action in the new store. |
| 91 | } |
| 92 | |
| 93 | try { |
| 94 | switch ( $status ) { |
| 95 | case \ActionScheduler_Store::STATUS_FAILED: |
| 96 | $this->destination->mark_failure( $destination_action_id ); |
| 97 | break; |
| 98 | case \ActionScheduler_Store::STATUS_CANCELED: |
| 99 | $this->destination->cancel_action( $destination_action_id ); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | $this->log_migrator->migrate( $source_action_id, $destination_action_id ); |
| 104 | $this->source->delete_action( $source_action_id ); |
| 105 | |
| 106 | $test_action = $this->source->fetch_action( $source_action_id ); |
| 107 | if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) { |
| 108 | // translators: %s is an action ID. |
| 109 | throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'woocommerce' ), $source_action_id ) ); |
| 110 | } |
| 111 | do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 112 | |
| 113 | return $destination_action_id; |
| 114 | } catch ( \Exception $e ) { |
| 115 | // could not delete from the old store. |
| 116 | $this->source->mark_migrated( $source_action_id ); |
| 117 | |
| 118 | // phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 119 | do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
| 120 | do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
| 121 | // phpcs:enable |
| 122 | |
| 123 | return $destination_action_id; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 |