13
13
14
14
use AsyncAws \DynamoDb \DynamoDbClient ;
15
15
use AsyncAws \DynamoDb \Exception \ConditionalCheckFailedException ;
16
+ use AsyncAws \DynamoDb \Input \CreateTableInput ;
17
+ use AsyncAws \DynamoDb \Input \DeleteItemInput ;
16
18
use AsyncAws \DynamoDb \Input \DescribeTableInput ;
19
+ use AsyncAws \DynamoDb \Input \GetItemInput ;
20
+ use AsyncAws \DynamoDb \Input \PutItemInput ;
17
21
use AsyncAws \DynamoDb \ValueObject \AttributeDefinition ;
18
22
use AsyncAws \DynamoDb \ValueObject \AttributeValue ;
19
23
use AsyncAws \DynamoDb \ValueObject \KeySchemaElement ;
@@ -135,22 +139,22 @@ public function save(Key $key): void
135
139
$ key ->reduceLifetime ($ this ->initialTtl );
136
140
137
141
try {
138
- $ this ->client ->putItem ([
142
+ $ this ->client ->putItem (new PutItemInput ( [
139
143
'TableName ' => $ this ->tableName ,
140
144
'Item ' => [
141
- $ this ->idAttr => ['S ' => $ this ->getHashedKey ($ key )],
142
- $ this ->tokenAttr => ['S ' => $ this ->getUniqueToken ($ key )],
143
- $ this ->expirationAttr => ['N ' => (string ) (\microtime () + $ this ->initialTtl )],
145
+ $ this ->idAttr => new AttributeValue ( ['S ' => $ this ->getHashedKey ($ key )]) ,
146
+ $ this ->tokenAttr => new AttributeValue ( ['S ' => $ this ->getUniqueToken ($ key )]) ,
147
+ $ this ->expirationAttr => new AttributeValue ( ['N ' => (string ) (\microtime () + $ this ->initialTtl )]) ,
144
148
],
145
149
'ConditionExpression ' => 'attribute_not_exists(#key) OR #expires_at < :now ' ,
146
150
'ExpressionAttributeNames ' => [
147
151
'#key ' => $ this ->idAttr ,
148
152
'#expires_at ' => $ this ->expirationAttr ,
149
153
],
150
154
'ExpressionAttributeValues ' => [
151
- ':now ' => ['N ' => (string ) \microtime ()],
155
+ ':now ' => new AttributeValue ( ['N ' => (string ) \microtime ()]) ,
152
156
],
153
- ]);
157
+ ])) ;
154
158
} catch (ConditionalCheckFailedException ) {
155
159
// the lock is already acquired. It could be us. Let's try to put off.
156
160
$ this ->putOffExpiration ($ key , $ this ->initialTtl );
@@ -163,23 +167,23 @@ public function save(Key $key): void
163
167
164
168
public function delete (Key $ key ): void
165
169
{
166
- $ this ->client ->deleteItem ([
170
+ $ this ->client ->deleteItem (new DeleteItemInput ( [
167
171
'TableName ' => $ this ->tableName ,
168
172
'Key ' => [
169
173
$ this ->idAttr => new AttributeValue (['S ' => $ this ->getHashedKey ($ key )]),
170
174
],
171
- ]);
175
+ ])) ;
172
176
}
173
177
174
178
public function exists (Key $ key ): bool
175
179
{
176
- $ existingLock = $ this ->client ->getItem ([
180
+ $ existingLock = $ this ->client ->getItem (new GetItemInput ( [
177
181
'TableName ' => $ this ->tableName ,
178
182
'ConsistentRead ' => true ,
179
183
'Key ' => [
180
184
$ this ->idAttr => new AttributeValue (['S ' => $ this ->getHashedKey ($ key )]),
181
185
],
182
- ]);
186
+ ])) ;
183
187
184
188
$ item = $ existingLock ->getItem ();
185
189
@@ -208,12 +212,12 @@ public function putOffExpiration(Key $key, float $ttl): void
208
212
$ uniqueToken = $ this ->getUniqueToken ($ key );
209
213
210
214
try {
211
- $ this ->client ->putItem ([
215
+ $ this ->client ->putItem (new PutItemInput ( [
212
216
'TableName ' => $ this ->tableName ,
213
217
'Item ' => [
214
- $ this ->idAttr => ['S ' => $ this ->getHashedKey ($ key )],
215
- $ this ->tokenAttr => ['S ' => $ uniqueToken ],
216
- $ this ->expirationAttr => ['N ' => (string ) (\microtime () + $ ttl )],
218
+ $ this ->idAttr => new AttributeValue ( ['S ' => $ this ->getHashedKey ($ key )]) ,
219
+ $ this ->tokenAttr => new AttributeValue ( ['S ' => $ uniqueToken ]) ,
220
+ $ this ->expirationAttr => new AttributeValue ( ['N ' => (string ) (\microtime () + $ ttl )]) ,
217
221
],
218
222
'ConditionExpression ' => 'attribute_exists(#key) AND (#token = :token OR #expires_at <= :now) ' ,
219
223
'ExpressionAttributeNames ' => [
@@ -222,10 +226,10 @@ public function putOffExpiration(Key $key, float $ttl): void
222
226
'#token ' => $ this ->tokenAttr ,
223
227
],
224
228
'ExpressionAttributeValues ' => [
225
- ':now ' => ['N ' => (string ) \microtime ()],
229
+ ':now ' => new AttributeValue ( ['N ' => (string ) \microtime ()]) ,
226
230
':token ' => $ uniqueToken ,
227
231
],
228
- ]);
232
+ ])) ;
229
233
} catch (ConditionalCheckFailedException ) {
230
234
// The item doesn't exist or was acquired by someone else
231
235
throw new LockConflictedException ();
@@ -238,7 +242,7 @@ public function putOffExpiration(Key $key, float $ttl): void
238
242
239
243
public function createTable (): void
240
244
{
241
- $ this ->client ->createTable ([
245
+ $ this ->client ->createTable (new CreateTableInput ( [
242
246
'TableName ' => $ this ->tableName ,
243
247
'AttributeDefinitions ' => [
244
248
new AttributeDefinition (['AttributeName ' => $ this ->idAttr , 'AttributeType ' => 'S ' ]),
@@ -252,7 +256,7 @@ public function createTable(): void
252
256
'ReadCapacityUnits ' => $ this ->readCapacityUnits ,
253
257
'WriteCapacityUnits ' => $ this ->writeCapacityUnits ,
254
258
]),
255
- ]);
259
+ ])) ;
256
260
257
261
$ this ->client ->tableExists (new DescribeTableInput (['TableName ' => $ this ->tableName ]))->wait ();
258
262
}
0 commit comments