1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Orm\DataMappers; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Domain\Entities\Token as Entity; |
8
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
9
|
|
|
use DateTimeImmutable; |
10
|
|
|
use Exception; |
11
|
|
|
use Opulence\Orm\DataMappers\SqlDataMapper; |
12
|
|
|
use Opulence\Orm\OrmException; |
13
|
|
|
use Opulence\QueryBuilders\Expression; |
14
|
|
|
use Opulence\QueryBuilders\InvalidQueryException; |
15
|
|
|
use Opulence\QueryBuilders\MySql\QueryBuilder; |
16
|
|
|
use Opulence\QueryBuilders\SelectQuery; |
17
|
|
|
|
18
|
|
|
/** @phan-file-suppress PhanTypeMismatchArgument */ |
19
|
|
|
class TokenSqlDataMapper extends SqlDataMapper implements ITokenDataMapper |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param IStringerEntity $entity |
23
|
|
|
*/ |
24
|
|
|
public function add($entity) |
25
|
|
|
{ |
26
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
27
|
|
|
|
28
|
|
|
$revokedAtTye = $entity->getRevokedAt() === null ? \PDO::PARAM_NULL : \PDO::PARAM_STR; |
29
|
|
|
|
30
|
|
|
$query = (new QueryBuilder()) |
31
|
|
|
->insert( |
32
|
|
|
'tokens', |
33
|
|
|
[ |
34
|
|
|
'id' => [$entity->getId(), \PDO::PARAM_STR], |
35
|
|
|
'api_client_id' => [$entity->getApiClientId(), \PDO::PARAM_STR], |
36
|
|
|
'expires_at' => [$entity->getExpiresAt(), \PDO::PARAM_STR], |
37
|
|
|
'revoked_at' => [$entity->getRevokedAt(), $revokedAtTye], |
38
|
|
|
] |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$statement = $this->writeConnection->prepare($query->getSql()); |
42
|
|
|
$statement->bindValues($query->getParameters()); |
43
|
|
|
$statement->execute(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param IStringerEntity $entity |
48
|
|
|
* |
49
|
|
|
* @throws InvalidQueryException |
50
|
|
|
*/ |
51
|
|
|
public function delete($entity) |
52
|
|
|
{ |
53
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
54
|
|
|
|
55
|
|
|
$query = (new QueryBuilder()) |
56
|
|
|
->update( |
57
|
|
|
'tokens', |
58
|
|
|
'tokens', |
59
|
|
|
['deleted_at' => new Expression('NOW()')] |
60
|
|
|
) |
61
|
|
|
->where('id = ?') |
62
|
|
|
->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR); |
63
|
|
|
|
64
|
|
|
$statement = $this->writeConnection->prepare($query->getSql()); |
65
|
|
|
$statement->bindValues($query->getParameters()); |
66
|
|
|
$statement->execute(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return Entity[] |
71
|
|
|
* @throws OrmException |
72
|
|
|
*/ |
73
|
|
|
public function getAll(): array |
74
|
|
|
{ |
75
|
|
|
$query = $this->getBaseQuery(); |
76
|
|
|
|
77
|
|
|
$sql = $query->getSql(); |
78
|
|
|
|
79
|
|
|
return $this->read($sql, [], self::VALUE_TYPE_ARRAY); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int|string $id |
84
|
|
|
* |
85
|
|
|
* @return Entity|null |
86
|
|
|
* @throws OrmException |
87
|
|
|
*/ |
88
|
|
|
public function getById($id): ?Entity |
89
|
|
|
{ |
90
|
|
|
$query = $this->getBaseQuery()->andWhere('tokens.id = :token_id'); |
91
|
|
|
|
92
|
|
|
$sql = $query->getSql(); |
93
|
|
|
$params = ['token_id' => [$id, \PDO::PARAM_STR]]; |
94
|
|
|
|
95
|
|
|
return $this->read($sql, $params, self::VALUE_TYPE_ENTITY, true); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $clientId |
100
|
|
|
* |
101
|
|
|
* @return Entity|null |
102
|
|
|
* @throws OrmException |
103
|
|
|
*/ |
104
|
|
|
public function getByClientId(string $clientId): ?Entity |
105
|
|
|
{ |
106
|
|
|
$query = $this->getBaseQuery()->andWhere('tokens.api_client_id = :api_client_id'); |
107
|
|
|
|
108
|
|
|
$sql = $query->getSql(); |
109
|
|
|
$params = ['api_client_id' => [$clientId, \PDO::PARAM_STR]]; |
110
|
|
|
|
111
|
|
|
return $this->read($sql, $params, self::VALUE_TYPE_ENTITY, true); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param IStringerEntity $entity |
116
|
|
|
* |
117
|
|
|
* @throws InvalidQueryException |
118
|
|
|
*/ |
119
|
|
|
public function update($entity) |
120
|
|
|
{ |
121
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
122
|
|
|
|
123
|
|
|
$revokedAtTye = $entity->getRevokedAt() === null ? \PDO::PARAM_NULL : \PDO::PARAM_STR; |
124
|
|
|
|
125
|
|
|
$query = (new QueryBuilder()) |
126
|
|
|
->update( |
127
|
|
|
'tokens', |
128
|
|
|
'tokens', |
129
|
|
|
[ |
130
|
|
|
'api_client_id' => [$entity->getApiClientId(), \PDO::PARAM_STR], |
131
|
|
|
'expires_at' => [$entity->getExpiresAt(), \PDO::PARAM_STR], |
132
|
|
|
'revoked_at' => [$entity->getRevokedAt(), $revokedAtTye], |
133
|
|
|
] |
134
|
|
|
) |
135
|
|
|
->where('id = ?') |
136
|
|
|
->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR); |
137
|
|
|
|
138
|
|
|
$statement = $this->writeConnection->prepare($query->getSql()); |
139
|
|
|
$statement->bindValues($query->getParameters()); |
140
|
|
|
$statement->execute(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array $data |
145
|
|
|
* |
146
|
|
|
* @return Entity |
147
|
|
|
* @throws Exception |
148
|
|
|
*/ |
149
|
|
|
protected function loadEntity(array $data): Entity |
150
|
|
|
{ |
151
|
|
|
$expiresAt = new DateTimeImmutable($data['expires_at']); |
152
|
|
|
$revokedAt = null; |
153
|
|
|
if (null !== $data['revoked_at']) { |
154
|
|
|
$revokedAt = new DateTimeImmutable($data['revoked_at']); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return new Entity( |
158
|
|
|
$data['id'], |
159
|
|
|
$data['api_client_id'], |
160
|
|
|
$expiresAt, |
161
|
|
|
$revokedAt |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return SelectQuery |
167
|
|
|
*/ |
168
|
|
|
private function getBaseQuery(): SelectQuery |
169
|
|
|
{ |
170
|
|
|
return (new QueryBuilder()) |
171
|
|
|
->select( |
172
|
|
|
'tokens.id', |
173
|
|
|
'tokens.api_client_id', |
174
|
|
|
'tokens.expires_at', |
175
|
|
|
'tokens.revoked_at' |
176
|
|
|
) |
177
|
|
|
->from('tokens') |
178
|
|
|
->where('tokens.deleted_at IS NULL'); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|