← All changes
|
backend/src/Controllers/Notification/DuplicateNotificationController.php
+80
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,80 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\Controllers\Notification; | |
| 4 | + | |
| 5 | +use IvyForms\Common\Exceptions\ForbiddenException; | |
| 6 | +use IvyForms\Common\Exceptions\InvalidArgumentException; | |
| 7 | +use IvyForms\Common\Exceptions\NotFoundException; | |
| 8 | +use IvyForms\Common\Exceptions\QueryExecutionException; | |
| 9 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 10 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 11 | +use IvyForms\Controllers\Controller; | |
| 12 | +use IvyForms\Factory\Notification\NotificationFactory; | |
| 13 | +use IvyForms\Services\Notification\NotificationService; | |
| 14 | +use IvyForms\Services\Translations\BackendStrings; | |
| 15 | +use WP_REST_Request; | |
| 16 | +use WP_REST_Response; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * Class DuplicateNotificationController | |
| 20 | + * | |
| 21 | + * @package IvyForms\Controllers\Notification | |
| 22 | + */ | |
| 23 | +class DuplicateNotificationController extends Controller | |
| 24 | +{ | |
| 25 | + private NotificationService $notificationService; | |
| 26 | + | |
| 27 | + public function __construct(NotificationService $notificationService) | |
| 28 | + { | |
| 29 | + $this->notificationService = $notificationService; | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * @param WP_REST_Request $data | |
| 34 | + * | |
| 35 | + * @return WP_REST_Response | |
| 36 | + * | |
| 37 | + * @throws QueryExecutionException | |
| 38 | + * @throws InvalidArgumentException | |
| 39 | + * @throws NotFoundException | |
| 40 | + * @throws ValidationException | |
| 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 | + // Validate request data | |
| 48 | + $notificationId = Sanitizer::sanitizeId($data->get_param('id')); | |
| 49 | + if (empty($notificationId)) { | |
| 50 | + throw new InvalidArgumentException( | |
| 51 | + BackendStrings::getExceptionStrings()['notification_id_required'] | |
| 52 | + ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + // Retrieve the original notification | |
| 56 | + $originalNotification = $this->notificationService->getNotificationById($notificationId); | |
| 57 | + | |
| 58 | + // Duplicate the notification's parameters, excluding the ID | |
| 59 | + $newNotificationData = $originalNotification->toArray(); | |
| 60 | + unset($newNotificationData['id']); // Exclude the ID | |
| 61 | + $newNotificationData['name'] .= ' (Copy)'; // Append "(Copy)" to the name | |
| 62 | + $newNotification = NotificationFactory::create($newNotificationData); | |
| 63 | + | |
| 64 | + // Save the new notification to the database | |
| 65 | + $newNotificationId = $this->notificationService->createNotification($newNotification); | |
| 66 | + if (!$newNotificationId) { | |
| 67 | + throw new QueryExecutionException( | |
| 68 | + BackendStrings::getSettingsFormBuilderStrings()['failed_to_duplicate_notification' . '.'] | |
| 69 | + ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + // Set the new notification ID | |
| 73 | + $newNotification->setId($newNotificationId); | |
| 74 | + | |
| 75 | + return new WP_REST_Response([ | |
| 76 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 77 | + 'data' => $newNotification->toArray() | |
| 78 | + ], 200); | |
| 79 | + } | |
| 80 | +} | |