ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
CachedRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 
26 class CachedRepository implements Repository
27 {
28  private const REFERENCE_PROPERTIES_TABLE = 'object_reference';
29 
30  private array $data_cache = [];
31 
32  public function __construct(
33  private readonly AvailabilityPeriodRepository $time_based_activation_properties_repository,
34  private readonly \ilDBInterface $database
35  ) {
36  }
37 
38  public function getFor(?int $object_reference_id): ObjectReferenceProperties
39  {
40  if ($object_reference_id === null
41  || $object_reference_id === 0) {
42  return new ObjectReferenceProperties();
43  }
44 
45  if (!isset($this->data_cache[$object_reference_id])) {
46  $this->data_cache[$object_reference_id] = $this->retrieveDataForObjectReferenceId($object_reference_id);
47  }
48 
49  $data = $this->data_cache[$object_reference_id];
50  return new ObjectReferenceProperties(
51  $object_reference_id,
52  $data['obj_id'],
53  $data['date_of_deletion'],
54  $data['deleted_by'],
55  $this->time_based_activation_properties_repository->getFor($object_reference_id)
56  );
57  }
58 
60  AvailabilityPeriod $time_based_activation_property
61  ): void {
62  $this->time_based_activation_properties_repository->store(
63  $time_based_activation_property
64  );
65  }
66 
71  public function preload(array $object_reference_ids): void
72  {
73  $this->data_cache += $this->retrieveDataForObjectReferenceIds($object_reference_ids);
74  $this->time_based_activation_properties_repository->preload($object_reference_ids);
75  }
76 
77  public function resetPreloadedData(): void
78  {
79  $this->data_cache = [];
80  $this->reference_properties_repository->resetPreloadedData();
81  }
82 
86  protected function retrieveDataForObjectReferenceId(int $object_reference_id): array
87  {
88  $where = 'WHERE ref_id=' . $this->database->quote($object_reference_id, \ilDBConstants::T_INTEGER);
89  $data = $this->retrieveDataForWhereClause($where);
90 
91  if ($data === []) {
92  throw new \Exception('The object with the following reference_id does not exist: '
93  . (string) $object_reference_id);
94  }
95 
96  return $data[$object_reference_id];
97  }
98 
102  protected function retrieveDataForObjectReferenceIds(array $object_reference_ids): array
103  {
104  $where = 'WHERE ' . $this->database->in('ref_id', $object_reference_ids, false, \ilDBConstants::T_INTEGER);
105  return $this->retrieveDataForWhereClause($where);
106  }
107 
108  protected function retrieveDataForWhereClause(string $where): array
109  {
110  $query = 'SELECT '
111  . 'ref_id, obj_id, deleted, deleted_by' . PHP_EOL
112  . 'FROM ' . self::REFERENCE_PROPERTIES_TABLE . PHP_EOL
113  . $where;
114 
115  $statement = $this->database->query($query);
116  $num_rows = $this->database->numRows($statement);
117 
118  if ($num_rows === 0) {
119  return [];
120  }
121 
122  $data = [];
123  while ($row = $this->database->fetchAssoc($statement)) {
124  $data[$row['ref_id']] = [
125  'obj_id' => $row['obj_id'],
126  'date_of_deletion' => $row['deleted'] !== null
127  ? new \DateTimeImmutable($row['deleted'], new \DateTimeZone('UTC'))
128  : null,
129  'deleted_by' => $row['deleted_by']
130  ];
131  }
132 
133  return $data;
134  }
135 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
storePropertyAvailabilityPeriod(AvailabilityPeriod $time_based_activation_property)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct(private readonly AvailabilityPeriodRepository $time_based_activation_properties_repository, private readonly \ilDBInterface $database)