|
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\AbstractItemOperation; |
|
9
|
|
|
use Guillermoandrae\DynamoDb\Factory\ExceptionFactory; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* PutItem 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#putitem |
|
16
|
|
|
*/ |
|
17
|
|
|
final class PutItemOperation extends AbstractItemOperation |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array $itemData The item data. |
|
21
|
|
|
*/ |
|
22
|
|
|
private array $itemData = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Registers the DynamoDb client, Marshaler, table name, and item data with this object. |
|
26
|
|
|
* |
|
27
|
|
|
* @param DynamoDbClient $client The DynamoDb client. |
|
28
|
|
|
* @param Marshaler $marshaler The Marshaler. |
|
29
|
|
|
* @param string $tableName The table name. |
|
30
|
|
|
* @param array $itemData The item data. |
|
31
|
|
|
*/ |
|
32
|
8 |
|
public function __construct(DynamoDbClient $client, Marshaler $marshaler, string $tableName, array $itemData) |
|
33
|
|
|
{ |
|
34
|
8 |
|
$this->setClient($client); |
|
35
|
8 |
|
$this->setMarshaler($marshaler); |
|
36
|
8 |
|
$this->setTableName($tableName); |
|
37
|
8 |
|
$this->setItemData($itemData); |
|
38
|
8 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Registers the item data with this object. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $item The item data. |
|
44
|
|
|
* @return PutItemOperation This object. |
|
45
|
|
|
*/ |
|
46
|
8 |
|
public function setItemData(array $item): PutItemOperation |
|
47
|
|
|
{ |
|
48
|
8 |
|
$this->itemData = $this->marshaler->marshalItem($item); |
|
49
|
8 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
8 |
|
public function execute(): bool |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
8 |
|
$this->getClient()->putItem($this->toArray()); |
|
56
|
7 |
|
return true; |
|
57
|
1 |
|
} catch (DynamoDbException $ex) { |
|
58
|
1 |
|
throw ExceptionFactory::factory($ex); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
8 |
|
public function toArray(): array |
|
63
|
|
|
{ |
|
64
|
8 |
|
return array_merge(parent::toArray(), [ |
|
65
|
8 |
|
'Item' => $this->itemData, |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|