Elgg  Version 4.3
NotificationsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
10 
18 
19  use Loggable;
20 
22  protected $queue;
23 
25  protected $hooks;
26 
28  protected $elgg_events;
29 
31  protected $session;
32 
34  protected $events = [];
35 
37  protected $methods = [];
38 
47  public function __construct(
48  Queue $queue,
52  ) {
53 
54  $this->queue = $queue;
55  $this->hooks = $hooks;
56  $this->session = $session;
57  $this->elgg_events = $elgg_events;
58  }
59 
75  public function registerEvent($type, $subtype, array $actions = [], string $handler = NotificationEventHandler::class) {
76  if (!is_a($handler, NotificationEventHandler::class, true)) {
77  throw new InvalidArgumentException('$handler needs to be a ' . NotificationEventHandler::class . ' classname');
78  }
79 
80  if (!isset($this->events[$type])) {
81  $this->events[$type] = [];
82  }
83  if (!isset($this->events[$type][$subtype])) {
84  $this->events[$type][$subtype] = [];
85  }
86 
87  if (empty($actions) && !array_key_exists('create', $this->events[$type][$subtype])) {
88  $actions[] = 'create';
89  }
90 
91  foreach ($actions as $action) {
92  $this->events[$type][$subtype][$action] = $handler;
93  }
94  }
95 
106  public function unregisterEvent($type, $subtype, array $actions = []) {
107 
108  if (empty($actions)) {
109  unset($this->events[$type][$subtype]);
110  }
111 
112  foreach ($actions as $action) {
113  unset($this->events[$type][$subtype][$action]);
114  }
115 
116  if (empty($this->events[$type][$subtype])) {
117  unset($this->events[$type][$subtype]);
118  }
119 
120  if (empty($this->events[$type])) {
121  unset($this->events[$type]);
122  }
123 
124  return true;
125  }
126 
132  public function getEvents() {
133  return $this->events;
134  }
135 
144  public function registerMethod($name) {
145  $this->methods[$name] = $name;
146  }
147 
156  public function unregisterMethod($name) {
157  if ($this->isRegisteredMethod($name)) {
158  unset($this->methods[$name]);
159 
160  return true;
161  }
162 
163  return false;
164  }
165 
172  public function getMethods() {
173  return $this->methods;
174  }
175 
183  public function isRegisteredMethod(string $method): bool {
184  return in_array($method, $this->methods);
185  }
186 
197  public function enqueueEvent($action, $type, $object, \ElggEntity $actor = null): void {
198 
199  if (!$object instanceof \ElggData) {
200  return;
201  }
202 
203  $object_type = $object->getType();
204  $object_subtype = $object->getSubtype();
205  $actor = $actor ?? elgg_get_logged_in_user_entity(); // default to logged in user
206  if (!isset($actor) && ($object instanceof \ElggEntity || $object instanceof \ElggExtender)) {
207  // still not set, default to the owner of $object
208  $actor = $object->getOwnerEntity() ?: null;
209  }
210 
211  $registered = isset($this->events[$object_type][$object_subtype][$action]);
212 
213  if ($registered) {
214  $params = [
215  'action' => $action,
216  'object' => $object,
217  'actor' => $actor,
218  ];
219  $registered = (bool) $this->hooks->trigger('enqueue', 'notification', $params, $registered);
220  }
221 
222  if (!$registered) {
223  return;
224  }
225 
226  $this->elgg_events->trigger('enqueue', 'notifications', $object);
227  $this->queue->enqueue(new SubscriptionNotificationEvent($object, $action, $actor));
228  }
229 
238  $object = $event->getObject();
239  $handler = NotificationEventHandler::class;
240 
241  if (isset($this->events[$object->getType()][$object->getSubtype()][$event->getAction()])) {
242  $handler = $this->events[$object->getType()][$object->getSubtype()][$event->getAction()];
243  }
244 
245  return new $handler($event, $this);
246  }
247 
256  public function processQueue($stopTime, $matrix = false) {
257 
258  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($stopTime, $matrix) {
259  $delivery_matrix = [];
260 
261  $count = 0;
262 
263  while (time() < $stopTime) {
264  // dequeue notification event
265  $event = $this->queue->dequeue();
266  /* @var $event NotificationEvent */
267 
268  if (!$event) {
269  // queue is empty
270  break;
271  }
272 
273  if (!$event instanceof NotificationEvent || !$event->getObject() || !$event->getActor()) {
274  // event object or actor have been deleted since the event was enqueued
275  continue;
276  }
277 
278  $this->elgg_events->trigger('dequeue', 'notifications', $event->getObject());
279 
280  $handler = $this->getNotificationHandler($event);
281 
282  try {
283  $delivery_matrix[$event->getDescription()] = $handler->send();
284  $count++;
285  } catch (\Throwable $t) {
286  $this->getLogger()->error($t);
287  }
288  }
289 
290  return $matrix ? $delivery_matrix : $count;
291  });
292  }
293 
330  public function sendInstantNotifications(\ElggEntity $sender, array $recipients = [], array $params = []) {
331  if (empty($this->methods)) {
332  return [];
333  }
334 
335  $params['recipients'] = array_filter($recipients, function($e) {
336  return ($e instanceof \ElggUser);
337  });
338 
339  $object = elgg_extract('object', $params);
340  $action = elgg_extract('action', $params);
341 
342  $event = new InstantNotificationEvent($object, $action, $sender);
343 
344  $handler = new InstantNotificationEventHandler($event, $this, $params);
345 
346  return $handler->send();
347  }
348 }
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:592
getObject()
Get the object of the event.
Exception thrown if an argument is not of the expected type.
unregisterEvent($type, $subtype, array $actions=[])
Unregister a notification event.
$params
Saves global plugin settings.
Definition: save.php:13
__construct(Queue $queue, PluginHooksService $hooks,\ElggSession $session, EventsService $elgg_events)
Constructor.
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
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.
$type
Definition: delete.php:21
Notification Event Handler for instant notifications.
Queue interface.
Definition: Queue.php:11
registerMethod($name)
Register 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:146
unregisterMethod($name)
Unregister a delivery method for notifications.
$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.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
$action
Definition: subscribe.php:11
enqueueEvent($action, $type, $object,\ElggEntity $actor=null)
Add a notification event to the queue.
getLogger()
Returns logger.
Definition: Loggable.php:37
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
Notification event interface.
$subtype
Definition: delete.php:22
isRegisteredMethod(string $method)
Check if a notification method is registed.
registerEvent($type, $subtype, array $actions=[], string $handler=NotificationEventHandler::class)
Register a notification event.
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.