You are here

public function DigestManager::processSingleUserDigest in Message Digest 8

Processes and sends an individual digest for a given user.

Parameters

int $account_id: The recipient's account ID.

string $notifier_id: The digest notifier plugin ID.

int $end_time: The unix timestamp prior to which to aggregate digests.

Overrides DigestManagerInterface::processSingleUserDigest

File

src/DigestManager.php, line 196

Class

DigestManager
Digest manager service.

Namespace

Drupal\message_digest

Code

public function processSingleUserDigest($account_id, $notifier_id, $end_time) {

  /** @var \Drupal\user\UserInterface $account */
  $account = $this->entityTypeManager
    ->getStorage('user')
    ->load($account_id);

  // If the user has been deleted, do not attempt to send any messages.
  if (empty($account)) {
    return;
  }

  /** @var \Drupal\message_digest\Plugin\Notifier\DigestInterface $notifier */
  $notifier = $this->notifierManager
    ->createInstance($notifier_id);
  assert($notifier instanceof DigestInterface, 'Notifier ID ' . $notifier_id . ' is not an instance of DigestInterface.');
  $plugin_definition = $notifier
    ->getPluginDefinition();
  $view_modes = array_combine($plugin_definition['viewModes'], $plugin_definition['viewModes']);
  $digests = $notifier
    ->aggregate($account_id, $end_time);
  $max_mid = 0;
  foreach ($digests as $entity_type => $entity_ids) {
    foreach ($entity_ids as $entity_id => $message_ids) {
      $last_mid = max($message_ids);
      $max_mid = $last_mid > $max_mid ? $last_mid : $max_mid;

      // Load up the messages.
      $messages = $this->entityTypeManager
        ->getStorage('message')
        ->loadMultiple($message_ids);
      if (empty($messages)) {
        continue;
      }
      $context = [
        'deliver' => TRUE,
        'entity_type' => $entity_type,
        'entity_id' => $entity_id,
        'messages' => $messages,
        'notifier_id' => $notifier_id,
        'view_modes' => $view_modes,
      ];
      $this->moduleHandler
        ->alter('message_digest_aggregate', $context, $account, $notifier);
      $this->moduleHandler
        ->alter('message_digest_view_mode', $context, $notifier, $account);
      if ($context['deliver']) {
        $formatted_messages = $this->formatter
          ->format($context['messages'], $context['view_modes'], $account);
        $this
          ->deliverDigest($account, $context['entity_type'], $context['entity_id'], $notifier, $formatted_messages);
      }
    }
  }
  $notifier
    ->markSent($account, $max_mid);
}