ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilDatabaseUpdateStepsExecutedObjective.php
Go to the documentation of this file.
1 <?php
2 
21 use ILIAS\Data;
22 
28 {
29  public const STEP_METHOD_PREFIX = "step_";
30 
32  protected string $steps_class;
33 
34  public function __construct(ilDatabaseUpdateSteps $steps)
35  {
36  $this->steps = $steps;
37  $this->steps_class = get_class($this->steps);
38  }
39 
44  final public function getHash(): string
45  {
46  return hash(
47  "sha256",
48  self::class . $this->steps_class
49  );
50  }
51 
52  final public function getLabel(): string
53  {
54  return "Database update steps in $this->steps_class.";
55  }
56 
60  final public function isNotable(): bool
61  {
62  return true;
63  }
64 
68  public function getPreconditions(Environment $environment): array
69  {
70  return [
75  ];
76  }
77 
81  public function achieve(Environment $environment): Environment
82  {
83  $execution_log = $environment->getResource(ilDatabaseUpdateStepExecutionLog::class);
84  $step_reader = $environment->getResource(ilDBStepReader::class);
85 
86  $last_started_step = $execution_log->getLastStartedStep($this->steps_class);
87  $last_finished_step = $execution_log->getLastFinishedStep($this->steps_class);
88  if ($last_started_step !== $last_finished_step) {
89  $this->throwStepNotFinishedException($last_started_step, $last_finished_step);
90  throw new LogicException(
91  "ilDatabaseUpdateStepExecutionLog::throwStepNotFinishedException should throw an exception."
92  );
93  }
94 
95  if ($last_finished_step === $step_reader->getLatestStepNumber($this->steps_class, self::STEP_METHOD_PREFIX)) {
96  return $environment;
97  }
98 
99  $db = $environment->getResource(Environment::RESOURCE_DATABASE);
100  $this->steps->prepare($db);
101 
102  $steps = $step_reader->readStepNumbers($this->steps_class, self::STEP_METHOD_PREFIX);
103  foreach ($steps as $step) {
104  if ($step <= $last_finished_step) {
105  continue;
106  }
107  $execution_log->started($this->steps_class, $step);
108  $method = self::STEP_METHOD_PREFIX . $step;
109  $this->steps->$method();
110  $execution_log->finished($this->steps_class, $step);
111  }
112 
113  return $environment;
114  }
115 
119  public function isApplicable(Environment $environment): bool
120  {
121  $execution_log = $environment->getResource(ilDatabaseUpdateStepExecutionLog::class);
122  $step_reader = $environment->getResource(ilDBStepReader::class);
123 
124  return $execution_log->getLastFinishedStep($this->steps_class) !== $step_reader->getLatestStepNumber(
125  $this->steps_class,
126  self::STEP_METHOD_PREFIX
127  );
128  }
129 
130  protected function throwStepNotFinishedException(int $started, int $finished): void
131  {
132  throw new RuntimeException(
133  "For update steps in $this->steps_class: step $started was started " .
134  "last, but step $finished was finished last. Aborting because of that " .
135  "mismatch."
136  );
137  }
138 }
An objective is a desired state of the system that is supposed to be created by the setup...
Definition: Objective.php:30
getHash()
The hash for the objective is calculated over the classname and the steps that are contained...
Builds data types.
Definition: Factory.php:35
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...