Completed
Push — master ( be4d4a...bfa2f5 )
by hook
02:34
created

DynamoDBBuilder::scan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hoooklife\DynamodbPodm\Grammars;
4
5
use Aws\DynamoDb\DynamoDbClient;
6
7
/**
8
 * Class DynamoDBBuilder
9
 *
10
 * @package BaoPham\DynamoDb\DynamoDb
11
 *
12
 * Methods are in the form of `set<key_name>`, where `<key_name>`
13
 * is the key name of the query body to be sent.
14
 *
15
 * For example, to build a query:
16
 * [
17
 *     'AttributeDefinitions' => ...,
18
 *     'GlobalSecondaryIndexUpdates' => ...
19
 *     'TableName' => ...
20
 * ]
21
 *
22
 * Do:
23
 *
24
 * $query = $query->setAttributeDefinitions(...)->setGlobalSecondaryIndexUpdates(...)->setTableName(...);
25
 *
26
 * When ready:
27
 *
28
 * $query->prepare()->updateTable();
29
 *
30
 * Common methods:
31
 *
32
 * @method DynamoDBBuilder setExpressionAttributeNames(array $mapping)
33
 * @method DynamoDBBuilder setFilterExpression(string $expression)
34
 * @method DynamoDBBuilder setUpdateExpression(string $expression)
35
 * @method DynamoDBBuilder setAttributeUpdates(array $updates)
36
 * @method DynamoDBBuilder setConsistentRead(bool $consistent)
37
 * @method DynamoDBBuilder setScanIndexForward(bool $forward)
38
 * @method DynamoDBBuilder setExclusiveStartKey(mixed $key)
39
 * @method DynamoDBBuilder setReturnValues(string $type)
40
 * @method DynamoDBBuilder setTableName(string $table)
41
 * @method DynamoDBBuilder setIndexName(string $index)
42
 * @method DynamoDBBuilder setSelect(string $select)
43
 * @method DynamoDBBuilder setItem(array $item)
44
 * @method DynamoDBBuilder setKeys(array $keys)
45
 * @method DynamoDBBuilder setLimit(int $limit)
46
 * @method DynamoDBBuilder setKey(array $key)
47
 */
48
class DynamoDBBuilder
49
{
50
    /**
51
     * Query body to be sent to AWS
52
     *
53
     * @var array
54
     */
55
    public $query = [];
56
    public $batchWriteItem = [];
57
58
    /** @var $dynamodbClient DynamoDbClient */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $dynamodbClient at position 0 could not be parsed: Unknown type name '$dynamodbClient' at position 0 in $dynamodbClient.
Loading history...
59
    protected $dynamodbClient;
60
61
    public function __construct(array $config)
62
    {
63
        $this->dynamodbClient = new DynamoDbClient($config["S3Config"]);
64
    }
65
66
    public function hydrate(array $query)
67
    {
68
        $this->query = $query;
69
        return $this;
70
    }
71
72
    public function setExpressionAttributeName($placeholder, $name)
73
    {
74
        $this->query['ExpressionAttributeNames'][$placeholder] = $name;
75
        return $this;
76
    }
77
78
    public function setExpressionAttributeValue($placeholder, $value)
79
    {
80
        $this->query['ExpressionAttributeValues'][$placeholder] = $value;
81
        return $this;
82
    }
83
84
    public function setKeyConditionExpression($mapping)
85
    {
86
        if ($mapping) {
87
            $this->query['KeyConditionExpression'] = $mapping;
88
        }
89
        return $this;
90
    }
91
92
    public function setProjectionExpression($expression)
93
    {
94
        if ($expression) {
95
            $this->query['ProjectionExpression'] = $expression;
96
        }
97
        return $this;
98
    }
99
100
    public function setExpressionAttributeValues($mapping)
101
    {
102
        if ($mapping) {
103
            $this->query['ExpressionAttributeValues'] = $mapping;
104
        }
105
        return $this;
106
    }
107
108
    public function setRequestItems($items)
109
    {
110
        $this->batchWriteItem['RequestItems'] = $items;
111
        return $this;
112
    }
113
114
    public function batchWriteItem()
115
    {
116
//        var_dump($this->batchWriteItem);
117
        return $this->dynamodbClient->batchWriteItem($this->batchWriteItem);
118
    }
119
120
    public function scan()
121
    {
122
        return $this->dynamodbClient->scan($this->query);
123
    }
124
125
    public function query()
126
    {
127
//        var_dump($this->query);
128
        return $this->dynamodbClient->query($this->query);
129
    }
130
131
    /**
132
     * @param  string $method
133
     * @param  array $parameters
134
     * @return mixed
135
     */
136
    public function __call($method, $parameters)
137
    {
138
        if (strpos($method, 'set') === 0) {
139
            $key = array_reverse(explode('set', $method, 2))[0];
140
            $this->query[$key] = current($parameters);
141
            return $this;
142
        }
143
        throw new BadMethodCallException(sprintf(
0 ignored issues
show
Bug introduced by
The type Hoooklife\DynamodbPodm\G...\BadMethodCallException was not found. Did you mean BadMethodCallException? If so, make sure to prefix the type with \.
Loading history...
144
            'Method %s::%s does not exist.',
145
            static::class,
146
            $method
147
        ));
148
    }
149
150
151
}