You are here

class RequestSubscriber in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/RequestSubscriber.php \Drupal\tmgmt_smartling\EventSubscriber\RequestSubscriber
  2. 8.3 src/EventSubscriber/RequestSubscriber.php \Drupal\tmgmt_smartling\EventSubscriber\RequestSubscriber

Class RequestSubscriber.

Needed for health check and show warning message.

@package Drupal\tmgmt_smartling\EventSubscriber

Hierarchy

Expanded class hierarchy of RequestSubscriber

1 string reference to 'RequestSubscriber'
tmgmt_smartling.services.yml in ./tmgmt_smartling.services.yml
tmgmt_smartling.services.yml
1 service uses RequestSubscriber
tmgmt_smartling.subscriber.request in ./tmgmt_smartling.services.yml
\Drupal\tmgmt_smartling\EventSubscriber\RequestSubscriber

File

src/EventSubscriber/RequestSubscriber.php, line 20

Namespace

Drupal\tmgmt_smartling\EventSubscriber
View source
class RequestSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;
  const CRON_LAST_RUN_THRESHOLD = 600;
  const QUEUE_THRESHOLD = 250;

  /**
   * @var QueueFactory
   */
  private $queueFactory;

  /**
   * @var AccountInterface
   */
  private $user;
  public function __construct(QueueFactory $queue, AccountInterface $user) {
    $this->queueFactory = $queue;
    $this->user = $user;
  }

  /**
   * Reacts on page load event.
   */
  public function init() {
    $http_x_requested_with = \Drupal::request()->server
      ->get('HTTP_X_REQUESTED_WITH');
    if (empty($http_x_requested_with) && $this->user
      ->hasPermission('see smartling messages')) {
      $last_cron_run = time() - \Drupal::state()
        ->get('system.cron_last');
      $show_cron_message = FALSE;
      $show_queue_message = FALSE;

      // Check last cron run time.
      if ($last_cron_run > static::CRON_LAST_RUN_THRESHOLD) {
        $show_cron_message = TRUE;
      }

      // Check amount of queue items in each queue.
      $queues = SmartlingTranslatorUi::getSmartlingQueuesDefinitions();
      foreach ($queues as $name => $queue_definition) {
        $queue = $this->queueFactory
          ->get($name);
        $items = $queue
          ->numberOfItems();
        if ($items > static::QUEUE_THRESHOLD) {
          $show_queue_message = TRUE;
          break;
        }
      }

      // Assemble warning message.
      $message = NULL;
      if ($show_cron_message) {
        $message = $this
          ->t('Last cron run happened more than 10 minutes ago.');
      }
      if ($show_queue_message) {
        $sub_message = $this
          ->t('Some of the Smartling cron queues are overflowed.');
        $message = empty($message) ? $sub_message : $message . ' ' . $sub_message;
      }
      if (!empty($message)) {
        $message .= ' ' . $this
          ->t('Configure your cron job to run once per 5-10 minutes in order to process Smartling queues more effectively. Please visit this <a href="http://docs.drush.org/en/master/cron/" target="_blank">page</a> for more information.');
        \Drupal::messenger()
          ->addWarning(new TranslatableMarkup($message));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'init',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequestSubscriber::$queueFactory private property
RequestSubscriber::$user private property
RequestSubscriber::CRON_LAST_RUN_THRESHOLD constant
RequestSubscriber::getSubscribedEvents public static function
RequestSubscriber::init public function Reacts on page load event.
RequestSubscriber::QUEUE_THRESHOLD constant
RequestSubscriber::__construct public function
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.