You are here

class PostUpdateSubscriber in Automatic Updates 8

Post update event subscriber.

Hierarchy

Expanded class hierarchy of PostUpdateSubscriber

1 string reference to 'PostUpdateSubscriber'
automatic_updates.services.yml in ./automatic_updates.services.yml
automatic_updates.services.yml
1 service uses PostUpdateSubscriber
automatic_updates.post_update_subscriber in ./automatic_updates.services.yml
Drupal\automatic_updates\EventSubscriber\PostUpdateSubscriber

File

src/EventSubscriber/PostUpdateSubscriber.php, line 17

Namespace

Drupal\automatic_updates\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
PostUpdateSubscriber::$configFactory protected property The config factory.
PostUpdateSubscriber::$entityTypeManager protected property Entity type manager.
PostUpdateSubscriber::$languageManager protected property The language manager.
PostUpdateSubscriber::$mailManager protected property Mail manager.
PostUpdateSubscriber::doSend protected function Composes and send the email message.
PostUpdateSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
PostUpdateSubscriber::onPostUpdate public function Send notification on post update with success/failure.
PostUpdateSubscriber::__construct public function PostUpdateSubscriber constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.