Elgg\Notifications\NotificationsService::sendNotification PHP Method

sendNotification() protected method

Send a notification to a subscriber
protected sendNotification ( Elgg\Notifications\NotificationEvent $event, integer $guid, string $method, array $params = [] ) : boolean
$event Elgg\Notifications\NotificationEvent The notification event
$guid integer The guid of the subscriber
$method string The notification method
$params array Default notification params
return boolean
    protected function sendNotification(NotificationEvent $event, $guid, $method, array $params = [])
    {
        $actor = $event->getActor();
        $object = $event->getObject();
        if ($event instanceof InstantNotificationEvent) {
            $recipient = $this->entities->get($guid);
            /* @var \ElggEntity $recipient */
            $subject = elgg_extract('subject', $params, '');
            $body = elgg_extract('body', $params, '');
            $summary = elgg_extract('summary', $params, '');
        } else {
            $recipient = $this->entities->get($guid, 'user');
            /* @var \ElggUser $recipient */
            if (!$recipient || $recipient->isBanned()) {
                return false;
            }
            if ($recipient->getGUID() == $event->getActorGUID()) {
                // Content creators should not be receiving subscription
                // notifications about their own content
                return false;
            }
            if (!$actor || !$object) {
                return false;
            }
            if ($object instanceof ElggEntity && !has_access_to_entity($object, $recipient)) {
                // Recipient does not have access to the notification object
                // The access level may have changed since the event was enqueued
                return false;
            }
            $subject = $this->getNotificationSubject($event, $recipient);
            $body = $this->getNotificationBody($event, $recipient);
            $summary = '';
            $params['origin'] = Notification::ORIGIN_SUBSCRIPTIONS;
        }
        $language = $recipient->language;
        $params['event'] = $event;
        $params['method'] = $method;
        $params['sender'] = $actor;
        $params['recipient'] = $recipient;
        $params['language'] = $language;
        $params['object'] = $object;
        $params['action'] = $event->getAction();
        $notification = new Notification($actor, $recipient, $language, $subject, $body, $summary, $params);
        $notification = $this->hooks->trigger('prepare', 'notification', $params, $notification);
        if (!$notification instanceof Notification) {
            throw new RuntimeException("'prepare','notification' hook must return an instance of " . Notification::class);
        }
        $type = 'notification:' . $event->getDescription();
        if ($this->hooks->hasHandler('prepare', $type)) {
            $notification = $this->hooks->trigger('prepare', $type, $params, $notification);
            if (!$notification instanceof Notification) {
                throw new RuntimeException("'prepare','{$type}' hook must return an instance of " . Notification::class);
            }
        } else {
            // pre Elgg 1.9 notification message generation
            $notification = $this->getDeprecatedNotificationBody($notification, $event, $method);
        }
        $notification = $this->hooks->trigger('format', "notification:{$method}", [], $notification);
        if (!$notification instanceof Notification) {
            throw new RuntimeException("'format','notification:{$method}' hook must return an instance of " . Notification::class);
        }
        if ($this->hooks->hasHandler('send', "notification:{$method}")) {
            // return true to indicate the notification has been sent
            $params = array('notification' => $notification, 'event' => $event);
            $result = $this->hooks->trigger('send', "notification:{$method}", $params, false);
            if ($this->logger->getLevel() == Logger::INFO) {
                $logger_data = print_r((array) $notification->toObject(), true);
                if ($result) {
                    $this->logger->info("Notification sent: " . $logger_data);
                } else {
                    $this->logger->info("Notification was not sent: " . $logger_data);
                }
            }
            return $result;
        } else {
            // pre Elgg 1.9 notification handler
            $userGuid = $notification->getRecipientGUID();
            $senderGuid = $notification->getSenderGUID();
            $subject = $notification->subject;
            $body = $notification->body;
            $params = $notification->params;
            return (bool) _elgg_notify_user($userGuid, $senderGuid, $subject, $body, $params, array($method));
        }
    }