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

DynamoDBGrammar   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A all() 0 7 1
A parseProjectionExpression() 0 6 2
A insert() 0 8 1
A createInsertParam() 0 12 4
A parseExpressionAttributeValues() 0 3 1
A parseLimit() 0 3 1
A getOperators() 0 3 1
A parseKeyConditionExpression() 0 9 2
1
<?php
2
3
namespace Hoooklife\DynamodbPodm\Grammars;
4
5
use Aws\DynamoDb\Marshaler;
6
use Hoooklife\DynamodbPodm\Collection;
7
use Hoooklife\DynamodbPodm\DB;
8
use Hoooklife\DynamodbPodm\Query\Builder;
9
use Hoooklife\DynamodbPodm\Grammars\DynamoDBBuilder;
10
11
class DynamoDBGrammar
12
{
13
    protected $operators = [];
14
15
    protected $params = [];
16
17
    protected $insertParam = [];
18
19
    /**
20
     * @var Builder
21
     */
22
    private $builder;
23
    /**
24
     * @var array
25
     */
26
    private $config;
27
28
    private $dynamoDBBuilder;
29
30
    private $attributeValues = [];
31
32
    /** @var Marshaler $marshaler */
33
    private $marshaler;
34
35
    /**
36
     * DynamoDBGrammar constructor.
37
     * @param Builder $builder
38
     * @param array $config
39
     */
40
    public function __construct(Builder $builder, array $config)
41
    {
42
        $this->builder = $builder;
43
        $this->config = $config;
44
45
        $this->dynamoDBBuilder = new DynamoDBBuilder($config);
46
        $this->marshaler = new Marshaler();
47
48
    }
49
50
    // 表达式解析 where
51
    private function parseKeyConditionExpression()
52
    {
53
        $expression = [];
54
        foreach ($this->builder->wheres as $index => $where) {
55
            $expression[] = "{$where['column']} {$where['operator']} :{$index}";
56
            // param bind
57
            $this->attributeValues[':' . $index] = $where['value'];
58
        }
59
        return implode("and", $expression);
60
    }
61
62
    public function parseExpressionAttributeValues()
63
    {
64
        return $this->marshaler->marshalItem($this->attributeValues);
65
    }
66
67
    // select
68
    public function parseProjectionExpression()
69
    {
70
        if (reset($this->builder->columns) != '*') {
71
            return implode(",", $this->columns);
0 ignored issues
show
Bug Best Practice introduced by
The property columns does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar. Did you maybe forget to declare it?
Loading history...
72
        }
73
        return null;
74
    }
75
76
    // limit
77
    public function parseLimit()
78
    {
79
        return $this->builder->limit;
80
    }
81
82
    /**
83
     * Get the grammar specific operators.
84
     *
85
     * @return array
86
     */
87
    public function getOperators()
88
    {
89
        return $this->operators;
90
    }
91
92
    public function insert($data)
93
    {
94
        $this->createInsertParam($data);
95
96
        $builder = $this->dynamoDBBuilder
97
            ->setRequestItems([$this->builder->table => $this->insertParam]);
98
99
        return $builder->batchWriteItem();
100
    }
101
102
    protected function createInsertParam($data)
103
    {
104
        foreach ($data as $key => $value) {
105
            if (is_numeric($key) && is_array($value)) {
106
                $this->createInsertParam($value);
107
            } else {
108
                $this->insertParam[] = [
109
                    'PutRequest' => [
110
                        'Item' => $this->marshaler->marshalItem($data)
111
                    ]
112
                ];
113
                break;
114
            }
115
        }
116
    }
117
118
    public function all($columns): Collection
0 ignored issues
show
Unused Code introduced by
The parameter $columns is not used and could be removed. ( Ignorable by Annotation )

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

118
    public function all(/** @scrutinizer ignore-unused */ $columns): Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        $builder = $this->dynamoDBBuilder
121
            ->setTableName($this->builder->table)
122
            ->setKeyConditionExpression($this->parseKeyConditionExpression())
123
            ->setExpressionAttributeValues($this->parseExpressionAttributeValues());
124
        return new Collection($builder->query());
125
    }
126
127
128
}