You are here

class ReadinessCheckerManager in Automatic Updates 8

Defines a chained readiness checker implementation combining multiple checks.

Hierarchy

Expanded class hierarchy of ReadinessCheckerManager

1 string reference to 'ReadinessCheckerManager'
automatic_updates.services.yml in ./automatic_updates.services.yml
automatic_updates.services.yml
1 service uses ReadinessCheckerManager
automatic_updates.readiness_checker in ./automatic_updates.services.yml
Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManager

File

src/ReadinessChecker/ReadinessCheckerManager.php, line 11

Namespace

Drupal\automatic_updates\ReadinessChecker
View source
class ReadinessCheckerManager implements ReadinessCheckerManagerInterface {

  /**
   * The key/value storage.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected $keyValue;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * An unsorted array of active checkers.
   *
   * The keys are category, next level is integers that indicate priority.
   * Values are arrays of ReadinessCheckerInterface objects.
   *
   * @var \Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerInterface[][][]
   */
  protected $checkers = [];

  /**
   * ReadinessCheckerManager constructor.
   *
   * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value
   *   The key/value service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(KeyValueFactoryInterface $key_value, ConfigFactoryInterface $config_factory) {
    $this->keyValue = $key_value
      ->get('automatic_updates');
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function addChecker(ReadinessCheckerInterface $checker, $category = 'warning', $priority = 0) {
    if (!in_array($category, $this
      ->getCategories(), TRUE)) {
      throw new \InvalidArgumentException(sprintf('Readiness checker category "%s" is invalid. Use "%s" instead.', $category, implode('" or "', $this
        ->getCategories())));
    }
    $this->checkers[$category][$priority][] = $checker;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function run($category) {
    $messages = [];
    if (!$this
      ->isEnabled()) {
      return $messages;
    }
    if (!isset($this
      ->getSortedCheckers()[$category])) {
      throw new \InvalidArgumentException(sprintf('No readiness checkers exist of category "%s"', $category));
    }
    foreach ($this
      ->getSortedCheckers()[$category] as $checker) {
      $messages[] = $checker
        ->run();
    }
    $messages = array_merge(...$messages);
    $this->keyValue
      ->set("readiness_check_results.{$category}", $messages);
    $this->keyValue
      ->set('readiness_check_timestamp', \Drupal::time()
      ->getRequestTime());
    return $messages;
  }

  /**
   * {@inheritdoc}
   */
  public function getResults($category) {
    $results = [];
    if ($this
      ->isEnabled()) {
      $results = $this->keyValue
        ->get("readiness_check_results.{$category}", []);
    }
    return $results;
  }

  /**
   * {@inheritdoc}
   */
  public function timestamp() {
    $last_check_timestamp = $this->keyValue
      ->get('readiness_check_timestamp');
    if (!is_numeric($last_check_timestamp)) {
      $last_check_timestamp = \Drupal::state()
        ->get('install_time', 0);
    }
    return $last_check_timestamp;
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled() {
    return $this->configFactory
      ->get('automatic_updates.settings')
      ->get('enable_readiness_checks');
  }

  /**
   * {@inheritdoc}
   */
  public function getCategories() {
    return [
      self::ERROR,
      self::WARNING,
    ];
  }

  /**
   * Sorts checkers according to priority.
   *
   * @return \Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerInterface[]
   *   A sorted array of checker objects.
   */
  protected function getSortedCheckers() {
    $sorted = [];
    foreach ($this->checkers as $category => $priorities) {
      foreach ($priorities as $checkers) {
        krsort($checkers);
        $sorted[$category][] = $checkers;
      }
      $sorted[$category] = array_merge(...$sorted[$category]);
    }
    return $sorted;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReadinessCheckerManager::$checkers protected property An unsorted array of active checkers.
ReadinessCheckerManager::$configFactory protected property The config factory.
ReadinessCheckerManager::$keyValue protected property The key/value storage.
ReadinessCheckerManager::addChecker public function Appends a checker to the checker chain. Overrides ReadinessCheckerManagerInterface::addChecker
ReadinessCheckerManager::getCategories public function Get the categories of checkers. Overrides ReadinessCheckerManagerInterface::getCategories
ReadinessCheckerManager::getResults public function Get results of most recent run. Overrides ReadinessCheckerManagerInterface::getResults
ReadinessCheckerManager::getSortedCheckers protected function Sorts checkers according to priority.
ReadinessCheckerManager::isEnabled public function Determine if readiness checks is enabled. Overrides ReadinessCheckerManagerInterface::isEnabled
ReadinessCheckerManager::run public function Run checks. Overrides ReadinessCheckerManagerInterface::run
ReadinessCheckerManager::timestamp public function Get timestamp of most recent run. Overrides ReadinessCheckerManagerInterface::timestamp
ReadinessCheckerManager::__construct public function ReadinessCheckerManager constructor.
ReadinessCheckerManagerInterface::ERROR constant Error category.
ReadinessCheckerManagerInterface::LAST_CHECKED_WARNING constant Last checked ago warning (in seconds).
ReadinessCheckerManagerInterface::WARNING constant Warning category.