PluginProbe
The Innovative Form Builder – IvyForms / 0.3
The Innovative Form Builder – IvyForms v0.3
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 0.3, at backend/src/Controllers/Controller.php

130 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 © TMS-Plugins. 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("Unexpected HTTP status code: {$response->get_status()}");
46 }
47
48 return new WP_REST_Response([
49 'message' => BackendStrings::getCommonStrings()['ok'],
50 'data' => $response->get_data(),
51 ], 200);
52 } catch (ForbiddenException $e) {
53 // Log errors
54 if (defined('WP_DEBUG') && WP_DEBUG) {
55 error_log('Error 403: ' . $e->getMessage());
56 }
57 // Handle forbidden errors
58 // HTTP 403 FORBIDDEN
59 return new WP_REST_Response([
60 'message' => $e->getMessage(),
61 'data' => []
62 ], 403);
63 } catch (NotFoundException $e) {
64 // Log errors
65 if (defined('WP_DEBUG') && WP_DEBUG) {
66 error_log('Error 404: ' . $e->getMessage());
67 }
68 // Handle not found errors
69 // HTTP 404 NOT FOUND
70 return new WP_REST_Response([
71 'message' => $e->getMessage(),
72 'data' => []
73 ], 404);
74 } catch (InvalidArgumentException $e) {
75 // Log errors
76 if (defined('WP_DEBUG') && WP_DEBUG) {
77 error_log('Error: ' . $e->getMessage());
78 }
79 // Handle validation or invalid arguments
80 // HTTP 400 Bad Request
81 return new WP_REST_Response([
82 'message' => $e->getMessage(),
83 'data' => []
84 ], 400);
85 } catch (ValidationException $e) {
86 // Log errors
87 if (defined('WP_DEBUG') && WP_DEBUG) {
88 error_log('Validation error: ' . $e->getMessage());
89 }
90 // Handle validation for data
91 // HTTP 422 Unprocessable Content
92 return new WP_REST_Response([
93 'message' => 'Validation error: ' . $e->getMessage(),
94 'data' => []
95 ], 422);
96 } catch (QueryExecutionException $e) {
97 // Log errors
98 if (defined('WP_DEBUG') && WP_DEBUG) {
99 error_log('Database error: ' . $e->getMessage());
100 }
101 // Handle validation for database errors
102 // HTTP 500 Internal Server Error
103 return new WP_REST_Response([
104 'message' => 'Database error: ' . $e->getMessage(),
105 'data' => []
106 ], 500);
107 } catch (\Exception $e) {
108 // Log errors
109 if (defined('WP_DEBUG') && WP_DEBUG) {
110 error_log('Unexpected error occurred: ' . $e->getMessage());
111 }
112 // General error handling
113 // HTTP 500 Internal Server Error
114 return new WP_REST_Response([
115 'message' => 'Unexpected error occurred: ' . $e->getMessage(),
116 'data' => []
117 ], 500);
118 }
119 }
120
121 /**
122 * Abstract method to handle the WP_REST_Request.
123 *
124 * @param WP_REST_Request $data
125 *
126 * @return WP_REST_Response
127 */
128 abstract protected function handle(WP_REST_Request $data): WP_REST_Response;
129 }
130