You are here

final class ReadinessRequirements in Automatic Updates 8.2

Class for generating the readiness checkers' output for hook_requirements().

@internal This class implements logic to output the messages from readiness checkers on the status report page. It should not be called directly.

Hierarchy

Expanded class hierarchy of ReadinessRequirements

See also

automatic_updates_requirements()

1 file declares its use of ReadinessRequirements
automatic_updates.install in ./automatic_updates.install
Contains install and update functions for Automatic Updates.

File

src/Validation/ReadinessRequirements.php, line 23

Namespace

Drupal\automatic_updates\Validation
View source
final class ReadinessRequirements implements ContainerInjectionInterface {
  use StringTranslationTrait;
  use ReadinessTrait;

  /**
   * The readiness checker manager.
   *
   * @var \Drupal\automatic_updates\Validation\ReadinessValidationManager
   */
  protected $readinessCheckerManager;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Constructor ReadinessRequirement object.
   *
   * @param \Drupal\automatic_updates\Validation\ReadinessValidationManager $readiness_checker_manager
   *   The readiness checker manager service.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The translation service.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(ReadinessValidationManager $readiness_checker_manager, TranslationInterface $translation, DateFormatterInterface $date_formatter) {
    $this->readinessCheckerManager = $readiness_checker_manager;
    $this
      ->setStringTranslation($translation);
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) : self {
    return new static($container
      ->get('automatic_updates.readiness_validation_manager'), $container
      ->get('string_translation'), $container
      ->get('date.formatter'));
  }

  /**
   * Gets requirements arrays as specified in hook_requirements().
   *
   * @return array[]
   *   Requirements arrays as specified by hook_requirements().
   */
  public function getRequirements() : array {
    $run_link = $this
      ->createRunLink();
    $last_check_timestamp = $this->readinessCheckerManager
      ->getLastRunTime();
    if ($last_check_timestamp === NULL) {
      $requirement['title'] = $this
        ->t('Update readiness checks');
      $requirement['severity'] = SystemManager::REQUIREMENT_WARNING;

      // @todo Link "automatic updates" to documentation in
      //   https://www.drupal.org/node/3168405.
      $requirement['value'] = $this
        ->t('Your site has never checked if it is ready to apply automatic updates.');
      if ($run_link) {
        $requirement['description'] = $run_link;
      }
      return [
        'automatic_updates_readiness' => $requirement,
      ];
    }
    else {
      $results = $this->readinessCheckerManager
        ->runIfNoStoredResults()
        ->getResults();
      $requirements = [];
      if (empty($results)) {
        $requirements['automatic_updates_readiness'] = [
          'title' => $this
            ->t('Update readiness checks'),
          'severity' => SystemManager::REQUIREMENT_OK,
          // @todo Link "automatic updates" to documentation in
          //   https://www.drupal.org/node/3168405.
          'value' => $this
            ->t('Your site is ready for automatic updates.'),
        ];
        if ($run_link) {
          $requirements['automatic_updates_readiness']['description'] = $run_link;
        }
      }
      else {
        foreach ([
          SystemManager::REQUIREMENT_WARNING,
          SystemManager::REQUIREMENT_ERROR,
        ] as $severity) {
          if ($requirement = $this
            ->createRequirementForSeverity($severity)) {
            $requirements["automatic_updates_readiness_{$severity}"] = $requirement;
          }
        }
      }
      return $requirements;
    }
  }

  /**
   * Creates a requirement for checker results of a specific severity.
   *
   * @param int $severity
   *   The severity for requirement. Should be one of the
   *   SystemManager::REQUIREMENT_* constants.
   *
   * @return mixed[]|null
   *   Requirements array as specified by hook_requirements(), or NULL
   *   if no requirements can be determined.
   */
  protected function createRequirementForSeverity(int $severity) : ?array {
    $severity_messages = [];
    $results = $this->readinessCheckerManager
      ->getResults($severity);
    if (!$results) {
      return NULL;
    }
    foreach ($results as $result) {
      $checker_messages = $result
        ->getMessages();
      if (count($checker_messages) === 1) {
        $severity_messages[] = [
          '#markup' => array_pop($checker_messages),
        ];
      }
      else {
        $severity_messages[] = [
          '#type' => 'details',
          '#title' => $result
            ->getSummary(),
          '#open' => FALSE,
          'messages' => [
            '#theme' => 'item_list',
            '#items' => $checker_messages,
          ],
        ];
      }
    }
    $requirement = [
      'title' => $this
        ->t('Update readiness checks'),
      'severity' => $severity,
      'value' => $this
        ->getFailureMessageForSeverity($severity),
      'description' => [
        'messages' => $severity_messages,
      ],
    ];
    if ($run_link = $this
      ->createRunLink()) {
      $requirement['description']['run_link'] = [
        '#type' => 'container',
        '#markup' => $run_link,
      ];
    }
    return $requirement;
  }

  /**
   * Creates a link to run the readiness checkers.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup|null
   *   A link, if the user has access to run the readiness checkers, otherwise
   *   NULL.
   */
  protected function createRunLink() : ?TranslatableMarkup {
    $readiness_check_url = Url::fromRoute('automatic_updates.update_readiness');
    if ($readiness_check_url
      ->access()) {
      return $this
        ->t('<a href=":link">Run readiness checks</a> now.', [
        ':link' => $readiness_check_url
          ->toString(),
      ]);
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReadinessRequirements::$dateFormatter protected property The date formatter service.
ReadinessRequirements::$readinessCheckerManager protected property The readiness checker manager.
ReadinessRequirements::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ReadinessRequirements::createRequirementForSeverity protected function Creates a requirement for checker results of a specific severity.
ReadinessRequirements::createRunLink protected function Creates a link to run the readiness checkers.
ReadinessRequirements::getRequirements public function Gets requirements arrays as specified in hook_requirements().
ReadinessRequirements::__construct public function Constructor ReadinessRequirement object.
ReadinessTrait::getFailureMessageForSeverity protected function Gets a message, based on severity, when readiness checkers fail.
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.