Completed
Push — master ( 19c355...d500cb )
by Guillermo A.
02:33
created

PutItemOperation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
c 1
b 0
f 0
dl 0
loc 55
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A toArray() 0 4 1
A setItemData() 0 4 1
A execute() 0 7 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\AbstractItemOperation;
9
use Guillermoandrae\DynamoDb\Exception;
10
11
/**
12
 * @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#putitem
13
 */
14
final class PutItemOperation extends AbstractItemOperation
15
{
16
    /**
17
     * @var array $itemData The item data.
18
     */
19
    protected $itemData;
20
21
    /**
22
     * Registers the DynamoDb client, Marshaler, table name, and item data with this object.
23
     *
24
     * @param DynamoDbClient $client The DynamoDb client.
25
     * @param Marshaler $marshaler The Marshaler.
26
     * @param string $tableName The table name.
27
     * @param array $itemData The item data.
28
     */
29 5
    public function __construct(DynamoDbClient $client, Marshaler $marshaler, string $tableName, array $itemData)
30
    {
31 5
        $this->setClient($client);
32 5
        $this->setMarshaler($marshaler);
33 5
        $this->setTableName($tableName);
34 5
        $this->setItemData($itemData);
35 5
    }
36
37
    /**
38
     * Registers the item data with this object.
39
     *
40
     * @param array $item The item data.
41
     * @return PutItemOperation This object.
42
     */
43 5
    public function setItemData(array $item): PutItemOperation
44
    {
45 5
        $this->itemData = $this->marshaler->marshalItem($item);
46 5
        return $this;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 5
    public function execute(): bool
53
    {
54
        try {
55 5
            $this->getClient()->putItem($this->toArray());
56 4
            return true;
57 1
        } catch (DynamoDbException $ex) {
58 1
            throw new Exception($ex->getMessage());
59
        }
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65 5
    public function toArray(): array
66
    {
67 5
        return array_merge(parent::toArray(), [
68 5
            'Item' => $this->itemData,
69
        ]);
70
    }
71
}
72