ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
PendingRegistrationDatabaseRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27
32{
33 public function __construct(private \ilDBInterface $db)
34 {
35 }
36
38 {
40 }
41
43 {
44 do {
45 $unique_id = uniqid((string) mt_rand(), true);
46 $hash = substr(md5($unique_id), 0, 16);
47
48 $res = $this->db->queryF(
49 'SELECT COUNT(usr_id) cnt FROM reg_dual_opt_in WHERE reg_hash = %s',
51 [$hash]
52 );
53 $row = $this->db->fetchObject($res);
54 if ($row->cnt > 0) {
55 continue;
56 }
57 break;
58 } while (true);
59
60 return new PendingRegistrationHash($hash);
61 }
62
63 public function store(PendingRegistration $reg): void
64 {
65 $this->db->manipulateF(
66 'REPLACE INTO reg_dual_opt_in (id, usr_id, reg_hash, creation_date) VALUES (%s, %s, %s, %s)',
67 [
72 ],
73 [
74 $reg->id()->toString(),
75 $reg->userId()->toInt(),
76 $reg->hash()->toString(),
77 $reg->createdAt()->getTimestamp()
78 ]
79 );
80 }
81
82 public function findByHashValue(string $hash_value): ?PendingRegistration
83 {
84 $res = $this->db->queryF(
85 'SELECT id, usr_id, reg_hash, creation_date FROM reg_dual_opt_in WHERE reg_hash = %s',
87 [$hash_value]
88 );
89
90 $row = $this->db->fetchAssoc($res);
91 if (!$row) {
92 return null;
93 }
94
95 return $this->rebuildObjFromRow($row);
96 }
97
98 public function delete(PendingRegistration ...$pending_registrations): void
99 {
100 if (\count($pending_registrations) === 0) {
101 return;
102 }
103
104 if (\count($pending_registrations) === 1) {
105 $this->db->manipulateF(
106 'DELETE FROM reg_dual_opt_in WHERE id = %s',
108 [$pending_registrations[0]->id()->toString()]
109 );
110 } else {
111 $ids = array_map(
112 static fn(PendingRegistration $pr): string => $pr->id()->toString(),
113 $pending_registrations
114 );
115
116 $this->db->manipulate(
117 'DELETE FROM reg_dual_opt_in ' .
118 'WHERE ' . $this->db->in('id', $ids, false, \ilDBConstants::T_TEXT)
119 );
120 }
121 }
122
123 public function deleteByUserId(int $usr_id): void
124 {
125 $this->db->manipulateF(
126 'DELETE FROM reg_dual_opt_in WHERE usr_id = %s',
128 [$usr_id]
129 );
130 }
131
135 public function findExpired(int $cutoff_ts, ?int $prioritize_usr_id = null): array
136 {
137 $except_anon_and_sys = $this->db->in(
138 'pr.usr_id',
140 true,
142 );
143
144 $query = '
145 SELECT pr.*
146 FROM reg_dual_opt_in pr
147 JOIN usr_data ud ON ud.usr_id = pr.usr_id
148 WHERE ud.active = 0
149 AND pr.creation_date < %s
150 AND ' . $except_anon_and_sys . '
151 ORDER BY (CASE WHEN pr.usr_id = %s THEN 0 ELSE 1 END), pr.creation_date
152 ';
153
154 $res = $this->db->queryF(
155 $query,
157 [$cutoff_ts, $prioritize_usr_id ?? 0]
158 );
159
160 $expired_registrations = [];
161 while ($row = $this->db->fetchAssoc($res)) {
162 $expired_registrations[] = $this->rebuildObjFromRow($row);
163 }
164
165 return $expired_registrations;
166 }
167
171 private function rebuildObjFromRow(array $row): PendingRegistration
172 {
173 $id = new PendingRegistrationId($row['id']);
174 $usr_id = new ObjectId($row['usr_id']);
175 $reg_hash = new PendingRegistrationHash($row['reg_hash']);
176 $created_at = new \DateTimeImmutable('@' . $row['creation_date']);
177
178 return new PendingRegistration($id, $usr_id, $reg_hash, $created_at);
179 }
180}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
@phpstan-type PendingRegistrationRecord array{id: string, usr_id: int, reg_hash: string,...
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
const ANONYMOUS_USER_ID
Definition: constants.php:27
Interface ilDBInterface.
$res
Definition: ltiservices.php:69