PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 1.2 1.3
ivyforms / backend / src / Controllers / Controller.php

Controller.php in The Innovative Form Builder – IvyForms 0.8, at backend/src/Controllers/Controller.php

132 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 * @licence See COPYING.md for license details.
6 */
7
8 namespace IvyForms\Controllers;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\InvalidArgumentException;
16 use IvyForms\Common\Exceptions\NotFoundException;
17 use IvyForms\Common\Exceptions\QueryExecutionException;
18 use IvyForms\Common\Exceptions\ValidationException;
19 use IvyForms\Common\Exceptions\ForbiddenException;
20 use IvyForms\Services\Translations\BackendStrings;
21 use RuntimeException;
22 use WP_REST_Request;
23 use WP_REST_Response;
24
25 /**
26 * Class Controller
27 *
28 * @package IvyForms\Controllers
29 */
30 abstract class Controller
31 {
32 /**
33 * Handles the request and returns a structured array response.
34 *
35 * @param WP_REST_Request $data
36 *
37 * @return WP_REST_Response
38 */
39 public function __invoke(WP_REST_Request $data): WP_REST_Response
40 {
41 try {
42 $response = $this->handle($data);
43
44 if ($response->get_status() !== 200) {
45 throw new RuntimeException(
46 BackendStrings::getExceptionStrings()['unexpected_http_status'] . ": {$response->get_status()}"
47 );
48 }
49
50 return new WP_REST_Response([
51 'message' => BackendStrings::getCommonStrings()['ok'],
52 'data' => $response->get_data(),
53 ], 200);
54 } catch (ForbiddenException $e) {
55 // Log errors
56 if (defined('WP_DEBUG') && WP_DEBUG) {
57 error_log('Error 403: ' . $e->getMessage());
58 }
59 // Handle forbidden errors
60 // HTTP 403 FORBIDDEN
61 return new WP_REST_Response([
62 'message' => $e->getMessage(),
63 'data' => []
64 ], 403);
65 } catch (NotFoundException $e) {
66 // Log errors
67 if (defined('WP_DEBUG') && WP_DEBUG) {
68 error_log('Error 404: ' . $e->getMessage());
69 }
70 // Handle not found errors
71 // HTTP 404 NOT FOUND
72 return new WP_REST_Response([
73 'message' => $e->getMessage(),
74 'data' => []
75 ], 404);
76 } catch (InvalidArgumentException $e) {
77 // Log errors
78 if (defined('WP_DEBUG') && WP_DEBUG) {
79 error_log('Error: ' . $e->getMessage());
80 }
81 // Handle validation or invalid arguments
82 // HTTP 400 Bad Request
83 return new WP_REST_Response([
84 'message' => $e->getMessage(),
85 'data' => []
86 ], 400);
87 } catch (ValidationException $e) {
88 // Log errors
89 if (defined('WP_DEBUG') && WP_DEBUG) {
90 error_log('Validation error: ' . $e->getMessage());
91 }
92 // Handle validation for data
93 // HTTP 422 Unprocessable Content
94 return new WP_REST_Response([
95 'message' => 'Validation error: ' . $e->getMessage(),
96 'data' => []
97 ], 422);
98 } catch (QueryExecutionException $e) {
99 // Log errors
100 if (defined('WP_DEBUG') && WP_DEBUG) {
101 error_log('Database error: ' . $e->getMessage());
102 }
103 // Handle validation for database errors
104 // HTTP 500 Internal Server Error
105 return new WP_REST_Response([
106 'message' => 'Database error: ' . $e->getMessage(),
107 'data' => []
108 ], 500);
109 } catch (\Exception $e) {
110 // Log errors
111 if (defined('WP_DEBUG') && WP_DEBUG) {
112 error_log('Unexpected error occurred: ' . $e->getMessage());
113 }
114 // General error handling
115 // HTTP 500 Internal Server Error
116 return new WP_REST_Response([
117 'message' => 'Unexpected error occurred: ' . $e->getMessage(),
118 'data' => []
119 ], 500);
120 }
121 }
122
123 /**
124 * Abstract method to handle the WP_REST_Request.
125 *
126 * @param WP_REST_Request $data
127 *
128 * @return WP_REST_Response
129 */
130 abstract protected function handle(WP_REST_Request $data): WP_REST_Response;
131 }
132