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

AbstractItemOperation::setPrimaryKey()   A

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
abstract class AbstractItemOperation extends AbstractOperation implements ItemOperationInterface
9
{
10
    use FilterExpressionAwareOperationTrait, ReturnConsumedCapacityAwareOperationTrait {
11
        FilterExpressionAwareOperationTrait::toArray as filterExpressionAwareTraitToArray;
12
        ReturnConsumedCapacityAwareOperationTrait::toArray as returnConsumedCapacityAwareTraitToArray;
13
    }
14
15
    /**
16
     * @var array The primary key.
17
     */
18
    protected $primaryKey;
19
20
    /**
21
     * Registers the DynamoDb client, Marshaler, and table name with this object.
22
     *
23
     * @param DynamoDbClient $client The DynamoDb client.
24
     * @param Marshaler $marshaler The Marshaler.
25
     * @param string $tableName The table name.
26
     * @param array $primaryKey The primary key.
27
     */
28 4
    public function __construct(DynamoDbClient $client, Marshaler $marshaler, string $tableName, array $primaryKey)
29
    {
30 4
        $this->setClient($client);
31 4
        $this->setMarshaler($marshaler);
32 4
        $this->setTableName($tableName);
33 4
        $this->setPrimaryKey($primaryKey);
34 4
    }
35
36
    /**
37
     * Registers the operation's primary key with this object.
38
     *
39
     * @param array $primaryKey The primary key values to be used when retrieving items.
40
     * @return mixed An implementation of this abstract.
41
     */
42 4
    public function setPrimaryKey(array $primaryKey)
43
    {
44 4
        $this->primaryKey = $this->getMarshaler()->marshalItem($primaryKey);
45 4
        return $this;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51 7
    public function toArray(): array
52
    {
53 7
        $query = parent::toArray();
54 7
        $query += $this->returnConsumedCapacityAwareTraitToArray();
55 7
        $query += $this->filterExpressionAwareTraitToArray();
56 7
        $query['Key'] = $this->primaryKey;
57 7
        return $query;
58
    }
59
}
60