class RequestSubscriber in TMGMT Translator Smartling 8.4
Same name and namespace in other branches
- 8.2 src/EventSubscriber/RequestSubscriber.php \Drupal\tmgmt_smartling\EventSubscriber\RequestSubscriber
- 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
- class \Drupal\tmgmt_smartling\EventSubscriber\RequestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of RequestSubscriber
1 string reference to 'RequestSubscriber'
1 service uses RequestSubscriber
File
- src/
EventSubscriber/ RequestSubscriber.php, line 20
Namespace
Drupal\tmgmt_smartling\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RequestSubscriber:: |
private | property | ||
RequestSubscriber:: |
private | property | ||
RequestSubscriber:: |
constant | |||
RequestSubscriber:: |
public static | function | ||
RequestSubscriber:: |
public | function | Reacts on page load event. | |
RequestSubscriber:: |
constant | |||
RequestSubscriber:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |