RunMigration.php
132 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\API\Endpoints\Migrations; |
| 4 | |
| 5 | use Exception; |
| 6 | use WP_REST_Request; |
| 7 | use WP_REST_Response; |
| 8 | use Give\MigrationLog\MigrationLogStatus; |
| 9 | use Give\MigrationLog\MigrationLogFactory; |
| 10 | use Give\Framework\Migrations\MigrationsRegister; |
| 11 | |
| 12 | /** |
| 13 | * Class RunMigration |
| 14 | * @package Give\API\Endpoints\Migrations |
| 15 | * |
| 16 | * @since 2.10.0 |
| 17 | */ |
| 18 | class RunMigration extends Endpoint { |
| 19 | |
| 20 | /** @var string */ |
| 21 | protected $endpoint = 'migrations/run-migration'; |
| 22 | |
| 23 | /** |
| 24 | * @var MigrationsRegister |
| 25 | */ |
| 26 | private $migrationRegister; |
| 27 | |
| 28 | /** |
| 29 | * @var MigrationLogFactory |
| 30 | */ |
| 31 | private $migrationLogFactory; |
| 32 | |
| 33 | /** |
| 34 | * RunMigration constructor. |
| 35 | * |
| 36 | * @param MigrationsRegister $migrationsRegister |
| 37 | * @param MigrationLogFactory $migrationLogFactory |
| 38 | */ |
| 39 | public function __construct( |
| 40 | MigrationsRegister $migrationsRegister, |
| 41 | MigrationLogFactory $migrationLogFactory |
| 42 | ) { |
| 43 | $this->migrationRegister = $migrationsRegister; |
| 44 | $this->migrationLogFactory = $migrationLogFactory; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @inheritDoc |
| 49 | */ |
| 50 | public function registerRoute() { |
| 51 | register_rest_route( |
| 52 | 'give-api/v2', |
| 53 | $this->endpoint, |
| 54 | [ |
| 55 | [ |
| 56 | 'methods' => 'POST', |
| 57 | 'callback' => [ $this, 'handleRequest' ], |
| 58 | 'permission_callback' => [ $this, 'permissionsCheck' ], |
| 59 | 'args' => [ |
| 60 | 'id' => [ |
| 61 | 'validate_callback' => function( $param ) { |
| 62 | return ! empty( trim( $param ) ); |
| 63 | }, |
| 64 | ], |
| 65 | ], |
| 66 | ], |
| 67 | 'schema' => [ $this, 'getSchema' ], |
| 68 | ] |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return array |
| 74 | */ |
| 75 | public function getSchema() { |
| 76 | return [ |
| 77 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 78 | 'title' => 'logs', |
| 79 | 'type' => 'object', |
| 80 | 'properties' => [ |
| 81 | 'id' => [ |
| 82 | 'type' => 'string', |
| 83 | 'description' => esc_html__( 'Migration ID', 'give' ), |
| 84 | ], |
| 85 | ], |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param WP_REST_Request $request |
| 91 | * |
| 92 | * @return WP_REST_Response |
| 93 | */ |
| 94 | public function handleRequest( WP_REST_Request $request ) { |
| 95 | global $wpdb; |
| 96 | $migrationId = $request->get_param( 'id' ); |
| 97 | $migrationLog = $this->migrationLogFactory->make( $migrationId ); |
| 98 | |
| 99 | // Begin transaction |
| 100 | $wpdb->query( 'START TRANSACTION' ); |
| 101 | |
| 102 | try { |
| 103 | $migrationClass = $this->migrationRegister->getMigration( $migrationId ); |
| 104 | $migration = give( $migrationClass ); |
| 105 | $migration->run(); |
| 106 | // Save migration status |
| 107 | $migrationLog->setStatus( MigrationLogStatus::SUCCESS ); |
| 108 | $migrationLog->setError( null ); |
| 109 | $migrationLog->save(); |
| 110 | |
| 111 | $wpdb->query( 'COMMIT' ); |
| 112 | |
| 113 | return new WP_REST_Response( [ 'status' => true ] ); |
| 114 | |
| 115 | } catch ( Exception $exception ) { |
| 116 | $wpdb->query( 'ROLLBACK' ); |
| 117 | |
| 118 | $migrationLog->setStatus( MigrationLogStatus::FAILED ); |
| 119 | $migrationLog->setError( $exception ); |
| 120 | $migrationLog->save(); |
| 121 | } |
| 122 | |
| 123 | return new WP_REST_Response( |
| 124 | [ |
| 125 | 'status' => false, |
| 126 | 'message' => $exception->getMessage(), |
| 127 | ] |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | } |
| 132 |