ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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 23 of file UploadPolicyDBRepository.php.

Constructor & Destructor Documentation

◆ __construct()

UploadPolicyDBRepository::__construct ( protected readonly ilDBInterface  $db)

Definition at line 28 of file UploadPolicyDBRepository.php.

29  {
30  }

Member Function Documentation

◆ delete()

UploadPolicyDBRepository::delete ( UploadPolicy  $policy)

Definition at line 93 of file UploadPolicyDBRepository.php.

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

◆ get()

UploadPolicyDBRepository::get ( int  $policy_id)

Definition at line 66 of file UploadPolicyDBRepository.php.

References transformToDtoOrAbort().

66  : ?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  }
transformToDtoOrAbort(stdClass $dataset)
+ Here is the call graph for this function:

◆ getAll()

UploadPolicyDBRepository::getAll ( )
Returns
UploadPolicy[]

Definition at line 80 of file UploadPolicyDBRepository.php.

References transformToDtoOrAbort().

80  : 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  }
transformToDtoOrAbort(stdClass $dataset)
+ 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 142 of file UploadPolicyDBRepository.php.

Referenced by transformToDtoOrAbort().

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  }
+ Here is the caller graph for this function:

◆ getDateString()

UploadPolicyDBRepository::getDateString ( DateTimeImmutable  $date)
protected

Definition at line 133 of file UploadPolicyDBRepository.php.

Referenced by store().

133  : string
134  {
135  return $date->format(self::MYSQL_DATE_FORMAT);
136  }
+ Here is the caller graph for this function:

◆ getDateTimeObject()

UploadPolicyDBRepository::getDateTimeObject ( string  $date_time_string)
protected

Definition at line 154 of file UploadPolicyDBRepository.php.

Referenced by transformToDtoOrAbort().

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  }
+ Here is the caller graph for this function:

◆ getDateTimeString()

UploadPolicyDBRepository::getDateTimeString ( DateTimeImmutable  $date_time)
protected

Definition at line 149 of file UploadPolicyDBRepository.php.

Referenced by store().

149  : string
150  {
151  return $date_time->format(self::MYSQL_DATETIME_FORMAT);
152  }
+ Here is the caller graph for this function:

◆ missingRequiredField()

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

Definition at line 164 of file UploadPolicyDBRepository.php.

Referenced by transformToDtoOrAbort().

164  : void
165  {
166  throw new LogicException("Could not retrieve data for required field '$field_name'.");
167  }
+ Here is the caller graph for this function:

◆ store()

UploadPolicyDBRepository::store ( UploadPolicy  $policy)

Definition at line 32 of file UploadPolicyDBRepository.php.

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().

32  : 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  }
getDateTimeString(DateTimeImmutable $date_time)
getDateString(DateTimeImmutable $date)
+ Here is the call graph for this function:

◆ transformToDtoOrAbort()

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

Definition at line 106 of file UploadPolicyDBRepository.php.

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

Referenced by get(), and getAll().

106  : 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  }
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)
+ 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 26 of file UploadPolicyDBRepository.php.

◆ MYSQL_DATETIME_FORMAT

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

Definition at line 25 of file UploadPolicyDBRepository.php.


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