UpdateItemOperation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 2
b 0
f 0
dl 0
loc 56
rs 10
ccs 21
cts 21
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 3
A execute() 0 7 2
A toArray() 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 ErrorException;
9
use Guillermoandrae\DynamoDb\Constant\Operators;
10
use Guillermoandrae\DynamoDb\Contract\AbstractItemOperation;
11
use Guillermoandrae\DynamoDb\Factory\ExceptionFactory;
12
13
/**
14
 * UpdateItem operation.
15
 *
16
 * Currently, only SET is supported.
17
 *
18
 * @author Guillermo A. Fisher <[email protected]>
19
 * @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#updateitem
20
 */
21
class UpdateItemOperation extends AbstractItemOperation
22
{
23
    /**
24
     * @var string The name of the update expression field.
25
     */
26
    protected string $expressionFieldName = 'UpdateExpression';
27
28
    /**
29
     * Registers the DynamoDb client, Marshaler, table name, and item data with this object.
30
     *
31
     * @param DynamoDbClient $client The DynamoDb client.
32
     * @param Marshaler $marshaler The Marshaler.
33
     * @param string $tableName The table name.
34
     * @param array $primaryKey The primary key of the item to update.
35
     * @param array $updateData The update data.
36
     * @throws ErrorException Thrown when an invalid operator is provided.
37
     */
38 3
    public function __construct(
39
        DynamoDbClient $client,
40
        Marshaler      $marshaler,
41
        string         $tableName,
42
        array          $primaryKey,
43
        array          $updateData
44
    ) {
45 3
        parent::__construct($client, $marshaler, $tableName, $primaryKey);
46 3
        $updateDataArray = [];
47 3
        foreach ($updateData as $key => $options) {
48 2
            if (!isset($options['operator'])) {
49 1
                $updateDataArray[$key] = [
50 1
                    'operator' => Operators::EQ,
51 1
                    'value' => $options
52
                ];
53
            } else {
54 1
                $updateDataArray[$key] = $options;
55
            }
56
        }
57 3
        $this->setExpression($updateDataArray);
58 3
    }
59
60 2
    public function execute(): bool
61
    {
62
        try {
63 2
            $this->getClient()->updateItem($this->toArray());
64 1
            return true;
65 1
        } catch (DynamoDbException $ex) {
66 1
            throw ExceptionFactory::factory($ex);
67
        }
68
    }
69
70 3
    public function toArray(): array
71
    {
72 3
        $operation = parent::toArray();
73 3
        if (!empty($this->expression)) {
74 2
            $operation[$this->expressionFieldName] = 'SET ' . $operation['UpdateExpression'];
75
        }
76 3
        return $operation;
77
    }
78
}
79