AbstractItemOperation::setPrimaryKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Guillermoandrae\DynamoDb\Contract;
4
5
use Aws\DynamoDb\DynamoDbClient;
6
use Aws\DynamoDb\Marshaler;
7
8
/**
9
 * Abstract for item operations.
10
 *
11
 * @author Guillermo A. Fisher <[email protected]>
12
 */
13
abstract class AbstractItemOperation extends AbstractOperation implements ItemOperationInterface
14
{
15
    use TableAwareOperationTrait,
16
        ExpressionAttributeValueAwareOperationTrait,
17
        ReturnConsumedCapacityAwareOperationTrait {
18
        TableAwareOperationTrait::toArray as tableAwareTraitToArray;
19
        ExpressionAttributeValueAwareOperationTrait::toArray as expressionAwareTraitToArray;
20
        ReturnConsumedCapacityAwareOperationTrait::toArray as returnConsumedCapacityAwareTraitToArray;
21
    }
22
23
    /**
24
     * @var array The primary key.
25
     */
26
    protected array $primaryKey = [];
27
28
    /**
29
     * Registers the DynamoDb client, Marshaler, table name, and primary key 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.
35
     */
36 7
    public function __construct(DynamoDbClient $client, Marshaler $marshaler, string $tableName, array $primaryKey)
37
    {
38 7
        parent::__construct($client, $marshaler);
39 7
        $this->setTableName($tableName);
40 7
        $this->setPrimaryKey($primaryKey);
41 7
    }
42
43 7
    final public function setPrimaryKey(array $primaryKey): ItemOperationInterface
44
    {
45 7
        $this->primaryKey = $this->getMarshaler()->marshalItem($primaryKey);
46 7
        return $this;
47
    }
48
49 12
    public function toArray(): array
50
    {
51 12
        $operation = $this->tableAwareTraitToArray();
52 12
        $operation += $this->returnConsumedCapacityAwareTraitToArray();
53 12
        $operation += $this->expressionAwareTraitToArray();
54 12
        $operation['Key'] = $this->primaryKey;
55 12
        return $operation;
56
    }
57
}
58