PostUpdateSubscriber.php in Automatic Updates 8
File
src/EventSubscriber/PostUpdateSubscriber.php
View source
<?php
namespace Drupal\automatic_updates\EventSubscriber;
use Drupal\automatic_updates\Event\PostUpdateEvent;
use Drupal\automatic_updates\Event\UpdateEvents;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PostUpdateSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
protected $configFactory;
protected $mailManager;
protected $languageManager;
protected $entityTypeManager;
public function __construct(ConfigFactoryInterface $config_factory, MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->configFactory = $config_factory;
$this->mailManager = $mail_manager;
$this->languageManager = $language_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function getSubscribedEvents() {
return [
UpdateEvents::POST_UPDATE => [
'onPostUpdate',
],
];
}
public function onPostUpdate(PostUpdateEvent $event) {
$notify_list = $this->configFactory
->get('update.settings')
->get('notification.emails');
if (!empty($notify_list)) {
$params['subject'] = $this
->t('Automatic update of "@project" succeeded', [
'@project' => $event
->getUpdateMetadata()
->getProjectName(),
]);
if (!$event
->success()) {
$params['subject'] = $this
->t('Automatic update of "@project" failed', [
'@project' => $event
->getUpdateMetadata()
->getProjectName(),
]);
}
$params['body'] = [
'#theme' => 'automatic_updates_post_update',
'#success' => $event
->success(),
'#metadata' => $event
->getUpdateMetadata(),
];
$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', 'post_update', $to, $params['langcode'], $params);
}
}
}