| 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\NotFoundException; |
| 13 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 14 |
use IvyForms\Controllers\Controller; |
| 15 |
use IvyForms\Services\Confirmation\ConfirmationService; |
| 16 |
use IvyForms\Services\Translations\BackendStrings; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_REST_Response; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class GetConformationController |
| 22 |
* |
| 23 |
* @package IvyForms\Controllers\Confirmation |
| 24 |
*/ |
| 25 |
class GetConformationController 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 InvalidArgumentException |
| 40 |
* @throws NotFoundException |
| 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 |
$confirmationId = Sanitizer::sanitizeId($data->get_param('id')); |
| 48 |
|
| 49 |
if (empty($confirmationId)) { |
| 50 |
throw new InvalidArgumentException( |
| 51 |
BackendStrings::getExceptionStrings()['confirmation_id_required'] |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
$confirmation = $this->confirmationService->getConfirmationById($confirmationId); |
| 56 |
|
| 57 |
return new WP_REST_Response([ |
| 58 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 59 |
'data' => $confirmation->toArray() |
| 60 |
], 200); |
| 61 |
} |
| 62 |
} |
| 63 |
|