| 1 |
<?php |
| 2 |
/** |
| 3 |
* Migration Controller - REST API endpoints for migration |
| 4 |
* |
| 5 |
* @package Yatra\Migration |
| 6 |
* @since 3.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Yatra\Migration; |
| 10 |
|
| 11 |
use WP_REST_Request; |
| 12 |
use WP_REST_Response; |
| 13 |
use Yatra\Migration\MigrationProgress; |
| 14 |
|
| 15 |
class MigrationController |
| 16 |
{ |
| 17 |
private $service; |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$this->service = new MigrationProgress(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Clear migration data (progress/logs) |
| 26 |
*/ |
| 27 |
public function clear(WP_REST_Request $request): WP_REST_Response |
| 28 |
{ |
| 29 |
try { |
| 30 |
$result = $this->service->clearMigrationData(); |
| 31 |
|
| 32 |
return new WP_REST_Response($result, 200); |
| 33 |
|
| 34 |
} catch (\Exception $e) { |
| 35 |
return new WP_REST_Response([ |
| 36 |
'success' => false, |
| 37 |
'error' => $e->getMessage(), |
| 38 |
], 500); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Register REST API routes |
| 44 |
*/ |
| 45 |
public function registerRoutes(): void |
| 46 |
{ |
| 47 |
register_rest_route('yatra/v1', '/migration/status', [ |
| 48 |
'methods' => 'GET', |
| 49 |
'callback' => [$this, 'getStatus'], |
| 50 |
'permission_callback' => [$this, 'checkPermission'], |
| 51 |
]); |
| 52 |
|
| 53 |
register_rest_route('yatra/v1', '/migration/migrate', [ |
| 54 |
'methods' => 'POST', |
| 55 |
'callback' => [$this, 'migrate'], |
| 56 |
'permission_callback' => [$this, 'checkPermission'], |
| 57 |
'args' => [ |
| 58 |
'data_type' => [ |
| 59 |
'required' => true, |
| 60 |
'type' => 'string', |
| 61 |
], |
| 62 |
'force' => [ |
| 63 |
'type' => 'boolean', |
| 64 |
'default' => false, |
| 65 |
], |
| 66 |
], |
| 67 |
]); |
| 68 |
|
| 69 |
register_rest_route('yatra/v1', '/migration/migrate-all', [ |
| 70 |
'methods' => 'POST', |
| 71 |
'callback' => [$this, 'migrateAll'], |
| 72 |
'permission_callback' => [$this, 'checkPermission'], |
| 73 |
'args' => [ |
| 74 |
'force' => [ |
| 75 |
'type' => 'boolean', |
| 76 |
'default' => false, |
| 77 |
], |
| 78 |
], |
| 79 |
]); |
| 80 |
|
| 81 |
register_rest_route('yatra/v1', '/migration/progress', [ |
| 82 |
'methods' => 'GET', |
| 83 |
'callback' => [$this, 'getProgress'], |
| 84 |
'permission_callback' => [$this, 'checkPermission'], |
| 85 |
]); |
| 86 |
|
| 87 |
register_rest_route('yatra/v1', '/migration/cancel', [ |
| 88 |
'methods' => 'POST', |
| 89 |
'callback' => [$this, 'cancel'], |
| 90 |
'permission_callback' => [$this, 'checkPermission'], |
| 91 |
]); |
| 92 |
|
| 93 |
register_rest_route('yatra/v1', '/migration/clear', [ |
| 94 |
'methods' => 'POST', |
| 95 |
'callback' => [$this, 'clear'], |
| 96 |
'permission_callback' => [$this, 'checkPermission'], |
| 97 |
]); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get migration status |
| 102 |
*/ |
| 103 |
public function getStatus(WP_REST_Request $request): WP_REST_Response |
| 104 |
{ |
| 105 |
try { |
| 106 |
$status = $this->service->getStatus(); |
| 107 |
|
| 108 |
return new WP_REST_Response($status, 200); |
| 109 |
|
| 110 |
} catch (\Exception $e) { |
| 111 |
return new WP_REST_Response([ |
| 112 |
'success' => false, |
| 113 |
'message' => $e->getMessage(), |
| 114 |
], 500); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Migrate specific data type |
| 120 |
*/ |
| 121 |
public function migrate(WP_REST_Request $request): WP_REST_Response |
| 122 |
{ |
| 123 |
try { |
| 124 |
$dataType = $request->get_param('data_type'); |
| 125 |
$force = (bool) $request->get_param('force'); |
| 126 |
|
| 127 |
$result = $this->service->migrate($dataType, $force); |
| 128 |
|
| 129 |
return new WP_REST_Response($result, 200); |
| 130 |
|
| 131 |
} catch (\Exception $e) { |
| 132 |
return new WP_REST_Response([ |
| 133 |
'success' => false, |
| 134 |
'message' => $e->getMessage(), |
| 135 |
], 500); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Migrate all data types |
| 141 |
*/ |
| 142 |
public function migrateAll(WP_REST_Request $request): WP_REST_Response |
| 143 |
{ |
| 144 |
try { |
| 145 |
$force = (bool) $request->get_param('force'); |
| 146 |
$results = $this->service->migrateAll($force); |
| 147 |
|
| 148 |
// Return results directly - service already returns proper structure |
| 149 |
return new WP_REST_Response($results, 200); |
| 150 |
|
| 151 |
} catch (\Exception $e) { |
| 152 |
return new WP_REST_Response([ |
| 153 |
'success' => false, |
| 154 |
'error' => $e->getMessage(), |
| 155 |
], 500); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get migration progress |
| 161 |
*/ |
| 162 |
public function getProgress(WP_REST_Request $request): WP_REST_Response |
| 163 |
{ |
| 164 |
try { |
| 165 |
$progress = $this->service->getMigrationProgress(); |
| 166 |
|
| 167 |
return new WP_REST_Response($progress, 200); |
| 168 |
|
| 169 |
} catch (\Exception $e) { |
| 170 |
return new WP_REST_Response([ |
| 171 |
'success' => false, |
| 172 |
'message' => $e->getMessage(), |
| 173 |
], 500); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Cancel migration |
| 179 |
*/ |
| 180 |
public function cancel(WP_REST_Request $request): WP_REST_Response |
| 181 |
{ |
| 182 |
try { |
| 183 |
$result = $this->service->cancelMigration(); |
| 184 |
|
| 185 |
return new WP_REST_Response($result, 200); |
| 186 |
|
| 187 |
} catch (\Exception $e) { |
| 188 |
return new WP_REST_Response([ |
| 189 |
'success' => false, |
| 190 |
'error' => $e->getMessage(), |
| 191 |
], 500); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Check if user has permission |
| 197 |
*/ |
| 198 |
public function checkPermission() |
| 199 |
{ |
| 200 |
if (!is_user_logged_in()) { |
| 201 |
return new \WP_Error( |
| 202 |
'rest_forbidden', |
| 203 |
__('You must be logged in to perform this action.', 'yatra'), |
| 204 |
['status' => 401] |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
if (!current_user_can('manage_options')) { |
| 209 |
return new \WP_Error( |
| 210 |
'rest_forbidden', |
| 211 |
__('You do not have permission to perform this action.', 'yatra'), |
| 212 |
['status' => 403] |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
return true; |
| 217 |
} |
| 218 |
} |
| 219 |
|