ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
UploadPolicyDBRepository.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
24 {
25  protected const MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s';
26  protected const MYSQL_DATE_FORMAT = 'Y-m-d';
27 
28  public function __construct(protected readonly ilDBInterface $db)
29  {
30  }
31 
32  public function store(UploadPolicy $policy): void
33  {
34  $data_for_storage = [
35  "title" => ['text', $policy->getTitle()],
36  "upload_limit_in_mb" => ['integer', $policy->getUploadLimitInMB()],
37  "audience" => ['text', json_encode($policy->getAudience(), JSON_THROW_ON_ERROR)],
38  "audience_type" => ['integer', $policy->getAudienceType()],
39  "scope_definition" => ['text', $policy->getScopeDefinition()],
40  "active" => ['integer', $policy->isActive()],
41  "valid_from" => ['date', (null !== ($from = $policy->getValidFrom())) ? $this->getDateString($from) : null],
42  "valid_until" => [
43  'date',
44  (null !== ($until = $policy->getValidUntil())) ? $this->getDateString($until) : null
45  ],
46  "owner" => ['integer', $policy->getOwnerId()],
47  "last_update" => ['timestamp', $this->getDateTimeString($policy->getLastUpdate())]
48  ];
49 
50  if (null !== ($policy_id = $policy->getPolicyId()) && null !== $this->get($policy_id)) {
51  // UPDATE
52  $this->db->update(
53  "il_upload_policy",
54  $data_for_storage,
55  ["policy_id" => ['integer', $policy_id]]
56  );
57  } else {
58  // CREATE
59  $data_for_storage["policy_id"] = ['integer', $this->db->nextId("il_upload_policy")];
60  $data_for_storage["create_date"] = ['timestamp', $this->getDateTimeString($policy->getCreateDate())];
61 
62  $this->db->insert("il_upload_policy", $data_for_storage);
63  }
64  }
65 
66  public function get(int $policy_id): ?UploadPolicy
67  {
68  $query = "SELECT * FROM il_upload_policy WHERE policy_id = %s";
69  $result = $this->db->queryF($query, ['integer'], [$policy_id]);
70  if (null !== ($dataset = $this->db->fetchObject($result))) {
71  return $this->transformToDtoOrAbort($dataset);
72  }
73 
74  return null;
75  }
76 
80  public function getAll(): array
81  {
82  $query = "SELECT * FROM il_upload_policy";
83  $result = $this->db->query($query);
84 
85  $upload_policies = [];
86  while (null !== ($dataset = $this->db->fetchObject($result))) {
87  $upload_policies[] = $this->transformToDtoOrAbort($dataset);
88  }
89 
90  return $upload_policies;
91  }
92 
93  public function delete(UploadPolicy $policy): void
94  {
95  if (null === ($policy_id = $policy->getPolicyId())) {
96  return;
97  }
98 
99  $query = "DELETE FROM il_upload_policy WHERE policy_id = %s";
100  $this->db->manipulateF($query, ['integer'], [$policy_id]);
101  }
102 
106  protected function transformToDtoOrAbort(stdClass $dataset): UploadPolicy
107  {
108  $audience_json = $dataset?->audience ?? $this->missingRequiredField('audience');
109 
110  $valid_from = (null !== ($from = $dataset?->valid_from)) ? $this->getDateObject((string) $from) : null;
111  $valid_until = (null !== ($until = $dataset?->valid_until)) ? $this->getDateObject((string) $until) : null;
112 
113  return new UploadPolicy(
114  $dataset?->policy_id ?? $this->missingRequiredField('policy_id'),
115  $dataset?->title ?? $this->missingRequiredField('title'),
116  $dataset->upload_limit_in_mb ?? $this->missingRequiredField('upload_limit_in_mb'),
117  json_decode((string) $audience_json, true, 512, JSON_THROW_ON_ERROR),
118  $dataset?->audience_type ?? $this->missingRequiredField('audience_type'),
119  $dataset?->scope_definition ?? $this->missingRequiredField('scope_definition'),
120  (bool) ($dataset?->active ?? $this->missingRequiredField('active')),
121  $valid_from,
122  $valid_until,
123  $dataset?->owner ?? $this->missingRequiredField('owner'),
124  $this->getDateTimeObject(
125  (string) ($dataset?->create_date ?? $this->missingRequiredField('create_date'))
126  ),
127  $this->getDateTimeObject(
128  (string) ($dataset?->last_update ?? $this->missingRequiredField('last_update'))
129  )
130  );
131  }
132 
133  protected function getDateString(DateTimeImmutable $date): string
134  {
135  return $date->format(self::MYSQL_DATE_FORMAT);
136  }
137 
142  protected function getDateObject(string $date_string): DateTimeImmutable
143  {
144  return
145  (DateTimeImmutable::createFromFormat(self::MYSQL_DATETIME_FORMAT, $date_string . ' 00:00:00')) ?:
146  throw new LogicException("Could not create DateTimeImmutable from '$date_string'.");
147  }
148 
149  protected function getDateTimeString(DateTimeImmutable $date_time): string
150  {
151  return $date_time->format(self::MYSQL_DATETIME_FORMAT);
152  }
153 
154  protected function getDateTimeObject(string $date_time_string): DateTimeImmutable
155  {
156  return
157  (DateTimeImmutable::createFromFormat(self::MYSQL_DATETIME_FORMAT, $date_time_string)) ?:
158  throw new LogicException("Could not create DateTimeImmutable from '$date_time_string'.");
159  }
160 
164  protected function missingRequiredField(string $field_name): void
165  {
166  throw new LogicException("Could not retrieve data for required field '$field_name'.");
167  }
168 }
transformToDtoOrAbort(stdClass $dataset)
getDateTimeString(DateTimeImmutable $date_time)
getDateString(DateTimeImmutable $date)
__construct(protected readonly ilDBInterface $db)
getDateObject(string $date_string)
Returns a datetime object with &#39;00:00:00&#39; as H:i:s to avoid comparison errors because PHP will use th...
getDateTimeObject(string $date_time_string)
missingRequiredField(string $field_name)