← All changes
|
backend/src/Controllers/Notification/GetNotificationsController.php
+57
-0
0.6.1
→
1.5.1
View file →
| @@ -1,0 +1,57 @@ | ||
| 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\Sanitizer\Sanitizer; | |
| 9 | +use IvyForms\Controllers\Controller; | |
| 10 | +use IvyForms\Services\Notification\NotificationService; | |
| 11 | +use IvyForms\Services\Translations\BackendStrings; | |
| 12 | +use WP_REST_Request; | |
| 13 | +use WP_REST_Response; | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * Class GetNotificationsController | |
| 17 | + * | |
| 18 | + * @package IvyForms\Controllers\Notification | |
| 19 | + */ | |
| 20 | +class GetNotificationsController extends Controller | |
| 21 | +{ | |
| 22 | + private NotificationService $notificationService; | |
| 23 | + | |
| 24 | + public function __construct(NotificationService $notificationService) | |
| 25 | + { | |
| 26 | + $this->notificationService = $notificationService; | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * @param WP_REST_Request $data | |
| 31 | + * | |
| 32 | + * @return WP_REST_Response | |
| 33 | + * @throws ForbiddenException | |
| 34 | + * @throws InvalidArgumentException|NotFoundException | |
| 35 | + */ | |
| 36 | + public function handle(WP_REST_Request $data): WP_REST_Response | |
| 37 | + { | |
| 38 | + // Verify the nonce | |
| 39 | + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); | |
| 40 | + $formId = Sanitizer::sanitizeId($data->get_param('id')); | |
| 41 | + // TODO: Update this logic to only require the form ID after templates. | |
| 42 | + $notificationsArr = $formId | |
| 43 | + ? $this->notificationService->getNotificationsByFormId((int) $formId) | |
| 44 | + : $this->notificationService->getAllNotifications(); | |
| 45 | + | |
| 46 | + $notifications = []; | |
| 47 | + | |
| 48 | + foreach ($notificationsArr as $notification) { | |
| 49 | + $notifications[] = $notification->toArray(); | |
| 50 | + } | |
| 51 | + | |
| 52 | + return new WP_REST_Response([ | |
| 53 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 54 | + 'data' => $notifications, | |
| 55 | + ], 200); | |
| 56 | + } | |
| 57 | +} | |