Completed
Push — master ( ff3d5b...77ffdb )
by hook
06:35
created

DynamoDBBuilder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 24
rs 10
c 1
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A __call() 0 11 2
1
<?php
2
3
use BadMethodCallException;
4
5
/**
6
 * Class QueryBuilder
7
 *
8
 * @package BaoPham\DynamoDb\DynamoDb
9
 *
10
 * Methods are in the form of `set<key_name>`, where `<key_name>`
11
 * is the key name of the query body to be sent.
12
 *
13
 * For example, to build a query:
14
 * [
15
 *     'AttributeDefinitions' => ...,
16
 *     'GlobalSecondaryIndexUpdates' => ...
17
 *     'TableName' => ...
18
 * ]
19
 *
20
 * Do:
21
 *
22
 * $query = $query->setAttributeDefinitions(...)->setGlobalSecondaryIndexUpdates(...)->setTableName(...);
23
 *
24
 * When ready:
25
 *
26
 * $query->prepare()->updateTable();
27
 *
28
 * Common methods:
29
 *
30
 * @method QueryBuilder setExpressionAttributeNames(array $mapping)
31
 * @method QueryBuilder setExpressionAttributeValues(array $mapping)
32
 * @method QueryBuilder setFilterExpression(string $expression)
33
 * @method QueryBuilder setKeyConditionExpression(string $expression)
34
 * @method QueryBuilder setProjectionExpression(string $expression)
35
 * @method QueryBuilder setUpdateExpression(string $expression)
36
 * @method QueryBuilder setAttributeUpdates(array $updates)
37
 * @method QueryBuilder setConsistentRead(bool $consistent)
38
 * @method QueryBuilder setScanIndexForward(bool $forward)
39
 * @method QueryBuilder setExclusiveStartKey(mixed $key)
40
 * @method QueryBuilder setReturnValues(string $type)
41
 * @method QueryBuilder setRequestItems(array $items)
42
 * @method QueryBuilder setTableName(string $table)
43
 * @method QueryBuilder setIndexName(string $index)
44
 * @method QueryBuilder setSelect(string $select)
45
 * @method QueryBuilder setItem(array $item)
46
 * @method QueryBuilder setKeys(array $keys)
47
 * @method QueryBuilder setLimit(int $limit)
48
 * @method QueryBuilder setKey(array $key)
49
 */
50
class DynamoDBBuilder{
51
    /**
52
     * Query body to be sent to AWS
53
     *
54
     * @var array
55
     */
56
    public $query = [];
57
58
    /**
59
     * @param  string $method
60
     * @param  array  $parameters
61
     * @return mixed
62
     */
63
    public function __call($method, $parameters)
64
    {
65
        if (starts_with($method, 'set')) {
0 ignored issues
show
Bug introduced by
The function starts_with was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        if (/** @scrutinizer ignore-call */ starts_with($method, 'set')) {
Loading history...
66
            $key = array_reverse(explode('set', $method, 2))[0];
67
            $this->query[$key] = current($parameters);
68
            return $this;
69
        }
70
        throw new BadMethodCallException(sprintf(
71
            'Method %s::%s does not exist.',
72
            static::class,
73
            $method
74
        ));
75
    }
76
}