AdminReadinessMessages.php in Automatic Updates 8.2
File
src/Validation/AdminReadinessMessages.php
View source
<?php
namespace Drupal\automatic_updates\Validation;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\system\SystemManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class AdminReadinessMessages implements ContainerInjectionInterface {
use MessengerTrait;
use StringTranslationTrait;
use RedirectDestinationTrait;
use ReadinessTrait;
protected $readinessCheckerManager;
protected $adminContext;
protected $currentUser;
protected $currentRouteMatch;
public function __construct(ReadinessValidationManager $readiness_checker_manager, MessengerInterface $messenger, AdminContext $admin_context, AccountProxyInterface $current_user, TranslationInterface $translation, CurrentRouteMatch $current_route_match) {
$this->readinessCheckerManager = $readiness_checker_manager;
$this
->setMessenger($messenger);
$this->adminContext = $admin_context;
$this->currentUser = $current_user;
$this
->setStringTranslation($translation);
$this->currentRouteMatch = $current_route_match;
}
public static function create(ContainerInterface $container) : self {
return new static($container
->get('automatic_updates.readiness_validation_manager'), $container
->get('messenger'), $container
->get('router.admin_context'), $container
->get('current_user'), $container
->get('string_translation'), $container
->get('current_route_match'));
}
public function displayAdminPageMessages() : void {
if (!$this
->displayResultsOnCurrentPage()) {
return;
}
if ($this->readinessCheckerManager
->getResults() === NULL) {
$checker_url = Url::fromRoute('automatic_updates.update_readiness')
->setOption('query', $this
->getDestinationArray());
if ($checker_url
->access()) {
$this
->messenger()
->addError($this
->t('Your site has not recently run an update readiness check. <a href=":url">Run readiness checks now.</a>', [
':url' => $checker_url
->toString(),
]));
}
}
else {
if (!$this
->displayResultsForSeverity(SystemManager::REQUIREMENT_ERROR)) {
$this
->displayResultsForSeverity(SystemManager::REQUIREMENT_WARNING);
}
}
}
protected function displayResultsOnCurrentPage() : bool {
if ($this->adminContext
->isAdminRoute() && $this->currentUser
->hasPermission('administer site configuration')) {
$disabled_routes = [
'update.theme_update',
'system.theme_install',
'update.module_update',
'update.module_install',
'update.status',
'update.report_update',
'update.report_install',
'update.settings',
'system.status',
'update.confirmation_page',
];
return !in_array($this->currentRouteMatch
->getRouteName(), $disabled_routes, TRUE);
}
return FALSE;
}
protected function displayResultsForSeverity(int $severity) : bool {
$results = $this->readinessCheckerManager
->getResults($severity);
if (empty($results)) {
return FALSE;
}
$failure_message = $this
->getFailureMessageForSeverity($severity);
if ($severity === SystemManager::REQUIREMENT_ERROR) {
$this
->messenger()
->addError($failure_message);
}
else {
$this
->messenger()
->addWarning($failure_message);
}
foreach ($results as $result) {
$messages = $result
->getMessages();
$message = count($messages) === 1 ? $messages[0] : $result
->getSummary();
if ($severity === SystemManager::REQUIREMENT_ERROR) {
$this
->messenger()
->addError($message);
}
else {
$this
->messenger()
->addWarning($message);
}
}
return TRUE;
}
}