class PostUpdateSubscriber in Automatic Updates 8
Post update event subscriber.
Hierarchy
- class \Drupal\automatic_updates\EventSubscriber\PostUpdateSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of PostUpdateSubscriber
1 string reference to 'PostUpdateSubscriber'
1 service uses PostUpdateSubscriber
File
- src/
EventSubscriber/ PostUpdateSubscriber.php, line 17
Namespace
Drupal\automatic_updates\EventSubscriberView source
class PostUpdateSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Mail manager.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* PostUpdateSubscriber constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* The mail manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager.
*/
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;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
UpdateEvents::POST_UPDATE => [
'onPostUpdate',
],
];
}
/**
* Send notification on post update with success/failure.
*
* @param \Drupal\automatic_updates\Event\PostUpdateEvent $event
* The post update event.
*/
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);
}
}
}
/**
* Composes and send the email message.
*
* @param string $to
* The email address where the message will be sent.
* @param array $params
* Parameters to build the email.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
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);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PostUpdateSubscriber:: |
protected | property | The config factory. | |
PostUpdateSubscriber:: |
protected | property | Entity type manager. | |
PostUpdateSubscriber:: |
protected | property | The language manager. | |
PostUpdateSubscriber:: |
protected | property | Mail manager. | |
PostUpdateSubscriber:: |
protected | function | Composes and send the email message. | |
PostUpdateSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
PostUpdateSubscriber:: |
public | function | Send notification on post update with success/failure. | |
PostUpdateSubscriber:: |
public | function | PostUpdateSubscriber constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |