Completed
Push — master ( b23853...6c4ac7 )
by hook
05:18 queued 02:51
created

DynamoDBBuilder::setExpressionAttributeValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
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 setProjectionExpression(string $expression)
35
 * @method DynamoDBBuilder setUpdateExpression(string $expression)
36
 * @method DynamoDBBuilder setAttributeUpdates(array $updates)
37
 * @method DynamoDBBuilder setConsistentRead(bool $consistent)
38
 * @method DynamoDBBuilder setScanIndexForward(bool $forward)
39
 * @method DynamoDBBuilder setExclusiveStartKey(mixed $key)
40
 * @method DynamoDBBuilder setReturnValues(string $type)
41
 * @method DynamoDBBuilder setTableName(string $table)
42
 * @method DynamoDBBuilder setIndexName(string $index)
43
 * @method DynamoDBBuilder setSelect(string $select)
44
 * @method DynamoDBBuilder setItem(array $item)
45
 * @method DynamoDBBuilder setKeys(array $keys)
46
 * @method DynamoDBBuilder setLimit(int $limit)
47
 * @method DynamoDBBuilder setKey(array $key)
48
 */
49
class DynamoDBBuilder
50
{
51
    /**
52
     * Query body to be sent to AWS
53
     *
54
     * @var array
55
     */
56
    public $query = [];
57
    public $batchWriteItem = [];
58
59
    /** @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...
60
    protected $dynamodbClient;
61
62
    public function __construct(array $config)
63
    {
64
        $this->dynamodbClient = new DynamoDbClient($config["S3Config"]);
65
    }
66
67
    public function hydrate(array $query)
68
    {
69
        $this->query = $query;
70
        return $this;
71
    }
72
73
    public function setExpressionAttributeName($placeholder, $name)
74
    {
75
        $this->query['ExpressionAttributeNames'][$placeholder] = $name;
76
        return $this;
77
    }
78
79
    public function setExpressionAttributeValue($placeholder, $value)
80
    {
81
        $this->query['ExpressionAttributeValues'][$placeholder] = $value;
82
        return $this;
83
    }
84
85
    public function setKeyConditionExpression($mapping)
86
    {
87
        if ($mapping) {
88
            $this->query['KeyConditionExpression'] = $mapping;
89
        }
90
        return $this;
91
    }
92
93
    public function setExpressionAttributeValues($mapping)
94
    {
95
        if ($mapping) {
96
            $this->query['ExpressionAttributeValues'] = $mapping;
97
        }
98
        return $this;
99
    }
100
101
    public function setRequestItems($items)
102
    {
103
        $this->batchWriteItem['RequestItems'] = $items;
104
        return $this;
105
    }
106
107
    public function batchWriteItem()
108
    {
109
        var_dump($this->batchWriteItem);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->batchWriteItem) looks like debug code. Are you sure you do not want to remove it?
Loading history...
110
        return $this->dynamodbClient->batchWriteItem($this->batchWriteItem);
111
    }
112
113
    public function scan()
114
    {
115
        return $this->dynamodbClient->scan($this->query);
116
    }
117
118
    public function query()
119
    {
120
        var_dump($this->query);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->query) looks like debug code. Are you sure you do not want to remove it?
Loading history...
121
        return $this->dynamodbClient->query($this->query);
122
    }
123
124
    /**
125
     * @param  string $method
126
     * @param  array $parameters
127
     * @return mixed
128
     */
129
    public function __call($method, $parameters)
130
    {
131
        if (strpos($method, 'set') === 0) {
132
            $key = array_reverse(explode('set', $method, 2))[0];
133
            $this->query[$key] = current($parameters);
134
            return $this;
135
        }
136
        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...
137
            'Method %s::%s does not exist.',
138
            static::class,
139
            $method
140
        ));
141
    }
142
143
144
}