Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AppController | |
0.00% |
0 / 63 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
| register_routes | |
0.00% |
0 / 33 |
|
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 | |||
| complete_blueprint | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 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 | /** |
| 9 | * AppController class for handling onboarding application REST API endpoints. |
| 10 | * |
| 11 | * This controller manages the REST API routes and handlers for the onboarding |
| 12 | * application functionality. It provides endpoints for starting and completing |
| 13 | * the onboarding process. |
| 14 | */ |
| 15 | class AppController { |
| 16 | |
| 17 | /** |
| 18 | * This is the REST API namespace that will be used for our custom API |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $namespace = 'newfold-onboarding/v1'; |
| 23 | |
| 24 | /** |
| 25 | * This is the REST endpoint |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $rest_base = '/app'; |
| 30 | |
| 31 | /** |
| 32 | * Register the REST API routes for the onboarding application. |
| 33 | * |
| 34 | * Registers two main endpoints: |
| 35 | * - /app/start: Initiates the onboarding process |
| 36 | * - /app/complete: Completes the onboarding process with selected homepage |
| 37 | */ |
| 38 | public function register_routes() { |
| 39 | \register_rest_route( |
| 40 | $this->namespace, |
| 41 | $this->rest_base . '/start', |
| 42 | array( |
| 43 | array( |
| 44 | 'methods' => \WP_REST_Server::EDITABLE, |
| 45 | 'callback' => array( $this, 'start' ), |
| 46 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 47 | ), |
| 48 | ) |
| 49 | ); |
| 50 | \register_rest_route( |
| 51 | $this->namespace, |
| 52 | $this->rest_base . '/complete', |
| 53 | array( |
| 54 | array( |
| 55 | 'methods' => \WP_REST_Server::EDITABLE, |
| 56 | 'callback' => array( $this, 'complete' ), |
| 57 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 58 | ), |
| 59 | ) |
| 60 | ); |
| 61 | \register_rest_route( |
| 62 | $this->namespace, |
| 63 | $this->rest_base . '/complete-blueprint', |
| 64 | array( |
| 65 | array( |
| 66 | 'methods' => \WP_REST_Server::EDITABLE, |
| 67 | 'callback' => array( $this, 'complete_blueprint' ), |
| 68 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 69 | ), |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Start onboarding backend process. |
| 76 | * |
| 77 | * @return \WP_REST_Response The response object. |
| 78 | */ |
| 79 | public function start(): \WP_REST_Response { |
| 80 | try { |
| 81 | ( new AppService() )->start(); |
| 82 | return new \WP_REST_Response( array(), 202 ); |
| 83 | } catch ( \Exception $e ) { |
| 84 | return new \WP_REST_Response( |
| 85 | array( |
| 86 | 'error' => 'Encountered an error while starting the app service.', |
| 87 | ), |
| 88 | 500 |
| 89 | ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Complete onboarding backend process. |
| 95 | * |
| 96 | * @param \WP_REST_Request $request The request object. |
| 97 | * @return \WP_REST_Response The response object. |
| 98 | */ |
| 99 | public function complete( \WP_REST_Request $request ): \WP_REST_Response { |
| 100 | $data = json_decode( $request->get_body(), true ); |
| 101 | $selected_sitegen_homepage = $data['selected_sitegen_homepage'] ?? false; |
| 102 | if ( ! $selected_sitegen_homepage ) { |
| 103 | return new \WP_REST_Response( |
| 104 | array( 'error' => 'Selected sitegen homepage is required.' ), |
| 105 | 400 |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | try { |
| 110 | ( new AppService() )->complete( $selected_sitegen_homepage ); |
| 111 | return new \WP_REST_Response( array(), 200 ); |
| 112 | } catch ( \Exception $e ) { |
| 113 | return new \WP_REST_Response( |
| 114 | array( 'error' => 'Encountered an error while completing the app service.' ), |
| 115 | 500 |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Complete blueprint onboarding backend process. |
| 122 | * |
| 123 | * @param \WP_REST_Request $request The request object. |
| 124 | * @return \WP_REST_Response The response object. |
| 125 | */ |
| 126 | public function complete_blueprint( \WP_REST_Request $request ): \WP_REST_Response { |
| 127 | try { |
| 128 | ( new AppService() )->complete_blueprint(); |
| 129 | return new \WP_REST_Response( array(), 200 ); |
| 130 | } catch ( \Exception $e ) { |
| 131 | return new \WP_REST_Response( |
| 132 | array( 'error' => 'Encountered an error while completing the blueprint app service.' ), |
| 133 | 500 |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 | } |