Elgg  Version 5.1
NotificationsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
9 
17 
18  use Loggable;
19 
21  protected $queue;
22 
24  protected $elgg_events;
25 
27  protected $session;
28 
30  protected $events = [];
31 
33  protected $methods = [];
34 
42  public function __construct(
43  Queue $queue,
46  ) {
47 
48  $this->queue = $queue;
49  $this->session = $session;
50  $this->elgg_events = $elgg_events;
51  }
52 
68  public function registerEvent(string $type, string $subtype, array $actions = [], string $handler = NotificationEventHandler::class) {
69  if (!is_a($handler, NotificationEventHandler::class, true)) {
70  throw new InvalidArgumentException('$handler needs to be a ' . NotificationEventHandler::class . ' classname');
71  }
72 
73  if (!isset($this->events[$type])) {
74  $this->events[$type] = [];
75  }
76 
77  if (!isset($this->events[$type][$subtype])) {
78  $this->events[$type][$subtype] = [];
79  }
80 
81  if (empty($actions) && !array_key_exists('create', $this->events[$type][$subtype])) {
82  $actions[] = 'create';
83  }
84 
85  foreach ($actions as $action) {
86  $this->events[$type][$subtype][$action] = $handler;
87  }
88  }
89 
100  public function unregisterEvent(string $type, string $subtype, array $actions = []): void {
101 
102  if (empty($actions)) {
103  unset($this->events[$type][$subtype]);
104  }
105 
106  foreach ($actions as $action) {
107  unset($this->events[$type][$subtype][$action]);
108  }
109 
110  if (empty($this->events[$type][$subtype])) {
111  unset($this->events[$type][$subtype]);
112  }
113 
114  if (empty($this->events[$type])) {
115  unset($this->events[$type]);
116  }
117  }
118 
129  public function isRegisteredEvent(string $type, string $subtype, string $action): bool {
130  return isset($this->events[$type][$subtype][$action]);
131  }
132 
138  public function getEvents(): array {
139  return $this->events;
140  }
141 
150  public function registerMethod(string $name): void {
151  $this->methods[$name] = $name;
152  }
153 
162  public function unregisterMethod(string $name): void {
163  if (!$this->isRegisteredMethod($name)) {
164  return;
165  }
166 
167  unset($this->methods[$name]);
168  }
169 
176  public function getMethods(): array {
177  return $this->methods;
178  }
179 
187  public function isRegisteredMethod(string $method): bool {
188  return in_array($method, $this->methods);
189  }
190 
200  public function enqueueEvent(string $action, \ElggData $object, \ElggEntity $actor = null): void {
201  $object_type = $object->getType();
202  $object_subtype = $object->getSubtype();
203  $actor = $actor ?? elgg_get_logged_in_user_entity(); // default to logged in user
204  if (!isset($actor) && ($object instanceof \ElggEntity || $object instanceof \ElggExtender)) {
205  // still not set, default to the owner of $object
206  $actor = $object->getOwnerEntity() ?: null;
207  }
208 
209  $registered = $this->isRegisteredEvent($object_type, $object_subtype, $action);
210  if ($registered) {
211  $params = [
212  'action' => $action,
213  'object' => $object,
214  'actor' => $actor,
215  ];
216  $registered = (bool) $this->elgg_events->triggerResults('enqueue', 'notification', $params, $registered);
217  }
218 
219  if (!$registered) {
220  return;
221  }
222 
223  $this->elgg_events->trigger('enqueue', 'notifications', $object);
224  $this->queue->enqueue(new SubscriptionNotificationEvent($object, $action, $actor));
225  }
226 
235  $object = $event->getObject();
236  $handler = NotificationEventHandler::class;
237 
238  if (isset($this->events[$object->getType()][$object->getSubtype()][$event->getAction()])) {
239  $handler = $this->events[$object->getType()][$object->getSubtype()][$event->getAction()];
240  }
241 
242  return new $handler($event, $this);
243  }
244 
253  public function processQueue($stopTime, $matrix = false) {
254 
255  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($stopTime, $matrix) {
256  $delivery_matrix = [];
257 
258  $count = 0;
259 
260  while (time() < $stopTime) {
261  // dequeue notification event
262  $event = $this->queue->dequeue();
263  /* @var $event NotificationEvent */
264 
265  if (!$event) {
266  // queue is empty
267  break;
268  }
269 
270  if (!$event instanceof NotificationEvent || !$event->getObject() || !$event->getActor()) {
271  // event object or actor have been deleted since the event was enqueued
272  continue;
273  }
274 
275  $this->elgg_events->trigger('dequeue', 'notifications', $event->getObject());
276 
277  $handler = $this->getNotificationHandler($event);
278 
279  try {
280  $delivery_matrix[$event->getDescription()] = $handler->send();
281  $count++;
282  } catch (\Throwable $t) {
283  $this->getLogger()->error($t);
284  }
285  }
286 
287  return $matrix ? $delivery_matrix : $count;
288  });
289  }
290 
327  public function sendInstantNotifications(\ElggEntity $sender, array $recipients = [], array $params = []) {
328  if (empty($this->methods)) {
329  return [];
330  }
331 
332  $params['recipients'] = array_filter($recipients, function($e) {
333  return ($e instanceof \ElggUser);
334  });
335 
336  $object = elgg_extract('object', $params);
337  $action = elgg_extract('action', $params);
338 
339  $event = new InstantNotificationEvent($object, $action, $sender);
340 
341  $handler = new InstantNotificationEventHandler($event, $this, $params);
342 
343  return $handler->send();
344  }
345 }
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:299
getObject()
Get the object of the event.
isRegisteredEvent(string $type, string $subtype, string $action)
Check if a notification event is registered.
Exception thrown if an argument is not of the expected type.
$params
Saves global plugin settings.
Definition: save.php:13
if(($owner instanceof\ElggGroup||$owner instanceof\ElggUser)&&!in_array($owner->guid, $mute_guids)) $actor
Definition: mute.php:86
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
getType()
Return the type of the object - eg.
unregisterEvent(string $type, string $subtype, array $actions=[])
Unregister a notification event.
enqueueEvent(string $action,\ElggData $object,\ElggEntity $actor=null)
Add a notification event to the queue.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
Events service.
getNotificationHandler(NotificationEvent $event)
Returns notification event handler based on event.
getAction()
Get the name of the action.
registerMethod(string $name)
Register a delivery method for notifications.
$type
Definition: delete.php:22
Notification Event Handler for instant notifications.
getSubtype()
Return a subtype.
Queue interface.
Definition: Queue.php:11
registerEvent(string $type, string $subtype, array $actions=[], string $handler=NotificationEventHandler::class)
Register a notification event.
Elgg Session Management.
Definition: ElggSession.php:19
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
unregisterMethod(string $name)
Unregister a delivery method for notifications.
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
Notification Event Handler handles preparation of a notification.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:130
__construct(Queue $queue,\ElggSession $session, EventsService $elgg_events)
Constructor.
$count
Definition: ban.php:24
getEvents()
Return the notification events.
if(!$menu instanceof\Elgg\Menu\PreparedMenu) $actions
Definition: user_hover.php:16
getMethods()
Returns registered delivery methods for notifications.
A generic class that contains shared code among , , and .
Definition: ElggData.php:10
sendInstantNotifications(\ElggEntity $sender, array $recipients=[], array $params=[])
Notify a user via their preferences.
$action
Definition: subscribe.php:11
getLogger()
Returns logger.
Definition: Loggable.php:37
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
Notification event interface.
$subtype
Definition: delete.php:23
isRegisteredMethod(string $method)
Check if a notification method is registed.
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:24
$handler
Definition: add.php:7
processQueue($stopTime, $matrix=false)
Pull notification events from queue until stop time is reached.