ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
UploadPolicyDBRepository Class Reference
+ Collaboration diagram for UploadPolicyDBRepository:

Public Member Functions

 __construct (protected readonly ilDBInterface $db)
 
 store (UploadPolicy $policy)
 
 get (int $policy_id)
 
 getAll ()
 
 delete (UploadPolicy $policy)
 

Protected Member Functions

 transformToDtoOrAbort (stdClass $dataset)
 
 getDateString (DateTimeImmutable $date)
 
 getDateObject (string $date_string)
 Returns a datetime object with '00:00:00' as H:i:s to avoid comparison errors because PHP will use the current time automatically otherwise. More...
 
 getDateTimeString (DateTimeImmutable $date_time)
 
 getDateTimeObject (string $date_time_string)
 
 missingRequiredField (string $field_name)
 

Protected Attributes

const MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s'
 
const MYSQL_DATE_FORMAT = 'Y-m-d'
 

Detailed Description

Author
Lukas Zehnder lukas.nosp@m.@sr..nosp@m.solut.nosp@m.ions

Definition at line 24 of file UploadPolicyDBRepository.php.

Constructor & Destructor Documentation

◆ __construct()

UploadPolicyDBRepository::__construct ( protected readonly ilDBInterface  $db)

Definition at line 29 of file UploadPolicyDBRepository.php.

30 {
31 }

Member Function Documentation

◆ delete()

UploadPolicyDBRepository::delete ( UploadPolicy  $policy)

Definition at line 94 of file UploadPolicyDBRepository.php.

94 : 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 }

◆ get()

UploadPolicyDBRepository::get ( int  $policy_id)

Definition at line 67 of file UploadPolicyDBRepository.php.

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 }
transformToDtoOrAbort(stdClass $dataset)

References transformToDtoOrAbort().

+ Here is the call graph for this function:

◆ getAll()

UploadPolicyDBRepository::getAll ( )
Returns
UploadPolicy[]

Definition at line 81 of file UploadPolicyDBRepository.php.

81 : 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 }

References transformToDtoOrAbort().

+ Here is the call graph for this function:

◆ getDateObject()

UploadPolicyDBRepository::getDateObject ( string  $date_string)
protected

Returns a datetime object with '00:00:00' as H:i:s to avoid comparison errors because PHP will use the current time automatically otherwise.

Definition at line 143 of file UploadPolicyDBRepository.php.

143 : 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 }

Referenced by transformToDtoOrAbort().

+ Here is the caller graph for this function:

◆ getDateString()

UploadPolicyDBRepository::getDateString ( DateTimeImmutable  $date)
protected

Definition at line 134 of file UploadPolicyDBRepository.php.

134 : string
135 {
136 return $date->format(self::MYSQL_DATE_FORMAT);
137 }

Referenced by store().

+ Here is the caller graph for this function:

◆ getDateTimeObject()

UploadPolicyDBRepository::getDateTimeObject ( string  $date_time_string)
protected

Definition at line 155 of file UploadPolicyDBRepository.php.

155 : 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 }

Referenced by transformToDtoOrAbort().

+ Here is the caller graph for this function:

◆ getDateTimeString()

UploadPolicyDBRepository::getDateTimeString ( DateTimeImmutable  $date_time)
protected

Definition at line 150 of file UploadPolicyDBRepository.php.

150 : string
151 {
152 return $date_time->format(self::MYSQL_DATETIME_FORMAT);
153 }

Referenced by store().

+ Here is the caller graph for this function:

◆ missingRequiredField()

UploadPolicyDBRepository::missingRequiredField ( string  $field_name)
protected
Exceptions
LogicException

Definition at line 165 of file UploadPolicyDBRepository.php.

165 : void
166 {
167 throw new LogicException("Could not retrieve data for required field '$field_name'.");
168 }

Referenced by transformToDtoOrAbort().

+ Here is the caller graph for this function:

◆ store()

UploadPolicyDBRepository::store ( UploadPolicy  $policy)

Definition at line 33 of file UploadPolicyDBRepository.php.

33 : 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 }
getDateString(DateTimeImmutable $date)
getDateTimeString(DateTimeImmutable $date_time)

References UploadPolicy\getAudience(), UploadPolicy\getAudienceType(), UploadPolicy\getCreateDate(), getDateString(), getDateTimeString(), UploadPolicy\getLastUpdate(), UploadPolicy\getOwnerId(), UploadPolicy\getPolicyId(), UploadPolicy\getScopeDefinition(), UploadPolicy\getTitle(), UploadPolicy\getUploadLimitInMB(), UploadPolicy\getValidFrom(), UploadPolicy\getValidUntil(), and UploadPolicy\isActive().

+ Here is the call graph for this function:

◆ transformToDtoOrAbort()

UploadPolicyDBRepository::transformToDtoOrAbort ( stdClass  $dataset)
protected
Exceptions
LogicExceptionif required data is missing

Definition at line 107 of file UploadPolicyDBRepository.php.

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 }
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...
getDateTimeObject(string $date_time_string)
missingRequiredField(string $field_name)

References getDateObject(), getDateTimeObject(), and missingRequiredField().

Referenced by get(), and getAll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ MYSQL_DATE_FORMAT

const UploadPolicyDBRepository::MYSQL_DATE_FORMAT = 'Y-m-d'
protected

Definition at line 27 of file UploadPolicyDBRepository.php.

◆ MYSQL_DATETIME_FORMAT

const UploadPolicyDBRepository::MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s'
protected

Definition at line 26 of file UploadPolicyDBRepository.php.


The documentation for this class was generated from the following file: