Completed
Push — master ( a7a349...3fc402 )
by Guillermo A.
03:23
created

QueryOperation::setSortKeyConditionExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
ccs 5
cts 5
cp 1
cc 1
nc 1
nop 3
crap 1
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\Common\Collection;
10
use Guillermoandrae\Common\CollectionInterface;
11
use Guillermoandrae\DynamoDb\Constant\Operators;
12
use Guillermoandrae\DynamoDb\Contract\AbstractSearchOperation;
13
use Guillermoandrae\DynamoDb\Exception;
14
15
final class QueryOperation extends AbstractSearchOperation
16
{
17
    /**
18
     * @var string The condition that specifies the key values for items to be retrieved.
19
     */
20
    private $keyConditionExpression = '';
21
22
    /**
23
     * QueryRequest constructor.
24
     *
25
     * @param DynamoDbClient $client The DynamoDb client.
26
     * @param Marshaler $marshaler The Marshaler.
27
     * @param string $tableName The table name.
28
     * @param array $keyConditions OPTIONAL The key conditions.
29
     * @throws ErrorException
30
     */
31 4
    public function __construct(
32
        DynamoDbClient $client,
33
        Marshaler $marshaler,
34
        string $tableName,
35
        array $keyConditions = []
36
    ) {
37 4
        parent::__construct($client, $marshaler, $tableName);
38 4
        if (!empty($keyConditions)) {
39 1
            $this->setPartitionKeyConditionExpression(
40 1
                $keyConditions['partition']['name'],
41 1
                $keyConditions['partition']['value']
42
            );
43 1
            if (isset($keyConditions['sort'])) {
44 1
                $this->setSortKeyConditionExpression(
45 1
                    $keyConditions['sort']['name'],
46 1
                    $keyConditions['sort']['operator'],
47 1
                    $keyConditions['sort']['value']
48
                );
49
            }
50
        }
51 4
    }
52
53
    /**
54
     * Sets condition expression for the partition key.
55
     *
56
     * @param string $keyName The name of the partition key.
57
     * @param mixed $value The desired value.
58
     * @return QueryOperation This object.
59
     * @throws ErrorException Thrown when an unsupported operator is requested.
60
     */
61 3
    public function setPartitionKeyConditionExpression(string $keyName, $value): QueryOperation
62
    {
63 3
        $partitionKeyConditionExpression = $this->parseExpression(Operators::EQ, $keyName);
64 3
        $this->addExpressionAttributeValue($keyName, $value);
65 3
        $this->keyConditionExpression = $partitionKeyConditionExpression;
66 3
        return $this;
67
    }
68
69
    /**
70
     * Sets condition expressions for the sort key.
71
     *
72
     * @param string $keyName The name of the sort key.
73
     * @param string $operator The operator.
74
     * @param mixed $value The desired value.
75
     * @return QueryOperation This object.
76
     * @throws ErrorException Thrown when an unsupported operator is requested.
77
     */
78 2
    public function setSortKeyConditionExpression(string $keyName, string $operator, $value): QueryOperation
79
    {
80 2
        $sortKeyConditionExpression = $this->parseExpression($operator, $keyName);
81 2
        $this->addExpressionAttributeValue($keyName, $value);
82 2
        $this->keyConditionExpression .= ' AND ' . $sortKeyConditionExpression;
83 2
        return $this;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89 4
    public function toArray(): array
90
    {
91 4
        $query = parent::toArray();
92 4
        $query['KeyConditionExpression'] = $this->keyConditionExpression;
93 4
        return $query;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     * @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#query
99
     */
100 2
    public function execute(): CollectionInterface
101
    {
102
        try {
103 2
            $results = $this->client->query($this->toArray());
104 1
            $rows = [];
105 1
            foreach ($results['Items'] as $item) {
106 1
                $rows[] = $this->marshaler->unmarshalItem($item);
107
            }
108 1
            return Collection::make($rows);
109 1
        } catch (DynamoDbException $ex) {
110 1
            throw new Exception($ex->getMessage());
111
        }
112
    }
113
}
114