ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilDBStepExecutionDB.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
27 {
28  public const TABLE_NAME = "il_db_steps";
29 
30  public const FIELD_CLASS = "class";
31  public const FIELD_STEP = "step";
32  public const FIELD_STARTED = "started";
33  public const FIELD_FINISHED = "finished";
34 
35  protected ilDBInterface $db;
36  protected $get_now;
37 
42  public function __construct(ilDBInterface $db, callable $get_now)
43  {
44  $this->db = $db;
45  $this->get_now = $get_now;
46  }
47 
51  public function started(string $class, int $step): void
52  {
53  $this->throwIfClassNameTooLong($class);
54 
55  $last_started_step = $this->getLastStartedStep($class);
56  if ($last_started_step >= $step) {
57  throw new \RuntimeException(
58  "The last started step for $class was $last_started_step, which" .
59  " is higher then the step $step started now."
60  );
61  }
62 
63  $last_finished_step = $this->getLastFinishedStep($class);
64  if ($last_started_step !== $last_finished_step) {
65  throw new \RuntimeException(
66  "Step $step should be started for $class, but last step $last_started_step " .
67  "has not finished by now."
68  );
69  }
70 
71  $this->db->insert(
72  self::TABLE_NAME,
73  [
74  self::FIELD_CLASS => ["text", $class],
75  self::FIELD_STEP => ["integer", $step],
76  self::FIELD_STARTED => ["text", $this->getFormattedNow()]
77  ]
78  );
79  }
80 
84  public function finished(string $class, int $step): void
85  {
86  $this->throwIfClassNameTooLong($class);
87 
88  $last_started_step = $this->getLastStartedStep($class);
89  if ($last_started_step != $step) {
90  throw new \RuntimeException(
91  "The step $step for $class is supposed to be finished, but" .
92  " $last_started_step was $step started last."
93  );
94  }
95 
96  $this->db->update(
97  self::TABLE_NAME,
98  [
99  self::FIELD_FINISHED => ["text", $this->getFormattedNow()]
100  ],
101  [
102  self::FIELD_CLASS => ["text", $class],
103  self::FIELD_STEP => ["integer", $step]
104  ]
105  );
106  }
107 
108  public function getLastStartedStep(string $class): int
109  {
110  $this->throwIfClassNameTooLong($class);
111 
112  $res = $this->db->query(
113  "SELECT MAX(" . self::FIELD_STEP . ") AS " . self::FIELD_STEP .
114  " FROM " . self::TABLE_NAME .
115  " WHERE " . self::FIELD_CLASS . " = " . $this->db->quote($class, "text")
116  );
117 
118  $row = $this->db->fetchAssoc($res);
119  return (int) ($row[self::FIELD_STEP] ?? 0);
120  }
121 
122  public function getLastFinishedStep(string $class): int
123  {
124  $this->throwIfClassNameTooLong($class);
125 
126  $res = $this->db->query(
127  "SELECT MAX(" . self::FIELD_STEP . ") AS " . self::FIELD_STEP .
128  " FROM " . self::TABLE_NAME .
129  " WHERE " . self::FIELD_CLASS . " = " . $this->db->quote($class, "text") .
130  " AND " . self::FIELD_FINISHED . " IS NOT NULL"
131  );
132 
133  $row = $this->db->fetchAssoc($res);
134  return (int) ($row[self::FIELD_STEP] ?? 0);
135  }
136 
137  protected function throwIfClassNameTooLong(string $class): void
138  {
139  if (strlen($class) > 200) {
140  throw new \InvalidArgumentException(
141  "This ilDatabaseUpdateStepExecutionLog only supports class names up to 200 chars."
142  );
143  }
144  }
145 
146  protected function getFormattedNow(): string
147  {
148  $now = ($this->get_now)();
149  if (!($now instanceof \DateTime)) {
150  throw new \LogicException(
151  "Expected \$get_now to return a DateTime."
152  );
153  }
154  return $now->format("Y-m-d H:i:s.u");
155  }
156 }
started(string $class, int $step)
$res
Definition: ltiservices.php:69
finished(string $class, int $step)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getLastFinishedStep(string $class)
Returns 0 as "first" step.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getLastStartedStep(string $class)
Returns 0 as "first" step.
__construct(ilDBInterface $db, callable $get_now)