PluginProbe
The Innovative Form Builder – IvyForms / 0.2.1
The Innovative Form Builder – IvyForms v0.2.1
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.2.1, at backend/src/Services/Notification/NotificationService.php

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