You are here

ContentHubExportQueueMessageSubscriber.php in Acquia Content Hub 8

File

src/EventSubscriber/ContentHubExportQueueMessageSubscriber.php
View source
<?php

namespace Drupal\acquia_contenthub\EventSubscriber;

use Drupal\acquia_contenthub\Controller\ContentHubExportQueueController;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class ContentHubExportQueueMessageSubscriber.
 *
 * This class catches kernel.request events and displays messages from enabled
 * plugins.
 *
 * @package Drupal\admin_status
 */
class ContentHubExportQueueMessageSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The Content Hub Export Queue Controller.
   *
   * @var \Drupal\acquia_contenthub\Controller\ContentHubExportQueueController
   */
  protected $contentHubExportQueueController;

  /**
   * The Drupal Messenger Service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  private $messenger;

  /**
   * The current request path.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  private $currentPath;

  /**
   * Construct an ContentHubExportQueueMessageSubscriber object.
   *
   * @param \Drupal\acquia_contenthub\Controller\ContentHubExportQueueController $export_queue_controller
   *   The Content Hub Export Queue Controller.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   Messenger Service.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path
   *   Current Path Service.
   */
  public function __construct(ContentHubExportQueueController $export_queue_controller, MessengerInterface $messenger, CurrentPathStack $current_path) {
    $this->contentHubExportQueueController = $export_queue_controller;
    $this->messenger = $messenger;
    $this->currentPath = $current_path;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Tell Symfony that we want to hear about kernel.request events.
    $events['kernel.request'] = [
      'kernelRequest',
    ];
    return $events;
  }

  /**
   * Handles kernel.request events.
   *
   * @param \Symfony\Component\EventDispatcher\Event $event
   *   The Symfony event.
   */
  public function kernelRequest(Event $event) {

    // Get our saved config data.
    $queue_items = $this->contentHubExportQueueController
      ->getQueueCount();

    // Get current path.
    $current_path = $this->currentPath
      ->getPath();

    // If we are processing the queue, then drop warning messages so they will
    // not pile up at every queue run.
    if ($current_path === '/admin/config/services/acquia-contenthub/export-queue') {
      $this->messenger
        ->messagesByType('warning');
    }
    if ($this
      ->currentUser()
      ->hasPermission('administer acquia content hub') && $queue_items > 1) {
      $message = $this
        ->t('You have %items items in the <a href="@link">Content Hub Export Queue</a>. Make sure to export those items to keep your system up to date.', [
        '%items' => $queue_items,
        '@link' => '/admin/config/services/acquia-contenthub/export-queue',
      ]);
      $this->messenger
        ->addWarning($message);
    }
  }

  /**
   * Obtains the current user.
   *
   * @return \Drupal\Core\Session\AccountProxyInterface
   *   The current user object.
   */
  protected function currentUser() {
    return \Drupal::currentUser();
  }

}

Classes

Namesort descending Description
ContentHubExportQueueMessageSubscriber Class ContentHubExportQueueMessageSubscriber.