You are here

public function RequestSubscriber::init in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 src/EventSubscriber/RequestSubscriber.php \Drupal\tmgmt_smartling\EventSubscriber\RequestSubscriber::init()
  2. 8.2 src/EventSubscriber/RequestSubscriber.php \Drupal\tmgmt_smartling\EventSubscriber\RequestSubscriber::init()

Reacts on page load event.

File

src/EventSubscriber/RequestSubscriber.php, line 45

Class

RequestSubscriber
Class RequestSubscriber.

Namespace

Drupal\tmgmt_smartling\EventSubscriber

Code

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();

    // Don't take into consideration check status queue. It might be big.
    unset($queues['tmgmt_extension_suit_check_status']);
    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_set_message(new TranslatableMarkup($message), 'warning');
    }
  }
}