| @@ -1,0 +1,147 @@ | ||
| 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\ServiceUnavailableException; | |
| 14 | +use IvyForms\Common\Exceptions\FieldValidationException; | |
| 15 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 16 | +use IvyForms\Common\Exceptions\ForbiddenException; | |
| 17 | +use IvyForms\Services\Translations\BackendStrings; | |
| 18 | +use RuntimeException; | |
| 19 | +use WP_REST_Request; | |
| 20 | +use WP_REST_Response; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * Class Controller | |
| 24 | + * | |
| 25 | + * @package IvyForms\Controllers | |
| 26 | + */ | |
| 27 | +abstract class Controller | |
| 28 | +{ | |
| 29 | + /** | |
| 30 | + * Handles the request and returns a structured array response. | |
| 31 | + * | |
| 32 | + * @param WP_REST_Request $data | |
| 33 | + * | |
| 34 | + * @return WP_REST_Response | |
| 35 | + */ | |
| 36 | + public function __invoke(WP_REST_Request $data): WP_REST_Response | |
| 37 | + { | |
| 38 | + try { | |
| 39 | + $response = $this->handle($data); | |
| 40 | + | |
| 41 | + if ($response->get_status() !== 200) { | |
| 42 | + throw new RuntimeException( | |
| 43 | + BackendStrings::getExceptionStrings()['unexpected_http_status'] . ": {$response->get_status()}" | |
| 44 | + ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + return new WP_REST_Response([ | |
| 48 | + 'message' => BackendStrings::getCommonStrings()['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 (ServiceUnavailableException $e) { | |
| 74 | + if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 75 | + error_log('Error 503: ' . $e->getMessage()); | |
| 76 | + } | |
| 77 | + | |
| 78 | + return new WP_REST_Response([ | |
| 79 | + 'message' => $e->getMessage(), | |
| 80 | + 'data' => [] | |
| 81 | + ], 503); | |
| 82 | + } catch (InvalidArgumentException $e) { | |
| 83 | + // Log errors | |
| 84 | + if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 85 | + error_log('Error: ' . $e->getMessage()); | |
| 86 | + } | |
| 87 | + // Handle validation or invalid arguments | |
| 88 | + // HTTP 400 Bad Request | |
| 89 | + return new WP_REST_Response([ | |
| 90 | + 'message' => $e->getMessage(), | |
| 91 | + 'data' => [] | |
| 92 | + ], 400); | |
| 93 | + } catch (ValidationException $e) { | |
| 94 | + // Log errors | |
| 95 | + if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 96 | + error_log('Validation error: ' . $e->getMessage()); | |
| 97 | + } | |
| 98 | + // Handle validation for data | |
| 99 | + // HTTP 422 Unprocessable Content | |
| 100 | + $data = []; | |
| 101 | + $fieldErrors = $e->getFieldErrors(); | |
| 102 | + if ($fieldErrors !== []) { | |
| 103 | + if ($e instanceof FieldValidationException) { | |
| 104 | + $data['field_errors'] = $fieldErrors; | |
| 105 | + } else { | |
| 106 | + $data['fieldErrors'] = $fieldErrors; | |
| 107 | + } | |
| 108 | + } | |
| 109 | + | |
| 110 | + return new WP_REST_Response([ | |
| 111 | + 'message' => $e->getMessage(), | |
| 112 | + 'data' => $data, | |
| 113 | + ], 422); | |
| 114 | + } catch (QueryExecutionException $e) { | |
| 115 | + // Log errors | |
| 116 | + if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 117 | + error_log('Database error: ' . $e->getMessage()); | |
| 118 | + } | |
| 119 | + // Handle validation for database errors | |
| 120 | + // HTTP 500 Internal Server Error | |
| 121 | + return new WP_REST_Response([ | |
| 122 | + 'message' => 'Database error: ' . $e->getMessage(), | |
| 123 | + 'data' => [] | |
| 124 | + ], 500); | |
| 125 | + } catch (\Exception $e) { | |
| 126 | + // Log errors | |
| 127 | + if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 128 | + error_log('Unexpected error occurred: ' . $e->getMessage()); | |
| 129 | + } | |
| 130 | + // General error handling | |
| 131 | + // HTTP 500 Internal Server Error | |
| 132 | + return new WP_REST_Response([ | |
| 133 | + 'message' => 'Unexpected error occurred: ' . $e->getMessage(), | |
| 134 | + 'data' => [] | |
| 135 | + ], 500); | |
| 136 | + } | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Abstract method to handle the WP_REST_Request. | |
| 141 | + * | |
| 142 | + * @param WP_REST_Request $data | |
| 143 | + * | |
| 144 | + * @return WP_REST_Response | |
| 145 | + */ | |
| 146 | + abstract protected function handle(WP_REST_Request $data): WP_REST_Response; | |
| 147 | +} | |