Completed
Push — master ( 005d83...940387 )
by Guillermo A.
02:52
created

BatchWriteItemOperation::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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
 * BatchWriteItem 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#batchwriteitem
16
 */
17
final class BatchWriteItemOperation 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 $deleteItems OPTIONAL The table(s) and associated primary key attributes to use when deleting.
25
     * @param array $putItems OPTIONAL The table(s) and associated items to add.
26
     */
27 3
    public function __construct(
28
        DynamoDbClient $client,
29
        Marshaler $marshaler,
30
        array $deleteItems = [],
31
        array $putItems = []
32
    ) {
33 3
        parent::__construct($client, $marshaler);
34 3
        if (!empty($deleteItems)) {
35 1
            $this->setDeleteRequest($deleteItems);
36
        }
37 3
        if (!empty($putItems)) {
38 1
            $this->setPutRequest($putItems);
39
        }
40 3
    }
41
42
    /**
43
     * Registers the DeleteRequest options.
44
     *
45
     * @see BatchWriteItemOperation::setWriteRequest()
46
     * @param array $deleteItems The table(s) and associated primary key attributes to use when deleting.
47
     * @return BatchWriteItemOperation
48
     */
49 2
    public function setDeleteRequest(array $deleteItems): BatchWriteItemOperation
50
    {
51 2
        return $this->setWriteRequest('Delete', $deleteItems);
52
    }
53
54
    /**
55
     * Registers the PutRequest options.
56
     *
57
     * @see BatchWriteItemOperation::setWriteRequest()
58
     * @param array $putItems The table(s) and associated items to add.
59
     * @return BatchWriteItemOperation
60
     */
61 1
    public function setPutRequest(array $putItems): BatchWriteItemOperation
62
    {
63 1
        return $this->setWriteRequest('Put', $putItems);
64
    }
65
66
    /**
67
     * Registers request options.
68
     *
69
     * @param string $type The request type (Put or Delete).
70
     * @param $requestItems The table(s) and associated options.
0 ignored issues
show
Bug introduced by
The type Guillermoandrae\DynamoDb\Operation\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
     * @return BatchWriteItemOperation
72
     */
73 3
    private function setWriteRequest(string $type, $requestItems): BatchWriteItemOperation
74
    {
75 3
        $type = ucfirst(strtolower($type));
76 3
        $keyName = ($type == 'Put') ? 'Item' : 'Key';
77 3
        foreach ($requestItems as $tableName => $items) {
78 2
            if (!isset($this->requestItems[$tableName])) {
79 2
                $this->requestItems[$tableName] = [];
80
            }
81 2
            foreach ($items as $item) {
82 2
                $this->requestItems[$tableName][] = [
83 2
                    sprintf('%sRequest', $type) => [
84 2
                        $keyName => $this->getMarshaler()->marshalItem($item)
85
                    ]
86
                ];
87
            }
88
        }
89 3
        return $this;
90
    }
91
92 3
    public function execute()
93
    {
94
        try {
95 3
            $this->getClient()->batchWriteItem($this->toArray());
96 2
            return true;
97 1
        } catch (DynamoDbException $ex) {
98 1
            throw ExceptionFactory::factory($ex);
99
        }
100
    }
101
}
102