ActionMigrator.php
6 years ago
ActionScheduler_DBStoreMigrator.php
6 years ago
BatchFetcher.php
6 years ago
Config.php
6 years ago
Controller.php
4 years ago
DryRun_ActionMigrator.php
6 years ago
DryRun_LogMigrator.php
6 years ago
LogMigrator.php
6 years ago
Runner.php
6 years ago
Scheduler.php
6 years ago
Runner.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Action_Scheduler\Migration; |
| 5 | |
| 6 | /** |
| 7 | * Class Runner |
| 8 | * |
| 9 | * @package Action_Scheduler\Migration |
| 10 | * |
| 11 | * @since 3.0.0 |
| 12 | * |
| 13 | * @codeCoverageIgnore |
| 14 | */ |
| 15 | class Runner { |
| 16 | /** @var ActionScheduler_Store */ |
| 17 | private $source_store; |
| 18 | |
| 19 | /** @var ActionScheduler_Store */ |
| 20 | private $destination_store; |
| 21 | |
| 22 | /** @var ActionScheduler_Logger */ |
| 23 | private $source_logger; |
| 24 | |
| 25 | /** @var ActionScheduler_Logger */ |
| 26 | private $destination_logger; |
| 27 | |
| 28 | /** @var BatchFetcher */ |
| 29 | private $batch_fetcher; |
| 30 | |
| 31 | /** @var ActionMigrator */ |
| 32 | private $action_migrator; |
| 33 | |
| 34 | /** @var LogMigrator */ |
| 35 | private $log_migrator; |
| 36 | |
| 37 | /** @var ProgressBar */ |
| 38 | private $progress_bar; |
| 39 | |
| 40 | /** |
| 41 | * Runner constructor. |
| 42 | * |
| 43 | * @param Config $config Migration configuration object. |
| 44 | */ |
| 45 | public function __construct( Config $config ) { |
| 46 | $this->source_store = $config->get_source_store(); |
| 47 | $this->destination_store = $config->get_destination_store(); |
| 48 | $this->source_logger = $config->get_source_logger(); |
| 49 | $this->destination_logger = $config->get_destination_logger(); |
| 50 | |
| 51 | $this->batch_fetcher = new BatchFetcher( $this->source_store ); |
| 52 | if ( $config->get_dry_run() ) { |
| 53 | $this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger ); |
| 54 | $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
| 55 | } else { |
| 56 | $this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger ); |
| 57 | $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
| 58 | } |
| 59 | |
| 60 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 61 | $this->progress_bar = $config->get_progress_bar(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Run migration batch. |
| 67 | * |
| 68 | * @param int $batch_size Optional batch size. Default 10. |
| 69 | * |
| 70 | * @return int Size of batch processed. |
| 71 | */ |
| 72 | public function run( $batch_size = 10 ) { |
| 73 | $batch = $this->batch_fetcher->fetch( $batch_size ); |
| 74 | $batch_size = count( $batch ); |
| 75 | |
| 76 | if ( ! $batch_size ) { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | if ( $this->progress_bar ) { |
| 81 | /* translators: %d: amount of actions */ |
| 82 | $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'woocommerce' ), number_format_i18n( $batch_size ) ) ); |
| 83 | $this->progress_bar->set_count( $batch_size ); |
| 84 | } |
| 85 | |
| 86 | $this->migrate_actions( $batch ); |
| 87 | |
| 88 | return $batch_size; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Migration a batch of actions. |
| 93 | * |
| 94 | * @param array $action_ids List of action IDs to migrate. |
| 95 | */ |
| 96 | public function migrate_actions( array $action_ids ) { |
| 97 | do_action( 'action_scheduler/migration_batch_starting', $action_ids ); |
| 98 | |
| 99 | \ActionScheduler::logger()->unhook_stored_action(); |
| 100 | $this->destination_logger->unhook_stored_action(); |
| 101 | |
| 102 | foreach ( $action_ids as $source_action_id ) { |
| 103 | $destination_action_id = $this->action_migrator->migrate( $source_action_id ); |
| 104 | if ( $destination_action_id ) { |
| 105 | $this->destination_logger->log( $destination_action_id, sprintf( |
| 106 | /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */ |
| 107 | __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'woocommerce' ), |
| 108 | $source_action_id, |
| 109 | get_class( $this->source_store ), |
| 110 | $destination_action_id, |
| 111 | get_class( $this->destination_store ) |
| 112 | ) ); |
| 113 | } |
| 114 | |
| 115 | if ( $this->progress_bar ) { |
| 116 | $this->progress_bar->tick(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if ( $this->progress_bar ) { |
| 121 | $this->progress_bar->finish(); |
| 122 | } |
| 123 | |
| 124 | \ActionScheduler::logger()->hook_stored_action(); |
| 125 | |
| 126 | do_action( 'action_scheduler/migration_batch_complete', $action_ids ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Initialize destination store and logger. |
| 131 | */ |
| 132 | public function init_destination() { |
| 133 | $this->destination_store->init(); |
| 134 | $this->destination_logger->init(); |
| 135 | } |
| 136 | } |
| 137 |