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\AbstractBatchItemOperation; |
9
|
|
|
use Guillermoandrae\DynamoDb\Factory\ExceptionFactory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* BatchGetItem operation. |
13
|
|
|
* |
14
|
|
|
* @author Guillermo A. Fisher <[email protected]> |
15
|
|
|
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#batchgetitem |
16
|
|
|
*/ |
17
|
|
|
final class BatchGetItemOperation extends AbstractBatchItemOperation |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Registers the DynamoDb client, Marshaler, and the mapping of tables and primary keys with this object. |
21
|
|
|
* |
22
|
|
|
* @param DynamoDbClient $client The DynamoDB client. |
23
|
|
|
* @param Marshaler $marshaler The Marshaler. |
24
|
|
|
* @param array $primaryKeys The tables and primary keys. |
25
|
|
|
*/ |
26
|
2 |
|
public function __construct(DynamoDbClient $client, Marshaler $marshaler, array $primaryKeys) |
27
|
|
|
{ |
28
|
2 |
|
parent::__construct($client, $marshaler); |
29
|
2 |
|
$this->setPrimaryKeys($primaryKeys); |
30
|
2 |
|
} |
31
|
|
|
|
32
|
2 |
|
public function setPrimaryKeys(array $primaryKeys): BatchGetItemOperation |
33
|
|
|
{ |
34
|
2 |
|
$requestItems = []; |
35
|
2 |
|
foreach ($primaryKeys as $tableName => $keys) { |
36
|
2 |
|
$requestItems[$tableName] = ['Keys' => []]; |
37
|
2 |
|
foreach ($keys as $key) { |
38
|
2 |
|
$requestItems[$tableName]['Keys'][] = $this->getMarshaler()->marshalItem($key); |
39
|
|
|
} |
40
|
|
|
} |
41
|
2 |
|
$this->setRequestItems($requestItems); |
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
|
|
|
|