Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppController
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 register_routes
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
2
 start
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 complete
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace NewfoldLabs\WP\Module\Onboarding\RestApi;
4
5use NewfoldLabs\WP\Module\Onboarding\Permissions;
6use 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 */
15class 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
51        \register_rest_route(
52            $this->namespace,
53            $this->rest_base . '/complete',
54            array(
55                array(
56                    'methods'             => \WP_REST_Server::EDITABLE,
57                    'callback'            => array( $this, 'complete' ),
58                    'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
59                ),
60            )
61        );
62    }
63
64    /**
65     * Start onboarding backend process.
66     *
67     * @return \WP_REST_Response The response object.
68     */
69    public function start(): \WP_REST_Response {
70        try {
71            ( new AppService() )->start();
72            return new \WP_REST_Response( array(), 202 );
73        } catch ( \Exception $e ) {
74            return new \WP_REST_Response(
75                array(
76                    'error' => 'Encountered an error while starting the app service.',
77                ),
78                500
79            );
80        }
81    }
82
83    /**
84     * Complete onboarding backend process.
85     *
86     * @param \WP_REST_Request $request The request object.
87     * @return \WP_REST_Response The response object.
88     */
89    public function complete( \WP_REST_Request $request ): \WP_REST_Response {
90        $data                      = json_decode( $request->get_body(), true );
91        $selected_sitegen_homepage = $data['selected_sitegen_homepage'] ?? false;
92        if ( ! $selected_sitegen_homepage ) {
93            return new \WP_REST_Response(
94                array( 'error' => 'Selected sitegen homepage is required.' ),
95                400
96            );
97        }
98
99        try {
100            ( new AppService() )->complete( $selected_sitegen_homepage );
101            return new \WP_REST_Response( array(), 200 );
102        } catch ( \Exception $e ) {
103            return new \WP_REST_Response(
104                array( 'error' => 'Encountered an error while completing the app service.' ),
105                500
106            );
107        }
108    }
109}