You are here

public function Notify::send in Automatic Updates 8

Send notification when PSAs are available.

Overrides NotifyInterface::send

File

src/Services/Notify.php, line 111

Class

Notify
Class EmailNotify.

Namespace

Drupal\automatic_updates\Services

Code

public function send() {

  // Don't send mail if notifications are disabled.
  if (!$this->configFactory
    ->get('automatic_updates.settings')
    ->get('notify')) {
    return;
  }
  $messages = $this->automaticUpdatesPsa
    ->getPublicServiceMessages();
  if (!$messages) {
    return;
  }
  $notify_list = $this->configFactory
    ->get('update.settings')
    ->get('notification.emails');
  if (!empty($notify_list)) {
    $frequency = $this->configFactory
      ->get('automatic_updates.settings')
      ->get('check_frequency');
    $last_check = $this->state
      ->get('automatic_updates.notify_last_check') ?: 0;
    if ($this->time
      ->getRequestTime() - $last_check > $frequency) {
      $this->state
        ->set('automatic_updates.notify_last_check', $this->time
        ->getRequestTime());
      $params['subject'] = new PluralTranslatableMarkup(count($messages), '@count urgent Drupal announcement requires your attention for @site_name', '@count urgent Drupal announcements require your attention for @site_name', [
        '@site_name' => $this->configFactory
          ->get('system.site')
          ->get('name'),
      ]);
      $params['body'] = [
        '#theme' => 'automatic_updates_psa_notify',
        '#messages' => $messages,
      ];
      $default_langcode = $this->languageManager
        ->getDefaultLanguage()
        ->getId();
      $params['langcode'] = $default_langcode;
      foreach ($notify_list as $to) {
        $this
          ->doSend($to, $params);
      }
    }
  }
}