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

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