RequestSubscriber.php in TMGMT Translator Smartling 8.3
File
src/EventSubscriber/RequestSubscriber.php
View source
<?php
namespace Drupal\tmgmt_smartling\EventSubscriber;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\tmgmt_smartling\SmartlingTranslatorUi;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class RequestSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
const CRON_LAST_RUN_THRESHOLD = 600;
const QUEUE_THRESHOLD = 250;
private $queueFactory;
private $user;
public function __construct(QueueFactory $queue, AccountInterface $user) {
$this->queueFactory = $queue;
$this->user = $user;
}
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;
if ($last_cron_run > static::CRON_LAST_RUN_THRESHOLD) {
$show_cron_message = TRUE;
}
$queues = SmartlingTranslatorUi::getSmartlingQueuesDefinitions();
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;
}
}
$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');
}
}
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'init',
];
return $events;
}
}