-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDynamoDBRepository.php
More file actions
176 lines (150 loc) · 6.5 KB
/
DynamoDBRepository.php
File metadata and controls
176 lines (150 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Shopware\App\SDK\Adapter\DynamoDB;
use AsyncAws\DynamoDb\DynamoDbClient;
use AsyncAws\DynamoDb\Input\DeleteItemInput;
use AsyncAws\DynamoDb\Input\GetItemInput;
use AsyncAws\DynamoDb\Input\PutItemInput;
use AsyncAws\DynamoDb\Input\UpdateItemInput;
use Shopware\App\SDK\Shop\ShopInterface;
use Shopware\App\SDK\Shop\ShopRepositoryInterface;
/**
* @implements ShopRepositoryInterface<DynamoDBShop>
*/
class DynamoDBRepository implements ShopRepositoryInterface
{
public function __construct(private readonly DynamoDbClient $client, private readonly string $tableName)
{
}
public function createShopStruct(string $shopId, string $shopUrl, string $shopSecret): ShopInterface
{
return new DynamoDBShop($shopId, $shopUrl, $shopSecret);
}
public function createShop(ShopInterface $shop): void
{
$item = [
'id' => ['S' => $shop->getShopId()],
'active' => ['BOOL' => $shop->isShopActive() ? '1' : '0'],
'confirmed' => ['BOOL' => $shop->isRegistrationConfirmed() ? '1' : '0'],
'url' => ['S' => $shop->getShopUrl()],
'secret' => ['S' => $shop->getShopSecret()],
'clientId' => ['S' => (string) $shop->getShopClientId()],
'clientSecret' => ['S' => (string) $shop->getShopClientSecret()],
'pendingShopSecret' => ['S' => (string) $shop->getPendingShopSecret()],
'pendingShopUrl' => ['S' => (string) $shop->getPendingShopUrl()],
'previousShopSecret' => ['S' => (string) $shop->getPreviousShopSecret()],
'secretsRotatedAt' => ['S' => (string) ($shop->getSecretsRotatedAt()?->getTimestamp() ?? '')],
'hasVerifiedWithDoubleSignature' => ['BOOL' => $shop->hasVerifiedWithDoubleSignature() ? '1' : '0'],
];
$this->client->putItem(new PutItemInput([
'TableName' => $this->tableName,
'Item' => $item,
]));
}
public function getShopFromId(string $shopId): ShopInterface|null
{
$item = $this->client->getItem(new GetItemInput([
'TableName' => $this->tableName,
'Key' => [
'id' => ['S' => $shopId],
],
]))->getItem();
if (!$item) {
return null;
}
$shopClientId = $item['clientId']->getS();
$shopClientSecret = $item['clientSecret']->getS();
if ($shopClientSecret === '') {
$shopClientSecret = null;
}
if ($shopClientId === '') {
$shopClientId = null;
}
$active = $item['active']->getBool();
if ($active === null) {
$active = false;
}
$confirmed = true;
if (isset($item['confirmed'])) {
$confirmed = $item['confirmed']->getBool();
if ($confirmed === null) {
$confirmed = false;
}
}
$pendingShopSecret = isset($item['pendingShopSecret']) ? $item['pendingShopSecret']->getS() : null;
$pendingShopUrl = isset($item['pendingShopUrl']) ? $item['pendingShopUrl']->getS() : null;
$previousShopSecret = isset($item['previousShopSecret']) ? $item['previousShopSecret']->getS() : null;
if ($pendingShopSecret === '') {
$pendingShopSecret = null;
}
if ($pendingShopUrl === '') {
$pendingShopUrl = null;
}
if ($previousShopSecret === '') {
$previousShopSecret = null;
}
$secretsRotatedAt = null;
if (isset($item['secretsRotatedAt'])) {
$timestamp = $item['secretsRotatedAt']->getS();
if ($timestamp !== null && $timestamp !== '') {
$secretsRotatedAt = (new \DateTimeImmutable())->setTimestamp((int) $timestamp);
}
}
$hasVerifiedWithDoubleSignature = false;
if (isset($item['hasVerifiedWithDoubleSignature'])) {
$hasVerifiedWithDoubleSignature = $item['hasVerifiedWithDoubleSignature']->getBool();
if ($hasVerifiedWithDoubleSignature === null) {
$hasVerifiedWithDoubleSignature = false;
}
}
return new DynamoDBShop(
$item['id']->getS() ?? '',
$item['url']->getS() ?? '',
$item['secret']->getS() ?? '',
$shopClientId,
$shopClientSecret,
$active,
$pendingShopSecret,
$pendingShopUrl,
$previousShopSecret,
$secretsRotatedAt,
$hasVerifiedWithDoubleSignature,
$confirmed,
);
}
public function updateShop(ShopInterface $shop): void
{
$this->client->updateItem(new UpdateItemInput([
'TableName' => $this->tableName,
'Key' => [
'id' => ['S' => $shop->getShopId()],
],
'UpdateExpression' => 'SET active = :active, confirmed = :confirmed, #u = :url, secret = :secret, clientId = :clientId, clientSecret = :clientSecret, pendingShopSecret = :pendingShopSecret, pendingShopUrl = :pendingShopUrl, previousShopSecret = :previousShopSecret, secretsRotatedAt = :secretsRotatedAt, hasVerifiedWithDoubleSignature = :hasVerifiedWithDoubleSignature',
'ExpressionAttributeNames' => [
'#u' => 'url',
],
'ExpressionAttributeValues' => [
':active' => ['BOOL' => $shop->isShopActive() ? '1' : '0'],
':confirmed' => ['BOOL' => $shop->isRegistrationConfirmed() ? '1' : '0'],
':url' => ['S' => $shop->getShopUrl()],
':secret' => ['S' => $shop->getShopSecret()],
':clientId' => ['S' => (string) $shop->getShopClientId()],
':clientSecret' => ['S' => (string) $shop->getShopClientSecret()],
':pendingShopSecret' => ['S' => (string) $shop->getPendingShopSecret()],
':pendingShopUrl' => ['S' => (string) $shop->getPendingShopUrl()],
':previousShopSecret' => ['S' => (string) $shop->getPreviousShopSecret()],
':secretsRotatedAt' => ['S' => (string) ($shop->getSecretsRotatedAt()?->getTimestamp() ?? '')],
':hasVerifiedWithDoubleSignature' => ['BOOL' => $shop->hasVerifiedWithDoubleSignature() ? '1' : '0'],
],
]));
}
public function deleteShop(string $shopId): void
{
$this->client->deleteItem(new DeleteItemInput([
'TableName' => $this->tableName,
'Key' => [
'id' => ['S' => $shopId],
],
]));
}
}