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