| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Notification; |
| 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\Notification\NotificationFactory; |
| 16 |
use IvyForms\Services\Notification\NotificationService; |
| 17 |
use IvyForms\Services\Translations\BackendStrings; |
| 18 |
use WP_REST_Request; |
| 19 |
use WP_REST_Response; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class UpdateNotificationController |
| 23 |
* |
| 24 |
* @package IvyForms\Controllers\Notification |
| 25 |
*/ |
| 26 |
class UpdateNotificationController extends Controller |
| 27 |
{ |
| 28 |
private NotificationService $notificationService; |
| 29 |
|
| 30 |
public function __construct(NotificationService $notificationService) |
| 31 |
{ |
| 32 |
$this->notificationService = $notificationService; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param WP_REST_Request $data |
| 37 |
* |
| 38 |
* @return WP_REST_Response |
| 39 |
* |
| 40 |
* @throws QueryExecutionException |
| 41 |
* @throws InvalidArgumentException |
| 42 |
* @throws ForbiddenException |
| 43 |
*/ |
| 44 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 45 |
{ |
| 46 |
// Verify the nonce |
| 47 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 48 |
// Validate and sanitize input data |
| 49 |
if (empty($data->get_params())) { |
| 50 |
throw new InvalidArgumentException( |
| 51 |
BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
$params = Sanitizer::sanitizeNotificationData($data->get_params()); |
| 56 |
|
| 57 |
$notification = NotificationFactory::create($params); |
| 58 |
|
| 59 |
$notificationId = $notification->getId(); |
| 60 |
|
| 61 |
$this->notificationService->updateNotification($notificationId, $notification); |
| 62 |
|
| 63 |
if (!$notification->getId()) { |
| 64 |
throw new QueryExecutionException( |
| 65 |
BackendStrings::getSettingsFormBuilderStrings()['failed_to_update_notification'] . '.' |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
return new WP_REST_Response([ |
| 70 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 71 |
'data' => $notification->toArray() |
| 72 |
], 200); |
| 73 |
} |
| 74 |
} |
| 75 |
|