Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AppController | |
0.00% |
0 / 45 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| register_routes | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
2 | |||
| start | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| complete | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
| 6 | use NewfoldLabs\WP\Module\Onboarding\Services\AppService; |
| 7 | |
| 8 | class AppController { |
| 9 | |
| 10 | /** |
| 11 | * This is the REST API namespace that will be used for our custom API |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $namespace = 'newfold-onboarding/v1'; |
| 16 | |
| 17 | /** |
| 18 | * This is the REST endpoint |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $rest_base = '/app'; |
| 23 | |
| 24 | public function register_routes() { |
| 25 | \register_rest_route( |
| 26 | $this->namespace, |
| 27 | $this->rest_base . '/start', |
| 28 | array( |
| 29 | array( |
| 30 | 'methods' => \WP_REST_Server::EDITABLE, |
| 31 | 'callback' => array( $this, 'start' ), |
| 32 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 33 | ), |
| 34 | ) |
| 35 | ); |
| 36 | |
| 37 | \register_rest_route( |
| 38 | $this->namespace, |
| 39 | $this->rest_base . '/complete', |
| 40 | array( |
| 41 | array( |
| 42 | 'methods' => \WP_REST_Server::EDITABLE, |
| 43 | 'callback' => array( $this, 'complete' ), |
| 44 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 45 | ), |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Start onboarding backend process. |
| 52 | * |
| 53 | * @return \WP_REST_Response The response object. |
| 54 | */ |
| 55 | public function start(): \WP_REST_Response { |
| 56 | try { |
| 57 | ( new AppService() )->start(); |
| 58 | return new \WP_REST_Response( array(), 202 ); |
| 59 | } catch ( \Exception $e ) { |
| 60 | return new \WP_REST_Response( |
| 61 | array( |
| 62 | 'error' => 'Encountered an error while starting the app service.', |
| 63 | ), |
| 64 | 500 |
| 65 | ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Complete onboarding backend process. |
| 71 | * |
| 72 | * @param \WP_REST_Request $request The request object. |
| 73 | * @return \WP_REST_Response The response object. |
| 74 | */ |
| 75 | public function complete( \WP_REST_Request $request ): \WP_REST_Response { |
| 76 | $data = json_decode( $request->get_body(), true ); |
| 77 | $selected_sitegen_homepage = $data['selected_sitegen_homepage']; |
| 78 | if ( ! $selected_sitegen_homepage ) { |
| 79 | return new \WP_REST_Response( |
| 80 | array( 'error' => 'Selected sitegen homepage is required.' ), |
| 81 | 400 |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | try { |
| 86 | ( new AppService() )->complete( $selected_sitegen_homepage ); |
| 87 | return new \WP_REST_Response( array(), 200 ); |
| 88 | } catch ( \Exception $e ) { |
| 89 | return new \WP_REST_Response( |
| 90 | array( 'error' => 'Encountered an error while completing the app service.' ), |
| 91 | 500 |
| 92 | ); |
| 93 | } |
| 94 | } |
| 95 | } |