ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
RepositoryDB.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26
30class RepositoryDB implements Repository
31{
32 private const string TABLE_NAME = 'il_shortlinks';
33
34 public function __construct(
35 private readonly \ilDBInterface $db,
36 private TypeDataResolver $type_data_resolver
37 ) {
38 }
39
40 public function hasId(string $id): bool
41 {
42 $q = $this->db->queryF(
43 'SELECT 1 FROM ' . self::TABLE_NAME . ' WHERE id = %s',
45 [$id]
46 );
47
48 return $q->numRows() > 0;
49 }
50 public function has(string $shortlink): bool
51 {
52 $q = $this->db->queryF(
53 'SELECT 1 FROM ' . self::TABLE_NAME . ' WHERE alias = %s',
55 [$shortlink]
56 );
57
58 return $q->numRows() > 0;
59 }
60
61 private function rowToShortlink(array $row): Shortlink
62 {
63 json_decode((string) $row['target_type_data'], true);
64 $target_type_data = new TypeData([]);
65 $target_type_data->unserialize($row['target_type_data']);
66
67 return new ShortlinkDTO(
68 $row['alias'],
69 Type::from($row['target_type']),
70 $target_type_data,
71 (int) $row['position'],
72 (bool) $row['active'],
73 (int) $row['used'],
74 $row['id'] ?? null
75 );
76 }
77
78 public function getByAlias(string $shortlink): ?Shortlink
79 {
80 $q = $this->db->queryF(
81 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE alias = %s',
83 [$shortlink]
84 );
85 $row = $this->db->fetchAssoc($q);
86 return $this->rowToShortlink($row);
87 }
88
89 public function getById(string $string): ?Shortlink
90 {
91 $q = $this->db->queryF(
92 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE id = %s',
94 [$string]
95 );
96 $row = $this->db->fetchAssoc($q);
97 return $this->rowToShortlink($row);
98 }
99
100 public function blank(Type $type = Type::REPO): Shortlink
101 {
102 return new ShortlinkDTO(
103 '',
104 $type,
105 new TypeData(),
106 0,
107 true,
108 0
109 );
110 }
111
112 public function store(Shortlink $shortlink): Shortlink
113 {
114 if (empty($shortlink->getId())) {
115 // shift position of all other items
116 $this->db->manipulateF(
117 'UPDATE ' . self::TABLE_NAME . ' SET position = position + 1 WHERE position >= %s',
119 [$shortlink->getPosition()]
120 );
121
122 $shortlink = $shortlink->withId(
123 uniqid('', true)
124 );
125 }
126
127 $this->db->replace(
128 self::TABLE_NAME,
129 ['id' => [\ilDBConstants::T_TEXT, $shortlink->getId()],],
130 [
131
132 'alias' => [\ilDBConstants::T_TEXT, $shortlink->getAlias()],
133 'target_type' => [\ilDBConstants::T_TEXT, $shortlink->getTargetType()->value],
134 'target_type_data' => [\ilDBConstants::T_TEXT, $shortlink->getTargetData()->serialize()],
135 'position' => [\ilDBConstants::T_INTEGER, $shortlink->getPosition()],
136 'active' => [\ilDBConstants::T_INTEGER, (int) $shortlink->isActive()],
137 'used' => [\ilDBConstants::T_INTEGER, $shortlink->getUsed()],
138 ]
139 );
140
141 return $shortlink;
142 }
143
144 public function delete(Shortlink $shortlink): bool
145 {
146 return $this->db->manipulateF(
147 'DELETE FROM ' . self::TABLE_NAME . ' WHERE id = %s',
149 [$shortlink->getId()]
150 ) > 0;
151 }
152
153 public function getRange(int $start, int $limit): \Generator
154 {
155 throw new \RuntimeException('Not implemented yet');
156 }
157
158 public function getAll(): \Generator
159 {
160 $q = $this->db->query(
161 'SELECT * FROM ' . self::TABLE_NAME . ' ORDER BY position ASC'
162 );
163 while ($row = $this->db->fetchAssoc($q)) {
164 yield $this->rowToShortlink($row);
165 }
166 }
167
168 public function count(): int
169 {
170 $q = $this->db->query(
171 'SELECT COUNT(*) AS cnt FROM ' . self::TABLE_NAME
172 );
173 $row = $this->db->fetchAssoc($q);
174 return (int) $row['cnt'];
175 }
176
177 public function increaseUsage(Shortlink $shortlink): Shortlink
178 {
179 return $this->store(
180 $shortlink->increaseUsage()
181 );
182 }
183
185 {
186 return $this->type_data_resolver;
187 }
188
189}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Interface ilDBInterface.
$q
Definition: shib_logout.php:23