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 / Confirmation / AddConfirmationController.php

AddConfirmationController.php in The Innovative Form Builder – IvyForms 0.1.2, at backend/src/Controllers/Confirmation/AddConfirmationController.php

72 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Controllers\Confirmation;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 if (!defined('ABSPATH')) {
7 exit; // Exit if accessed directly
8 }
9
10 use IvyForms\Common\Exceptions\ForbiddenException;
11 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 use IvyForms\Common\Exceptions\QueryExecutionException;
13 use IvyForms\Common\Sanitizer\Sanitizer;
14 use IvyForms\Controllers\Controller;
15 use IvyForms\Factory\Confirmation\ConfirmationFactory;
16 use IvyForms\Services\Confirmation\ConfirmationService;
17 use WP_REST_Request;
18 use WP_REST_Response;
19
20 /**
21 * Class AddConfirmationController
22 *
23 * @package IvyForms\Controllers\Confirmation
24 */
25 class AddConfirmationController extends Controller
26 {
27 private ConfirmationService $confirmationService;
28
29 public function __construct(ConfirmationService $confirmationService)
30 {
31 $this->confirmationService = $confirmationService;
32 }
33
34 /**
35 * @param WP_REST_Request $data
36 *
37 * @return WP_REST_Response
38 *
39 * @throws QueryExecutionException
40 * @throws InvalidArgumentException
41 * @throws ForbiddenException
42 */
43 public function handle(WP_REST_Request $data): WP_REST_Response
44 {
45 // Verify the nonce
46 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
47
48 // Validate request data
49 if (empty($data->get_params())) {
50 throw new InvalidArgumentException('Invalid request data.');
51 }
52
53 // Sanitize the input data
54 $params = Sanitizer::sanitizeConfirmationData($data->get_params());
55
56 $confirmation = ConfirmationFactory::create($params);
57
58 $confirmationId = $this->confirmationService->createConfirmation($confirmation);
59
60 if (!$confirmationId) {
61 throw new QueryExecutionException('Failed to create confirmation.');
62 }
63
64 $confirmation->setId($confirmationId);
65
66 return new WP_REST_Response([
67 'message' => 'OK',
68 'data' => $confirmation->toArray()
69 ], 200);
70 }
71 }
72