PluginProbe
The Innovative Form Builder – IvyForms / 0.3
The Innovative Form Builder – IvyForms v0.3
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Notification / NotificationService.php

NotificationService.php in The Innovative Form Builder – IvyForms 0.3, at backend/src/Services/Notification/NotificationService.php

195 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\InvalidArgumentException;
11 use IvyForms\Common\Exceptions\NotFoundException;
12 use IvyForms\Entity\Notification\Notification;
13 use IvyForms\Factory\Notification\NotificationFactory;
14 use IvyForms\Repository\Notification\NotificationRepositoryInterface;
15 use IvyForms\Services\Translations\BackendStrings;
16
17 class NotificationService
18 {
19 private NotificationRepositoryInterface $notificationRepository;
20
21 // Constructor injection for the NotificationRepository via PHP-DI
22 public function __construct(NotificationRepositoryInterface $notificationRepository)
23 {
24 $this->notificationRepository = $notificationRepository;
25 }
26
27 /**
28 * Get all notifications from the repository.
29 *
30 * @return array<mixed>|null
31 */
32 public function getAllNotifications(): ?array
33 {
34 return $this->notificationRepository->getAll();
35 }
36
37 /**
38 * Create a new notification.
39 *
40 * @param Notification $notificationData
41 *
42 * @return int Notification ID
43 */
44 public function createNotification(Notification $notificationData): int
45 {
46 // Create notification via repository and return new notification ID
47 return $this->notificationRepository->add($notificationData);
48 }
49
50 /**
51 * Get a specific notification by its ID.
52 *
53 * @param int $notificationId
54 *
55 * @return object NotificationEntity
56 *
57 * @throws NotFoundException If the notification is not found
58 */
59 public function getNotificationById(int $notificationId): object
60 {
61 $notification = $this->notificationRepository->getById($notificationId);
62
63 if (!$notification) {
64 throw new NotFoundException(
65 BackendStrings::getExceptionStrings()['notification_not_found']
66 );
67 }
68
69 return $notification;
70 }
71
72 /**
73 * Update an existing notification.
74 *
75 * @param int $notificationId
76 * @param Notification $notificationData
77 *
78 * @return bool
79 */
80 public function updateNotification(int $notificationId, Notification $notificationData): bool
81 {
82 // Validate notification data before updating
83
84 return $this->notificationRepository->update($notificationId, $notificationData);
85 }
86
87 /**
88 * Delete a notification by its ID.
89 *
90 * @param int $notificationId
91 *
92 * @return int
93 */
94 public function deleteNotification(int $notificationId): int
95 {
96 return $this->notificationRepository->delete($notificationId);
97 }
98 /**
99 * Get notifications by form ID.
100 *
101 * @param int $formId
102 *
103 * @return array<Notification>
104 *
105 * @throws NotFoundException If no notifications are found
106 */
107 public function getNotificationsByFormId(int $formId): array
108 {
109 $notification = $this->notificationRepository->getAllById($formId);
110
111 if (!$notification) {
112 throw new NotFoundException(
113 BackendStrings::getExceptionStrings()['notification_not_found']
114 );
115 }
116
117 return $notification;
118 }
119
120 /**
121 * Get active notifications by form ID.
122 *
123 * @param int $formId
124 *
125 * @return array<Notification>
126 *
127 * @throws NotFoundException If no active notifications are found
128 */
129 public function getActiveNotificationsByFormId(int $formId): array
130 {
131 $notification = $this->notificationRepository->getAllActiveById($formId);
132
133 if (!$notification) {
134 throw new NotFoundException(
135 BackendStrings::getExceptionStrings()['active_notif_not_found']
136 );
137 }
138
139 return $notification;
140 }
141
142 /**
143 *
144 * Delete notifications by form ID.
145 *
146 * @throws NotFoundException
147 */
148 public function deleteNotificationsByFormId(int $formId): void
149 {
150 $notifications = $this->getNotificationsByFormId($formId);
151
152 if (!$notifications) {
153 return;
154 }
155
156 foreach ($notifications as $notification) {
157 $this->deleteNotification($notification->getId());
158 }
159 }
160
161 /**
162 * Duplicate notifications from one form to another.
163 *
164 * @param int $originalFormId
165 * @param int $newFormId
166 *
167 * @return void
168 * @throws NotFoundException
169 * @throws InvalidArgumentException
170 */
171 public function duplicateNotifications(int $originalFormId, int $newFormId): void
172 {
173 $notifications = $this->getNotificationsByFormId($originalFormId);
174 foreach ($notifications as $notification) {
175 $notificationData = $notification->toArray();
176 unset($notificationData['id']);
177 $notificationData['formId'] = $newFormId;
178
179 $this->createNotification(NotificationFactory::create($notificationData));
180 }
181 }
182
183 /**
184 * Search notifications with pagination and sorting
185 *
186 * @param array<string, mixed> $params
187 *
188 * @return array<string, mixed>
189 */
190 public function searchNotifications(array $params): array
191 {
192 return $this->notificationRepository->search($params);
193 }
194 }
195