Notify.php in Automatic Updates 8
File
src/Services/Notify.php
View source
<?php
namespace Drupal\automatic_updates\Services;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class Notify implements NotifyInterface {
use StringTranslationTrait;
protected $mailManager;
protected $automaticUpdatesPsa;
protected $configFactory;
protected $languageManager;
protected $state;
protected $time;
protected $entityTypeManager;
protected $eventDispatcher;
public function __construct(MailManagerInterface $mail_manager, AutomaticUpdatesPsaInterface $automatic_updates_psa, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, StateInterface $state, TimeInterface $time, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
$this->mailManager = $mail_manager;
$this->automaticUpdatesPsa = $automatic_updates_psa;
$this->configFactory = $config_factory;
$this->languageManager = $language_manager;
$this->state = $state;
$this->time = $time;
$this->entityTypeManager = $entity_type_manager;
$this->stringTranslation = $string_translation;
}
public function send() {
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);
}
}
}
}
protected function doSend($to, array $params) {
$users = $this->entityTypeManager
->getStorage('user')
->loadByProperties([
'mail' => $to,
]);
foreach ($users as $user) {
$to_user = reset($users);
$params['langcode'] = $to_user
->getPreferredLangcode();
$this->mailManager
->mail('automatic_updates', 'notify', $to, $params['langcode'], $params);
}
}
}