Completed
Pull Request — master (#142)
by Bao
06:40
created

QueryBuilder::prepare()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BaoPham\DynamoDb\DynamoDb;
4
5
use Aws\DynamoDb\DynamoDbClient;
6
use BadMethodCallException;
7
use BaoPham\DynamoDb\DynamoDbClientInterface;
8
use BaoPham\DynamoDb\RawDynamoDbQuery;
9
10
/**
11
 * Class QueryBuilder
12
 *
13
 * @package BaoPham\DynamoDb\DynamoDb
14
 *
15
 * Methods are in the form of `set<key_name>`, where `<key_name>`
16
 * is the key name of the query body to be sent.
17
 *
18
 * For example, to build a query:
19
 * [
20
 *     'AttributeDefinitions' => ...,
21
 *     'GlobalSecondaryIndexUpdates' => ...
22
 *     'TableName' => ...
23
 * ]
24
 *
25
 * Do:
26
 *
27
 * $query = $query->setAttributeDefinitions(...)->setGlobalSecondaryIndexUpdates(...)->setTableName(...);
28
 *
29
 * When ready:
30
 *
31
 * $query->prepare()->updateTable();
32
 *
33
 * Common methods:
34
 *
35
 * @method QueryBuilder setExpressionAttributeNames(array $mapping)
36
 * @method QueryBuilder setExpressionAttributeValues(array $mapping)
37
 * @method QueryBuilder setFilterExpression(string $expression)
38
 * @method QueryBuilder setKeyConditionExpression(string $expression)
39
 * @method QueryBuilder setProjectionExpression(string $expression)
40
 * @method QueryBuilder setUpdateExpression(string $expression)
41
 * @method QueryBuilder setAttributeUpdates(array $updates)
42
 * @method QueryBuilder setConsistentRead(bool $consistent)
43
 * @method QueryBuilder setScanIndexForward(bool $forward)
44
 * @method QueryBuilder setExclusiveStartKey(mixed $key)
45
 * @method QueryBuilder setReturnValues(string $type)
46
 * @method QueryBuilder setRequestItems(array $items)
47
 * @method QueryBuilder setTableName(string $table)
48
 * @method QueryBuilder setIndexName(string $index)
49
 * @method QueryBuilder setSelect(string $select)
50
 * @method QueryBuilder setItem(array $item)
51
 * @method QueryBuilder setKeys(array $keys)
52
 * @method QueryBuilder setLimit(int $limit)
53
 * @method QueryBuilder setKey(array $key)
54
 */
55
class QueryBuilder
56
{
57
    /**
58
     * @var DynamoDbClientInterface
59
     */
60
    private $service;
61
62
    /**
63
     * Query body to be sent to AWS
64
     *
65
     * @var array
66
     */
67
    public $query = [];
68
69 123
    public function __construct(DynamoDbClientInterface $service)
70
    {
71 123
        $this->service = $service;
72 123
    }
73
74
    public function hydrate(array $query)
75
    {
76
        $this->query = $query;
77
78
        return $this;
79
    }
80
81 3
    public function setExpressionAttributeName($placeholder, $name)
82
    {
83 3
        $this->query['ExpressionAttributeNames'][$placeholder] = $name;
84
85 3
        return $this;
86
    }
87
88 2
    public function setExpressionAttributeValue($placeholder, $value)
89
    {
90 2
        $this->query['ExpressionAttributeValues'][$placeholder] = $value;
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * @param DynamoDbClient|null $client
97
     * @return ExecutableQuery
98
     */
99 121
    public function prepare(DynamoDbClient $client = null)
100
    {
101 121
        $raw = new RawDynamoDbQuery(null, $this->query);
102 121
        return new ExecutableQuery($client ?: $this->service->getClient(), $raw->finalize()->query);
103
    }
104
105
    /**
106
     * @param  string $method
107
     * @param  array  $parameters
108
     * @return mixed
109
     */
110 122
    public function __call($method, $parameters)
111
    {
112 122
        if (starts_with($method, 'set')) {
113 122
            $this->query[str_after($method, 'set')] = current($parameters);
114
115 122
            return $this;
116
        }
117
118
        throw new BadMethodCallException(sprintf(
119
            'Method %s::%s does not exist.',
120
            static::class,
121
            $method
122
        ));
123
    }
124
}
125