PluginProbe
The Innovative Form Builder – IvyForms / 1.1
The Innovative Form Builder – IvyForms v1.1
1.5.1 1.5 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 All 27 releases
ivyforms / backend / src / Controllers / Controller.php

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

127 lines 4.2 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 use IvyForms\Common\Exceptions\InvalidArgumentException;
11 use IvyForms\Common\Exceptions\NotFoundException;
12 use IvyForms\Common\Exceptions\QueryExecutionException;
13 use IvyForms\Common\Exceptions\ValidationException;
14 use IvyForms\Common\Exceptions\ForbiddenException;
15 use IvyForms\Services\Translations\BackendStrings;
16 use RuntimeException;
17 use WP_REST_Request;
18 use WP_REST_Response;
19
20 /**
21 * Class Controller
22 *
23 * @package IvyForms\Controllers
24 */
25 abstract class Controller
26 {
27 /**
28 * Handles the request and returns a structured array response.
29 *
30 * @param WP_REST_Request $data
31 *
32 * @return WP_REST_Response
33 */
34 public function __invoke(WP_REST_Request $data): WP_REST_Response
35 {
36 try {
37 $response = $this->handle($data);
38
39 if ($response->get_status() !== 200) {
40 throw new RuntimeException(
41 BackendStrings::getExceptionStrings()['unexpected_http_status'] . ": {$response->get_status()}"
42 );
43 }
44
45 return new WP_REST_Response([
46 'message' => BackendStrings::getCommonStrings()['ok'],
47 'data' => $response->get_data(),
48 ], 200);
49 } catch (ForbiddenException $e) {
50 // Log errors
51 if (defined('WP_DEBUG') && WP_DEBUG) {
52 error_log('Error 403: ' . $e->getMessage());
53 }
54 // Handle forbidden errors
55 // HTTP 403 FORBIDDEN
56 return new WP_REST_Response([
57 'message' => $e->getMessage(),
58 'data' => []
59 ], 403);
60 } catch (NotFoundException $e) {
61 // Log errors
62 if (defined('WP_DEBUG') && WP_DEBUG) {
63 error_log('Error 404: ' . $e->getMessage());
64 }
65 // Handle not found errors
66 // HTTP 404 NOT FOUND
67 return new WP_REST_Response([
68 'message' => $e->getMessage(),
69 'data' => []
70 ], 404);
71 } catch (InvalidArgumentException $e) {
72 // Log errors
73 if (defined('WP_DEBUG') && WP_DEBUG) {
74 error_log('Error: ' . $e->getMessage());
75 }
76 // Handle validation or invalid arguments
77 // HTTP 400 Bad Request
78 return new WP_REST_Response([
79 'message' => $e->getMessage(),
80 'data' => []
81 ], 400);
82 } catch (ValidationException $e) {
83 // Log errors
84 if (defined('WP_DEBUG') && WP_DEBUG) {
85 error_log('Validation error: ' . $e->getMessage());
86 }
87 // Handle validation for data
88 // HTTP 422 Unprocessable Content
89 return new WP_REST_Response([
90 'message' => 'Validation error: ' . $e->getMessage(),
91 'data' => []
92 ], 422);
93 } catch (QueryExecutionException $e) {
94 // Log errors
95 if (defined('WP_DEBUG') && WP_DEBUG) {
96 error_log('Database error: ' . $e->getMessage());
97 }
98 // Handle validation for database errors
99 // HTTP 500 Internal Server Error
100 return new WP_REST_Response([
101 'message' => 'Database error: ' . $e->getMessage(),
102 'data' => []
103 ], 500);
104 } catch (\Exception $e) {
105 // Log errors
106 if (defined('WP_DEBUG') && WP_DEBUG) {
107 error_log('Unexpected error occurred: ' . $e->getMessage());
108 }
109 // General error handling
110 // HTTP 500 Internal Server Error
111 return new WP_REST_Response([
112 'message' => 'Unexpected error occurred: ' . $e->getMessage(),
113 'data' => []
114 ], 500);
115 }
116 }
117
118 /**
119 * Abstract method to handle the WP_REST_Request.
120 *
121 * @param WP_REST_Request $data
122 *
123 * @return WP_REST_Response
124 */
125 abstract protected function handle(WP_REST_Request $data): WP_REST_Response;
126 }
127