| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\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\NotFoundException; |
| 11 |
use IvyForms\Entity\Notification\Notification; |
| 12 |
use IvyForms\Repository\Notification\NotificationRepositoryInterface; |
| 13 |
|
| 14 |
class NotificationService |
| 15 |
{ |
| 16 |
private NotificationRepositoryInterface $notificationRepository; |
| 17 |
|
| 18 |
// Constructor injection for the NotificationRepository via PHP-DI |
| 19 |
public function __construct(NotificationRepositoryInterface $notificationRepository) |
| 20 |
{ |
| 21 |
$this->notificationRepository = $notificationRepository; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get all notifications from the repository. |
| 26 |
* |
| 27 |
* @return array<mixed>|null |
| 28 |
*/ |
| 29 |
public function getAllNotifications(): ?array |
| 30 |
{ |
| 31 |
return $this->notificationRepository->getAll(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Create a new notification. |
| 36 |
* |
| 37 |
* @param Notification $notificationData |
| 38 |
* |
| 39 |
* @return int Notification ID |
| 40 |
*/ |
| 41 |
public function createNotification(Notification $notificationData): int |
| 42 |
{ |
| 43 |
// Create notification via repository and return new notification ID |
| 44 |
return $this->notificationRepository->add($notificationData); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get a specific notification by its ID. |
| 49 |
* |
| 50 |
* @param int $notificationId |
| 51 |
* |
| 52 |
* @return object NotificationEntity |
| 53 |
* |
| 54 |
* @throws NotFoundException If the notification is not found |
| 55 |
*/ |
| 56 |
public function getNotificationById(int $notificationId): object |
| 57 |
{ |
| 58 |
$notification = $this->notificationRepository->getById($notificationId); |
| 59 |
|
| 60 |
if (!$notification) { |
| 61 |
throw new NotFoundException('Notification not found.'); |
| 62 |
} |
| 63 |
|
| 64 |
return $notification; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Update an existing notification. |
| 69 |
* |
| 70 |
* @param int $notificationId |
| 71 |
* @param Notification $notificationData |
| 72 |
* |
| 73 |
* @return bool |
| 74 |
*/ |
| 75 |
public function updateNotification(int $notificationId, Notification $notificationData): bool |
| 76 |
{ |
| 77 |
// Validate notification data before updating |
| 78 |
|
| 79 |
return $this->notificationRepository->update($notificationId, $notificationData); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Delete a notification by its ID. |
| 84 |
* |
| 85 |
* @param int $notificationId |
| 86 |
* |
| 87 |
* @return int |
| 88 |
*/ |
| 89 |
public function deleteNotification(int $notificationId): int |
| 90 |
{ |
| 91 |
return $this->notificationRepository->delete($notificationId); |
| 92 |
} |
| 93 |
/** |
| 94 |
* Get notifications by form ID. |
| 95 |
* |
| 96 |
* @param int $formId |
| 97 |
* |
| 98 |
* @return array<Notification> |
| 99 |
* |
| 100 |
* @throws NotFoundException If no notifications are found |
| 101 |
*/ |
| 102 |
public function getNotificationsByFormId(int $formId): array |
| 103 |
{ |
| 104 |
$notification = $this->notificationRepository->getAllById($formId); |
| 105 |
|
| 106 |
if (!$notification) { |
| 107 |
throw new NotFoundException('Notification not found.'); |
| 108 |
} |
| 109 |
|
| 110 |
return $notification; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get active notifications by form ID. |
| 115 |
* |
| 116 |
* @param int $formId |
| 117 |
* |
| 118 |
* @return array<Notification> |
| 119 |
* |
| 120 |
* @throws NotFoundException If no active notifications are found |
| 121 |
*/ |
| 122 |
public function getActiveNotificationsByFormId(int $formId): array |
| 123 |
{ |
| 124 |
$notification = $this->notificationRepository->getAllActiveById($formId); |
| 125 |
|
| 126 |
if (!$notification) { |
| 127 |
throw new NotFoundException('Active notifications not found.'); |
| 128 |
} |
| 129 |
|
| 130 |
return $notification; |
| 131 |
} |
| 132 |
} |
| 133 |
|