-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotification.php
More file actions
245 lines (218 loc) · 5.74 KB
/
Copy pathNotification.php
File metadata and controls
245 lines (218 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
declare(strict_types=1);
namespace Crustum\Notification;
use Cake\Datasource\EntityInterface;
use Crustum\Notification\Message\DatabaseMessage;
use Crustum\Notification\Trait\SerializesNotificationTrait;
/**
* Notification Base Class
*
* Abstract base class for all notification types.
* Concrete notification classes must extend this and implement the via() method.
*
* Uses SerializesNotification trait for automatic queue serialization.
*
* Usage:
* ```
* class WelcomeNotification extends Notification
* {
* public function via(EntityInterface|AnonymousNotifiable $notifiable): array
* {
* return ['database', 'mail'];
* }
*
* public function toDatabase(EntityInterface|AnonymousNotifiable $notifiable): array
* {
* return ['message' => 'Welcome!'];
* }
* }
* ```
*/
abstract class Notification
{
use SerializesNotificationTrait;
/**
* Unique notification identifier
*
* @var string|null
*/
protected ?string $id = null;
/**
* Locale for this notification
*
* @var string|null
*/
protected ?string $locale = null;
/**
* Queue name for queued notifications
*
* @var string|null
*/
protected ?string $queue = null;
/**
* Queue connection name
*
* @var string|null
*/
protected ?string $connection = null;
/**
* Delay in seconds before sending queued notification
*
* @var int|null
*/
protected ?int $delay = null;
/**
* Get the notification delivery channels
*
* @param \Cake\Datasource\EntityInterface|\Crustum\Notification\AnonymousNotifiable $notifiable The entity to notify
* @return array<string> Array of channel names
*/
abstract public function via(EntityInterface|AnonymousNotifiable $notifiable): array;
/**
* Set the notification ID
*
* @param string $id Unique identifier
* @return void
*/
public function setId(string $id): void
{
$this->id = $id;
}
/**
* Get the notification ID
*
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}
/**
* Set the locale for this notification
*
* @param string $locale Locale code
* @return $this
*/
public function locale(string $locale)
{
$this->locale = $locale;
return $this;
}
/**
* Get the locale for this notification
*
* @return string|null
*/
public function getLocale(): ?string
{
return $this->locale;
}
/**
* Set the queue name for queued notifications
*
* @param string $queue Queue name
* @return $this
*/
public function onQueue(string $queue)
{
$this->queue = $queue;
return $this;
}
/**
* Get the queue name
*
* @return string|null
*/
public function getQueue(): ?string
{
return $this->queue;
}
/**
* Set the queue connection for queued notifications
*
* @param string $connection Connection name
* @return $this
*/
public function onConnection(string $connection)
{
$this->connection = $connection;
return $this;
}
/**
* Get the queue connection
*
* @return string|null
*/
public function getConnection(): ?string
{
return $this->connection;
}
/**
* Set delay for queued notifications
*
* @param int $seconds Delay in seconds
* @return $this
*/
public function delay(int $seconds)
{
$this->delay = $seconds;
return $this;
}
/**
* Get the delay in seconds
*
* @return int|null
*/
public function getDelay(): ?int
{
return $this->delay;
}
/**
* Get the database representation of the notification
*
* Override this method to provide notification data for database storage.
*
* @param \Cake\Datasource\EntityInterface|\Crustum\Notification\AnonymousNotifiable $notifiable The entity receiving the notification
* @return \Crustum\Notification\Message\DatabaseMessage|array<string, mixed> Database message or array
*/
public function toDatabase(EntityInterface|AnonymousNotifiable $notifiable): DatabaseMessage|array
{
return $this->toArray($notifiable);
}
/**
* Get the array representation of the notification
*
* Override this method to provide generic notification data.
*
* @param \Cake\Datasource\EntityInterface|\Crustum\Notification\AnonymousNotifiable $notifiable The entity receiving the notification
* @return array<string, mixed> Notification data
*/
public function toArray(EntityInterface|AnonymousNotifiable $notifiable): array
{
return [];
}
/**
* Determine if notification should be sent
*
* Override this method to conditionally send notifications based on
* notifiable entity or channel.
*
* @param \Cake\Datasource\EntityInterface|\Crustum\Notification\AnonymousNotifiable $notifiable The entity to notify
* @param string $channel The channel name
* @return bool True to send, false to skip
*/
public function shouldSend(EntityInterface|AnonymousNotifiable $notifiable, string $channel): bool
{
return true;
}
/**
* Get the mail representation of the notification
*
* @param \Cake\Datasource\EntityInterface|\Crustum\Notification\AnonymousNotifiable $notifiable The entity receiving the notification
* @return \Crustum\Notification\Message\MailMessage|string|null
*/
public function toMail(EntityInterface|AnonymousNotifiable $notifiable): mixed
{
return null;
}
}