TestChecker1.php in Automatic Updates 8.2
File
tests/modules/automatic_updates_test/src/ReadinessChecker/TestChecker1.php
View source
<?php
namespace Drupal\automatic_updates_test\ReadinessChecker;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TestChecker1 implements EventSubscriberInterface {
protected const STATE_KEY = 'automatic_updates_test.checker_results';
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
public static function setTestResult($checker_results, string $event_name = AutomaticUpdatesEvents::READINESS_CHECK) : void {
$key = static::STATE_KEY . ".{$event_name}";
if (isset($checker_results)) {
\Drupal::state()
->set($key, $checker_results);
}
else {
\Drupal::state()
->delete($key);
}
}
protected function addResults(UpdateEvent $event, string $state_key) : void {
$results = $this->state
->get($state_key, []);
if ($results instanceof \Throwable) {
throw $results;
}
foreach ($results as $result) {
$event
->addValidationResult($result);
}
}
public function runPreChecks(UpdateEvent $event) : void {
$this
->addResults($event, static::STATE_KEY . "." . AutomaticUpdatesEvents::READINESS_CHECK);
}
public function runPreCommitChecks(UpdateEvent $event) : void {
$this
->addResults($event, static::STATE_KEY . "." . AutomaticUpdatesEvents::PRE_COMMIT);
}
public function runStartChecks(UpdateEvent $event) : void {
$this
->addResults($event, static::STATE_KEY . "." . AutomaticUpdatesEvents::PRE_START);
}
public static function getSubscribedEvents() {
$priority = defined('AUTOMATIC_UPDATES_TEST_SET_PRIORITY') ? AUTOMATIC_UPDATES_TEST_SET_PRIORITY : 5;
$events[AutomaticUpdatesEvents::READINESS_CHECK][] = [
'runPreChecks',
$priority,
];
$events[AutomaticUpdatesEvents::PRE_START][] = [
'runStartChecks',
$priority,
];
$events[AutomaticUpdatesEvents::PRE_COMMIT][] = [
'runPreCommitChecks',
$priority,
];
return $events;
}
}