Completed
Push — master ( de3917...de6c58 )
by Guillermo A.
02:40
created

BatchGetItemOperation::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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