-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationManager.php
More file actions
298 lines (262 loc) · 8.81 KB
/
Copy pathNotificationManager.php
File metadata and controls
298 lines (262 loc) · 8.81 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
declare(strict_types=1);
namespace Crustum\Notification;
use Cake\Core\StaticConfigTrait;
use Cake\Datasource\EntityInterface;
use Crustum\Notification\Channel\ChannelInterface;
use Crustum\Notification\Channel\DatabaseChannel;
use Crustum\Notification\Channel\MailChannel;
use Crustum\Notification\Registry\ChannelRegistry;
use InvalidArgumentException;
/**
* Notification Manager
*
* Static facade for managing notification channels and sending notifications.
* Uses ChannelRegistry to load and manage channel instances.
*
* Usage:
* ```
* // Configure channels
* NotificationManager::setConfig('database', [
* 'className' => DatabaseChannel::class,
* ]);
*
* // Send notification
* NotificationManager::send($user, new WelcomeNotification());
*
* // Get channel instance
* $channel = NotificationManager::channel('database');
* ```
*/
class NotificationManager
{
use StaticConfigTrait;
/**
* Notification Channel Registry used for creating and using channel instances
*
* @var \Crustum\Notification\Registry\ChannelRegistry
*/
protected static ChannelRegistry $_registry;
/**
* Locale for notifications
*
* @var string|null
*/
protected static ?string $_locale = null;
/**
* Notification sender class to use
*
* @var class-string<\Crustum\Notification\NotificationSender>|null
*/
protected static ?string $_senderClass = null;
/**
* Returns the Channel Registry instance used for creating and using channel instances
*
* @return \Crustum\Notification\Registry\ChannelRegistry
*/
public static function getRegistry(): ChannelRegistry
{
if (!isset(static::$_registry)) {
static::$_registry = new ChannelRegistry();
static::$_registry->dispatchDiscoveryEvent();
}
return static::$_registry;
}
/**
* Sets the Channel Registry instance used for creating and using channel instances
*
* Also allows for injecting of a new registry instance.
*
* @param \Crustum\Notification\Registry\ChannelRegistry $registry Injectable registry object
* @return void
*/
public static function setRegistry(ChannelRegistry $registry): void
{
static::$_registry = $registry;
}
/**
* Get a ChannelInterface object for the named notification channel
*
* Can be called with channel name or class name:
* - NotificationManager::channel('database')
* - NotificationManager::channel(DatabaseChannel::class)
*
* @param string $name The name or class name of the notification channel
* @return \Crustum\Notification\Channel\ChannelInterface
* @throws \InvalidArgumentException When channel configuration is missing
*/
public static function channel(string $name): ChannelInterface
{
$registry = static::getRegistry();
if (class_exists($name) && is_subclass_of($name, ChannelInterface::class)) {
$className = $name;
$name = static::_getChannelNameFromClass($className);
if (!$registry->has($name)) {
$registry->load($name, ['className' => $className]);
}
return $registry->get($name);
}
if ($registry->has($name)) {
return $registry->get($name);
}
static::_buildChannel($name);
return $registry->get($name);
}
/**
* Build and load a notification channel
*
* @param string $name Name of the channel configuration
* @return void
* @throws \InvalidArgumentException When channel configuration is missing
*/
protected static function _buildChannel(string $name): void
{
$registry = static::getRegistry();
$defaultChannels = static::_getDefaultChannels();
if (isset($defaultChannels[$name])) {
$config = ['className' => $defaultChannels[$name]];
$registry->load($name, $config);
return;
}
if (empty(static::$_config[$name]['className'])) {
throw new InvalidArgumentException(
sprintf('The `%s` notification channel configuration does not exist.', $name),
);
}
$config = static::$_config[$name];
$registry->load($name, $config);
}
/**
* Get default channel mappings
*
* @return array<string, class-string<\Crustum\Notification\Channel\ChannelInterface>>
*/
protected static function _getDefaultChannels(): array
{
return [
'database' => DatabaseChannel::class,
'mail' => MailChannel::class,
];
}
/**
* Get channel name from class name
*
* @param string $className Channel class name
* @return string Channel name
*/
protected static function _getChannelNameFromClass(string $className): string
{
$parts = explode('\\', $className);
$shortName = end($parts);
return strtolower(str_replace('Channel', '', $shortName));
}
/**
* Configure the sender class to use
*
* @param class-string<\Crustum\Notification\NotificationSender> $class Sender class name
* @return void
*/
public static function configureSender(string $class): void
{
static::$_senderClass = $class;
}
/**
* Reset the sender class to default
*
* @return void
*/
public static function resetSender(): void
{
static::$_senderClass = null;
}
/**
* Get a sender instance
*
* @param string|null $locale Locale for notifications
* @return \Crustum\Notification\NotificationSender
*/
public static function getSender(?string $locale = null): NotificationSender
{
$class = static::$_senderClass ?? NotificationSender::class;
return new $class($locale);
}
/**
* Send the given notification to the given notifiable entities
*
* @param \Cake\Datasource\EntityInterface|\Crustum\Notification\AnonymousNotifiable|iterable<\Cake\Datasource\EntityInterface|\Crustum\Notification\AnonymousNotifiable> $notifiables The entity or entities to notify
* @param \Crustum\Notification\Notification $notification The notification to send
* @return void
*/
public static function send(EntityInterface|AnonymousNotifiable|iterable $notifiables, Notification $notification): void
{
static::getSender(static::$_locale)->send($notifiables, $notification);
}
/**
* Send the given notification immediately
*
* @param \Cake\Datasource\EntityInterface|\Crustum\Notification\AnonymousNotifiable|iterable<\Cake\Datasource\EntityInterface|\Crustum\Notification\AnonymousNotifiable> $notifiables The entity or entities to notify
* @param \Crustum\Notification\Notification $notification The notification to send
* @param array<string>|null $channels Optional array of specific channels to use
* @return void
*/
public static function sendNow(EntityInterface|AnonymousNotifiable|iterable $notifiables, Notification $notification, ?array $channels = null): void
{
static::getSender(static::$_locale)->sendNow($notifiables, $notification, $channels);
}
/**
* Set the locale for notifications
*
* @param string $locale Locale code
* @return void
*/
public static function locale(string $locale): void
{
static::$_locale = $locale;
}
/**
* Get the list of configured channels
*
* @return list<int|string> List of channel names
*/
public static function configured(): array
{
return array_keys(static::$_config);
}
/**
* Create an AnonymousNotifiable with routing for a single channel
*
* ```
* NotificationManager::route('broadcast', 'admin-channel')
* ->notify(new SystemAlert('Server down'));
* ```
*
* @param string $channel Channel name
* @param mixed $route Routing information
* @return \Crustum\Notification\AnonymousNotifiable
*/
public static function route(string $channel, mixed $route): AnonymousNotifiable
{
return (new AnonymousNotifiable())->route($channel, $route);
}
/**
* Create an AnonymousNotifiable with routing for multiple channels
*
* ```
* NotificationManager::routes([
* 'broadcast' => 'admin-channel',
* 'slack' => '#alerts',
* ])->notify(new SystemAlert('Server down'));
* ```
*
* @param array<string, mixed> $routes Channel routing information
* @return \Crustum\Notification\AnonymousNotifiable
*/
public static function routes(array $routes): AnonymousNotifiable
{
$anonymous = new AnonymousNotifiable();
foreach ($routes as $channel => $route) {
$anonymous->route($channel, $route);
}
return $anonymous;
}
}