|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Guillermoandrae\DynamoDb\Operation; |
|
4
|
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
|
6
|
|
|
use Aws\DynamoDb\Exception\DynamoDbException; |
|
7
|
|
|
use Aws\DynamoDb\Marshaler; |
|
8
|
|
|
use Guillermoandrae\DynamoDb\Contract\AbstractOperation; |
|
9
|
|
|
use Guillermoandrae\DynamoDb\Contract\ReturnConsumedCapacityAwareOperationTrait; |
|
10
|
|
|
use Guillermoandrae\DynamoDb\Factory\ExceptionFactory; |
|
11
|
|
|
|
|
12
|
|
|
final class BatchGetItemOperation extends AbstractOperation |
|
13
|
|
|
{ |
|
14
|
|
|
protected $primaryKeys = []; |
|
15
|
|
|
|
|
16
|
|
|
use ReturnConsumedCapacityAwareOperationTrait { |
|
17
|
|
|
ReturnConsumedCapacityAwareOperationTrait::toArray as returnConsumedCapacityAwareOperationTraitToArray; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Registers the DynamoDb client, Marshaler, and the mapping of tables and primary keys with this object. |
|
22
|
|
|
* |
|
23
|
|
|
* @param DynamoDbClient $client The DynamoDB client. |
|
24
|
|
|
* @param Marshaler $marshaler The Marshaler. |
|
25
|
|
|
* @param array $primaryKeys The tables and primary keys. |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function __construct(DynamoDbClient $client, Marshaler $marshaler, array $primaryKeys) |
|
28
|
|
|
{ |
|
29
|
2 |
|
parent::__construct($client, $marshaler); |
|
30
|
2 |
|
$this->setPrimaryKeys($primaryKeys); |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public function setPrimaryKeys(array $primaryKeys): BatchGetItemOperation |
|
34
|
|
|
{ |
|
35
|
2 |
|
$this->primaryKeys = []; |
|
36
|
2 |
|
foreach ($primaryKeys as $tableName => $keys) { |
|
37
|
2 |
|
$this->primaryKeys[$tableName] = ['Keys' => []]; |
|
38
|
2 |
|
foreach ($keys as $key) { |
|
39
|
2 |
|
$this->primaryKeys[$tableName]['Keys'][] = $this->getMarshaler()->marshalItem($key); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
2 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
public function execute() |
|
46
|
|
|
{ |
|
47
|
|
|
try { |
|
48
|
2 |
|
$items = []; |
|
49
|
2 |
|
$result = $this->client->batchGetItem($this->toArray()); |
|
50
|
1 |
|
foreach ($result['Responses'] as $table => $itemArrays) { |
|
51
|
1 |
|
foreach ($itemArrays as $itemArray) { |
|
52
|
1 |
|
$items[] = $this->getMarshaler()->unmarshalItem($itemArray); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
1 |
|
return $items; |
|
56
|
1 |
|
} catch (DynamoDbException $ex) { |
|
57
|
1 |
|
throw ExceptionFactory::factory($ex); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function toArray(): array |
|
62
|
|
|
{ |
|
63
|
2 |
|
$operation = $this->returnConsumedCapacityAwareOperationTraitToArray(); |
|
64
|
2 |
|
$operation['RequestItems'] = $this->primaryKeys; |
|
65
|
2 |
|
return $operation; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|