DynamoDBGrammar::parseKeyConditionExpression()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
c 0
b 0
f 0
rs 9.8666
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Hoooklife\DynamodbPodm\Grammars;
4
5
use Aws\DynamoDb\Marshaler;
6
use Hoooklife\DynamodbPodm\Collection;
0 ignored issues
show
Bug introduced by
The type Hoooklife\DynamodbPodm\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Hoooklife\DynamodbPodm\DB;
8
use Hoooklife\DynamodbPodm\Query\Builder;
0 ignored issues
show
Bug introduced by
The type Hoooklife\DynamodbPodm\Query\Builder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Hoooklife\DynamodbPodm\Grammars\DynamoDBBuilder;
10
11
class DynamoDBGrammar
12
{
13
    protected $operators = [
14
        'begins_with'
15
    ];
16
17
    protected $params = [];
18
    protected $insertParam = [];
19
20
    /**
21
     * @var Builder
22
     */
23
    private $builder;
24
    /**
25
     * @var array
26
     */
27
    private $config;
28
29
    /** @var \Hoooklife\DynamodbPodm\Grammars\DynamoDBBuilder $dynamoDBBuilder */
30
    private $dynamoDBBuilder;
31
32
    private $attributeValues = [];
33
    private $attributeNames = [];
34
35
36
    /** @var Marshaler $marshaler */
37
    private $marshaler;
38
39
    /**
40
     * DynamoDBGrammar constructor.
41
     * @param Builder $builder
42
     * @param array $config
43
     */
44
    public function __construct(Builder $builder, array $config)
45
    {
46
        $this->builder = $builder;
47
        $this->config = $config;
48
49
        $this->dynamoDBBuilder = new DynamoDBBuilder($config);
50
        $this->marshaler = new Marshaler();
51
52
    }
53
54
    // 表达式解析 where
55
    private function parseKeyConditionExpression()
56
    {
57
        $expression = [];
58
59
        foreach ($this->builder->wheres as $valueIndex => $where) {
60
            $columnIndex = count($this->attributeNames);
61
            switch (strtolower($where['operator'])) {
62
                case "begins_with":
63
                    $expression[] = "begins_with(#$columnIndex, :{$valueIndex})";
64
                    break;
65
                default:
66
                    $expression[] = "#$columnIndex {$where['operator']} :{$valueIndex}";
67
            }
68
69
            // param bind
70
            $this->attributeValues[':' . $valueIndex] = $where['value'];
71
            $this->attributeNames['#' . $columnIndex] = $where['column'];
72
        }
73
        return implode(" and ", $expression);
74
    }
75
76
    public function parseExpressionAttributeValues()
77
    {
78
        return $this->marshaler->marshalItem($this->attributeValues);
79
    }
80
81
82
    // select
83
    public function parseProjectionExpression()
84
    {
85
        $tmp = [];
86
        foreach ($this->builder->columns as $column) {
87
            $index = count($this->attributeNames);
88
            $this->attributeNames["#{$index}"] = $column;
89
            $tmp[] = "#{$index}";
90
        }
91
        return implode(",", $tmp);
92
93
    }
94
95
    // limit
96
    public function parseLimit()
97
    {
98
        return $this->builder->limit;
99
    }
100
101
    /**
102
     * Get the grammar specific operators.
103
     *
104
     * @return array
105
     */
106
    public function getOperators()
107
    {
108
        return $this->operators;
109
    }
110
111
    public function insert($data)
112
    {
113
        $this->createInsertParam($data);
114
115
        $builder = $this->dynamoDBBuilder
116
            ->setRequestItems([$this->builder->table => $this->insertParam]);
117
118
        return $builder->batchWriteItem();
0 ignored issues
show
Bug introduced by
The method batchWriteItem() does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBBuilder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

118
        return $builder->/** @scrutinizer ignore-call */ batchWriteItem();
Loading history...
119
    }
120
121
    protected function createInsertParam($data)
122
    {
123
        foreach ($data as $key => $value) {
124
            if (is_numeric($key) && is_array($value)) {
125
                $this->createInsertParam($value);
126
            } else {
127
                $this->insertParam[] = [
128
                    'PutRequest' => [
129
                        'Item' => $this->marshaler->marshalItem($data)
130
                    ]
131
                ];
132
                break;
133
            }
134
        }
135
    }
136
137
138
    public function all(): Collection
139
    {
140
141
        $builder = $this->dynamoDBBuilder
0 ignored issues
show
Unused Code introduced by
The assignment to $builder is dead and can be removed.
Loading history...
142
            ->setTableName($this->builder->table)
143
            ->setKeyConditionExpression($this->parseKeyConditionExpression())
144
            ->setExpressionAttributeValues($this->parseExpressionAttributeValues())
145
            ->setProjectionExpression($this->parseProjectionExpression())
146
            ->setExpressionAttributeNames($this->attributeNames)
147
            ->prepare()
148
            ->query();
0 ignored issues
show
Bug introduced by
The method query() does not exist on Hoooklife\DynamodbPodm\DynamoDB\ExecutableQuery. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

148
            ->/** @scrutinizer ignore-call */ query();
Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Hoooklife\DynamodbPodm\Collection. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
149
    }
150
151
    public function delete()
152
    {
153
        $builder = $this->dynamoDBBuilder
0 ignored issues
show
Unused Code introduced by
The assignment to $builder is dead and can be removed.
Loading history...
154
            ->setTableName($this->builder->table)
155
            ->setExpressionAttributeValues($this->parseExpressionAttributeValues());
156
157
    }
158
159
    public function getBuilder()
160
    {
161
        return $this->dynamoDBBuilder;
162
    }
163
}