Completed
Push — master ( cd48f7...0c447e )
by Guillermo A.
02:29
created

UpdateItemOperation::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
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 1
b 0
f 0
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 ErrorException;
9
use Guillermoandrae\DynamoDb\Contract\AbstractItemOperation;
10
use Guillermoandrae\DynamoDb\Factory\ExceptionFactory;
11
12
class UpdateItemOperation extends AbstractItemOperation
13
{
14
    /**
15
     * @var string The name of the update expression field.
16
     */
17
    protected $expressionFieldName = 'UpdateExpression';
18
19
    /**
20
     * Registers the DynamoDb client, Marshaler, table name, and item data with this object.
21
     *
22
     * @param DynamoDbClient $client The DynamoDb client.
23
     * @param Marshaler $marshaler The Marshaler.
24
     * @param string $tableName The table name.
25
     * @param array $primaryKey The primary key of the item to update.
26
     * @param array $updateData The update data.
27
     * @throws ErrorException Thrown when an invalid operator is provided.
28
     */
29 2
    public function __construct(
30
        DynamoDbClient $client,
31
        Marshaler $marshaler,
32
        string $tableName,
33
        array $primaryKey,
34
        array $updateData
35
    ) {
36 2
        parent::__construct($client, $marshaler, $tableName, $primaryKey);
37 2
        $this->setExpression($updateData);
38 2
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 2
    public function execute(): bool
44
    {
45
        try {
46 2
            $this->getClient()->updateItem($this->toArray());
47 1
            return true;
48 1
        } catch (DynamoDbException $ex) {
49 1
            throw ExceptionFactory::factory($ex);
50
        }
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 2
    public function toArray(): array
57
    {
58 2
        $operation = parent::toArray();
59 2
        if (!empty($this->expression)) {
60 1
            $operation[$this->expressionFieldName] = 'SET ' . $operation['UpdateExpression'];
61
        }
62 2
        return $operation;
63
    }
64
}
65