1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Orm\DataMappers; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Domain\Entities\User; |
8
|
|
|
use AbterPhp\Admin\Domain\Entities\UserLanguage; |
9
|
|
|
use AbterPhp\Files\Domain\Entities\File; |
10
|
|
|
use AbterPhp\Files\Domain\Entities\FileDownload as Entity; |
11
|
|
|
use Opulence\Orm\DataMappers\SqlDataMapper; |
12
|
|
|
use Opulence\QueryBuilders\Expression; |
13
|
|
|
use Opulence\QueryBuilders\MySql\QueryBuilder; |
14
|
|
|
use Opulence\QueryBuilders\MySql\SelectQuery; |
15
|
|
|
|
16
|
|
|
/** @phan-file-suppress PhanTypeMismatchArgument */ |
17
|
|
|
class FileDownloadSqlDataMapper extends SqlDataMapper implements IFileDownloadDataMapper |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param Entity $entity |
21
|
|
|
*/ |
22
|
|
|
public function add($entity) |
23
|
|
|
{ |
24
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
25
|
|
|
|
26
|
|
|
$query = (new QueryBuilder()) |
27
|
|
|
->insert( |
28
|
|
|
'file_downloads', |
29
|
|
|
[ |
30
|
|
|
'id' => [$entity->getId(), \PDO::PARAM_STR], |
31
|
|
|
'file_id' => [$entity->getFile()->getId(), \PDO::PARAM_STR], |
32
|
|
|
'user_id' => [$entity->getUser()->getId(), \PDO::PARAM_STR], |
33
|
|
|
'downloaded_at' => [$entity->getDownloadedAt()->format(Entity::DATE_FORMAT), \PDO::PARAM_STR], |
34
|
|
|
] |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$sql = $query->getSql(); |
38
|
|
|
$params = $query->getParameters(); |
39
|
|
|
|
40
|
|
|
$statement = $this->writeConnection->prepare($sql); |
41
|
|
|
$statement->bindValues($params); |
42
|
|
|
$statement->execute(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Entity $entity |
47
|
|
|
* |
48
|
|
|
* @throws \Opulence\QueryBuilders\InvalidQueryException |
49
|
|
|
*/ |
50
|
|
|
public function delete($entity) |
51
|
|
|
{ |
52
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
53
|
|
|
|
54
|
|
|
$query = (new QueryBuilder()) |
55
|
|
|
->update( |
56
|
|
|
'file_downloads', |
57
|
|
|
'file_downloads', |
58
|
|
|
['deleted_at' => new Expression('NOW()')] |
59
|
|
|
) |
60
|
|
|
->where('id = ?') |
61
|
|
|
->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR); |
62
|
|
|
|
63
|
|
|
$sql = $query->getSql(); |
64
|
|
|
$params = $query->getParameters(); |
65
|
|
|
|
66
|
|
|
$statement = $this->writeConnection->prepare($sql); |
67
|
|
|
$statement->bindValues($params); |
68
|
|
|
$statement->execute(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Entity[] |
73
|
|
|
* @throws \Opulence\Orm\OrmException |
74
|
|
|
*/ |
75
|
|
|
public function getAll(): array |
76
|
|
|
{ |
77
|
|
|
$query = $this->getBaseQuery(); |
78
|
|
|
|
79
|
|
|
return $this->read($query->getSql(), [], self::VALUE_TYPE_ARRAY); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int $limitFrom |
84
|
|
|
* @param int $pageSize |
85
|
|
|
* @param string[] $orders |
86
|
|
|
* @param array $conditions |
87
|
|
|
* @param array $params |
88
|
|
|
* |
89
|
|
|
* @return Entity[] |
90
|
|
|
* @throws \Opulence\Orm\OrmException |
91
|
|
|
*/ |
92
|
|
|
public function getPage(int $limitFrom, int $pageSize, array $orders, array $conditions, array $params): array |
93
|
|
|
{ |
94
|
|
|
$query = $this->getBaseQuery() |
95
|
|
|
->limit($pageSize) |
96
|
|
|
->offset($limitFrom); |
97
|
|
|
|
98
|
|
|
if (!$orders) { |
|
|
|
|
99
|
|
|
$query->orderBy('downloaded_at DESC'); |
100
|
|
|
} |
101
|
|
|
foreach ($orders as $order) { |
102
|
|
|
$query->addOrderBy($order); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($conditions as $condition) { |
106
|
|
|
$query->andWhere($condition); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$replaceCount = 1; |
110
|
|
|
|
111
|
|
|
$sql = $query->getSql(); |
112
|
|
|
$sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sql, $replaceCount); |
113
|
|
|
|
114
|
|
|
return $this->read($sql, $params, self::VALUE_TYPE_ARRAY); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param int|string $id |
119
|
|
|
* |
120
|
|
|
* @return Entity |
121
|
|
|
* @throws \Opulence\Orm\OrmException |
122
|
|
|
*/ |
123
|
|
|
public function getById($id) |
124
|
|
|
{ |
125
|
|
|
$query = $this->getBaseQuery()->andWhere('file_downloads.id = :file_download_id'); |
126
|
|
|
|
127
|
|
|
$parameters = [ |
128
|
|
|
'file_download_id' => [$id, \PDO::PARAM_STR], |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ENTITY, true); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $userId |
136
|
|
|
* |
137
|
|
|
* @return Entity[] |
138
|
|
|
* @throws \Opulence\Orm\OrmException |
139
|
|
|
*/ |
140
|
|
|
public function getByUserId(string $userId): array |
141
|
|
|
{ |
142
|
|
|
$query = $this->getBaseQuery()->andWhere('user_id = :user_id'); |
143
|
|
|
$parameters = [ |
144
|
|
|
'user_id' => [$userId, \PDO::PARAM_STR], |
145
|
|
|
]; |
146
|
|
|
|
147
|
|
|
return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ARRAY); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $fileId |
152
|
|
|
* |
153
|
|
|
* @return Entity[] |
154
|
|
|
* @throws \Opulence\Orm\OrmException |
155
|
|
|
*/ |
156
|
|
|
public function getByFileId(string $fileId): array |
157
|
|
|
{ |
158
|
|
|
$query = $this->getBaseQuery()->andWhere('file_id = :file_id'); |
159
|
|
|
|
160
|
|
|
$parameters = [ |
161
|
|
|
'file_id' => [$fileId, \PDO::PARAM_STR], |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ARRAY); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param object $entity |
169
|
|
|
* |
170
|
|
|
* @throws \Opulence\QueryBuilders\InvalidQueryException |
171
|
|
|
*/ |
172
|
|
|
public function update($entity) |
173
|
|
|
{ |
174
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
175
|
|
|
|
176
|
|
|
$query = (new QueryBuilder()) |
177
|
|
|
->update( |
178
|
|
|
'file_downloads', |
179
|
|
|
'file_downloads', |
180
|
|
|
[ |
181
|
|
|
'file_id' => [$entity->getFile()->getId(), \PDO::PARAM_STR], |
182
|
|
|
'user_id' => [$entity->getUser()->getId(), \PDO::PARAM_STR], |
183
|
|
|
'downloaded_at' => [$entity->getDownloadedAt()->format(Entity::DATE_FORMAT), \PDO::PARAM_STR], |
184
|
|
|
] |
185
|
|
|
) |
186
|
|
|
->where('id = ?') |
187
|
|
|
->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR); |
188
|
|
|
|
189
|
|
|
$sql = $query->getSql(); |
190
|
|
|
$params = $query->getParameters(); |
191
|
|
|
|
192
|
|
|
$statement = $this->writeConnection->prepare($sql); |
193
|
|
|
$statement->bindValues($params); |
194
|
|
|
$statement->execute(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param array $hash |
199
|
|
|
* |
200
|
|
|
* @return Entity |
201
|
|
|
* @throws \Exception |
202
|
|
|
*/ |
203
|
|
|
protected function loadEntity(array $hash) |
204
|
|
|
{ |
205
|
|
|
$file = new File($hash['file_id'], $hash['filesystem_name'], $hash['public_name'], $hash['mime'], ''); |
206
|
|
|
$userLanguage = new UserLanguage('', '', ''); |
207
|
|
|
$user = new User( |
208
|
|
|
$hash['user_id'], |
209
|
|
|
$hash['username'], |
210
|
|
|
'', |
211
|
|
|
'', |
212
|
|
|
true, |
213
|
|
|
true, |
214
|
|
|
$userLanguage |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
return new Entity( |
218
|
|
|
$hash['id'], |
219
|
|
|
$file, |
220
|
|
|
$user, |
221
|
|
|
new \DateTime($hash['downloaded_at']) |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return SelectQuery |
227
|
|
|
*/ |
228
|
|
|
private function getBaseQuery() |
229
|
|
|
{ |
230
|
|
|
/** @var SelectQuery $query */ |
231
|
|
|
$query = (new QueryBuilder()) |
232
|
|
|
->select( |
233
|
|
|
'file_downloads.id', |
234
|
|
|
'file_downloads.file_id', |
235
|
|
|
'file_downloads.user_id', |
236
|
|
|
'file_downloads.downloaded_at', |
237
|
|
|
'files.filesystem_name AS filesystem_name', |
238
|
|
|
'files.public_name AS public_name', |
239
|
|
|
'files.mime AS mime', |
240
|
|
|
'users.username AS username' |
241
|
|
|
) |
242
|
|
|
->from('file_downloads') |
243
|
|
|
->innerJoin( |
244
|
|
|
'files', |
245
|
|
|
'files', |
246
|
|
|
'files.id=file_downloads.file_id' |
247
|
|
|
) |
248
|
|
|
->innerJoin( |
249
|
|
|
'users', |
250
|
|
|
'users', |
251
|
|
|
'users.id=file_downloads.user_id' |
252
|
|
|
) |
253
|
|
|
->where('file_downloads.deleted_at IS NULL'); |
254
|
|
|
|
255
|
|
|
return $query; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|