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