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
|
|
|
use Guillermoandrae\DynamoDb\Helper\Inflector; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* BatchWriteItem operation. |
14
|
|
|
* |
15
|
|
|
* @author Guillermo A. Fisher <[email protected]> |
16
|
|
|
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#batchwriteitem |
17
|
|
|
*/ |
18
|
|
|
final class BatchWriteItemOperation extends AbstractBatchItemOperation |
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 $deleteItems OPTIONAL The table(s) and associated primary key attributes to use when deleting. |
26
|
|
|
* @param array $putItems OPTIONAL The table(s) and associated items to add. |
27
|
|
|
*/ |
28
|
3 |
|
public function __construct( |
29
|
|
|
DynamoDbClient $client, |
30
|
|
|
Marshaler $marshaler, |
31
|
|
|
array $deleteItems = [], |
32
|
|
|
array $putItems = [] |
33
|
|
|
) { |
34
|
3 |
|
parent::__construct($client, $marshaler); |
35
|
3 |
|
if (!empty($deleteItems)) { |
36
|
1 |
|
$this->setDeleteRequest($deleteItems); |
37
|
|
|
} |
38
|
3 |
|
if (!empty($putItems)) { |
39
|
1 |
|
$this->setPutRequest($putItems); |
40
|
|
|
} |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Registers the DeleteRequest options. |
45
|
|
|
* |
46
|
|
|
* @param array $deleteItems The table(s) and associated primary key attributes to use when deleting. |
47
|
|
|
* @return BatchWriteItemOperation |
48
|
|
|
* @see BatchWriteItemOperation::setWriteRequest() |
49
|
|
|
*/ |
50
|
2 |
|
public function setDeleteRequest(array $deleteItems): BatchWriteItemOperation |
51
|
|
|
{ |
52
|
2 |
|
return $this->setWriteRequest('Delete', $deleteItems); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Registers the PutRequest options. |
57
|
|
|
* |
58
|
|
|
* @param array $putItems The table(s) and associated items to add. |
59
|
|
|
* @return BatchWriteItemOperation |
60
|
|
|
* @see BatchWriteItemOperation::setWriteRequest() |
61
|
|
|
*/ |
62
|
1 |
|
public function setPutRequest(array $putItems): BatchWriteItemOperation |
63
|
|
|
{ |
64
|
1 |
|
return $this->setWriteRequest('Put', $putItems); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Registers request options. |
69
|
|
|
* |
70
|
|
|
* @param string $type The request type (Put or Delete). |
71
|
|
|
* @param array $requestItems The table(s) and associated options. |
72
|
|
|
* @return BatchWriteItemOperation |
73
|
|
|
*/ |
74
|
3 |
|
private function setWriteRequest(string $type, array $requestItems): BatchWriteItemOperation |
75
|
|
|
{ |
76
|
3 |
|
$type = Inflector::camelize($type); |
77
|
3 |
|
$keyName = ($type == 'Put') ? 'Item' : 'Key'; |
78
|
3 |
|
foreach ($requestItems as $tableName => $items) { |
79
|
2 |
|
if (!isset($this->requestItems[$tableName])) { |
80
|
2 |
|
$this->requestItems[$tableName] = []; |
81
|
|
|
} |
82
|
2 |
|
foreach ($items as $item) { |
83
|
2 |
|
$this->requestItems[$tableName][] = [ |
84
|
2 |
|
sprintf('%sRequest', $type) => [ |
85
|
2 |
|
$keyName => $this->getMarshaler()->marshalItem($item) |
86
|
|
|
] |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
3 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
public function execute(): bool |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
3 |
|
$this->getClient()->batchWriteItem($this->toArray()); |
97
|
2 |
|
return true; |
98
|
1 |
|
} catch (DynamoDbException $ex) { |
99
|
1 |
|
throw ExceptionFactory::factory($ex); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|