Completed
Push — master ( a7a349...3fc402 )
by Guillermo A.
03:23
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 14
c 1
b 0
f 0
dl 0
loc 55
rs 10
ccs 15
cts 15
cp 1

4 Methods

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