← All changes
|
backend/src/Controllers/Confirmation/UpdateConfirmationController.php
+77
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,77 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\Controllers\Confirmation; | |
| 4 | + | |
| 5 | +use IvyForms\Common\Exceptions\ForbiddenException; | |
| 6 | +use IvyForms\Common\Exceptions\InvalidArgumentException; | |
| 7 | +use IvyForms\Common\Exceptions\QueryExecutionException; | |
| 8 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 9 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 10 | +use IvyForms\Controllers\Controller; | |
| 11 | +use IvyForms\Factory\Confirmation\ConfirmationFactory; | |
| 12 | +use IvyForms\Services\Confirmation\ConfirmationService; | |
| 13 | +use IvyForms\Services\Translations\BackendStrings; | |
| 14 | +use WP_REST_Request; | |
| 15 | +use WP_REST_Response; | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * Class UpdateConfirmationController | |
| 19 | + * | |
| 20 | + * @package IvyForms\Controllers\Confirmation | |
| 21 | + */ | |
| 22 | +class UpdateConfirmationController extends Controller | |
| 23 | +{ | |
| 24 | + private ConfirmationService $confirmationService; | |
| 25 | + | |
| 26 | + public function __construct(ConfirmationService $confirmationService) | |
| 27 | + { | |
| 28 | + $this->confirmationService = $confirmationService; | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * @param WP_REST_Request $data | |
| 33 | + * | |
| 34 | + * @return WP_REST_Response | |
| 35 | + * | |
| 36 | + * @throws QueryExecutionException | |
| 37 | + * @throws InvalidArgumentException | |
| 38 | + * @throws ForbiddenException | |
| 39 | + * @throws ValidationException | |
| 40 | + */ | |
| 41 | + public function handle(WP_REST_Request $data): WP_REST_Response | |
| 42 | + { | |
| 43 | + // Verify the nonce | |
| 44 | + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); | |
| 45 | + // Validate and sanitize input data | |
| 46 | + if (empty($data->get_params())) { | |
| 47 | + throw new InvalidArgumentException( | |
| 48 | + BackendStrings::getExceptionStrings()['invalid_request_data'] | |
| 49 | + ); | |
| 50 | + } | |
| 51 | + | |
| 52 | + $params = Sanitizer::sanitizeConfirmationData($data->get_params()); | |
| 53 | + | |
| 54 | + if (empty(trim($params['name'] ?? ''))) { | |
| 55 | + throw new InvalidArgumentException( | |
| 56 | + BackendStrings::getSettingsFormBuilderStrings()['confirmation_name_required'] | |
| 57 | + ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + $confirmation = ConfirmationFactory::create($params); | |
| 61 | + | |
| 62 | + $confirmationId = $confirmation->getId(); | |
| 63 | + | |
| 64 | + $this->confirmationService->updateConfirmation($confirmationId, $confirmation); | |
| 65 | + | |
| 66 | + if (!$confirmation->getId()) { | |
| 67 | + throw new QueryExecutionException( | |
| 68 | + BackendStrings::getSettingsFormBuilderStrings()['failed_to_update_confirmation'] . '.' | |
| 69 | + ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + return new WP_REST_Response([ | |
| 73 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 74 | + 'data' => $confirmation->toArray() | |
| 75 | + ], 200); | |
| 76 | + } | |
| 77 | +} | |