Completed
Push — master ( 3890f4...ffa1fa )
by Guillermo A.
03:24
created

QueryRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 23
c 3
b 0
f 0
dl 0
loc 74
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 5 1
A setPartitionKeyConditionExpression() 0 6 1
A setSortKeyConditionExpression() 0 6 1
A __construct() 0 13 3
1
<?php
2
3
namespace Guillermoandrae\Db\DynamoDb;
4
5
use Aws\DynamoDb\Marshaler;
6
use ErrorException;
7
8
final class QueryRequest extends AbstractSearchRequest
9
{
10
    /**
11
     * @var string The condition that specifies the key values for items to be retrieved.
12
     */
13
    private $keyConditionExpression = '';
14
15
    /**
16
     * Registers the Marshaler, table name, and key conditions with this object.
17
     *
18
     * @param Marshaler $marshaler The Marshaler.
19
     * @param string $tableName The table name.
20
     * @param array $keyConditions OPTIONAL The key conditions.
21
     * @throws ErrorException
22
     */
23 4
    public function __construct(Marshaler $marshaler, string $tableName, array $keyConditions = [])
24
    {
25 4
        parent::__construct($marshaler, $tableName);
26 4
        if (!empty($keyConditions)) {
27 1
            $this->setPartitionKeyConditionExpression(
28 1
                $keyConditions['partition']['name'],
29 1
                $keyConditions['partition']['value']
30
            );
31 1
            if (isset($keyConditions['sort'])) {
32 1
                $this->setSortKeyConditionExpression(
33 1
                    $keyConditions['sort']['name'],
34 1
                    $keyConditions['sort']['operator'],
35 1
                    $keyConditions['sort']['value']
36
                );
37
            }
38
        }
39 4
    }
40
41
    /**
42
     * Sets condition expression for the partition key.
43
     *
44
     * @param string $keyName The name of the partition key.
45
     * @param mixed $value The desired value.
46
     * @return QueryRequest This object.
47
     * @throws ErrorException Thrown when an unsupported operator is requested.
48
     */
49 3
    public function setPartitionKeyConditionExpression(string $keyName, $value): QueryRequest
50
    {
51 3
        $partitionKeyConditionExpression = $this->parseExpression(RequestOperators::EQ, $keyName);
52 3
        $this->addExpressionAttributeValue($keyName, $value);
53 3
        $this->keyConditionExpression = $partitionKeyConditionExpression;
54 3
        return $this;
55
    }
56
57
    /**
58
     * Sets condition expressions for the sort key.
59
     *
60
     * @param string $keyName The name of the sort key.
61
     * @param string $operator The operator.
62
     * @param mixed $value The desired value.
63
     * @return QueryRequest This object.
64
     * @throws ErrorException Thrown when an unsupported operator is requested.
65
     */
66 2
    public function setSortKeyConditionExpression(string $keyName, string $operator, $value): QueryRequest
67
    {
68 2
        $sortKeyConditionExpression = $this->parseExpression($operator, $keyName);
69 2
        $this->addExpressionAttributeValue($keyName, $value);
70 2
        $this->keyConditionExpression .= ' AND ' . $sortKeyConditionExpression;
71 2
        return $this;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 4
    public function get(): array
78
    {
79 4
        $query = parent::get();
80 4
        $query['KeyConditionExpression'] = $this->keyConditionExpression;
81 4
        return $query;
82
    }
83
}
84