Driver::remove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Respect\Structural\Driver\DynamoDb;
4
5
use Aws\DynamoDb\DynamoDbClient;
6
use Aws\DynamoDb\Marshaler;
7
use Ramsey\Uuid\Uuid;
8
use Ramsey\Uuid\UuidFactoryInterface;
9
use Respect\Data\CollectionIterator;
10
use Respect\Data\Collections\Collection;
11
use Respect\Structural\Driver as BaseDriver;
12
13
class Driver implements BaseDriver
14
{
15
    /**
16
     * @var \Aws\DynamoDb\DynamoDbClient
17
     */
18
    private $connection;
19
20
    /**
21
     * @var \Aws\DynamoDb\Marshaler
22
     */
23
    private $marshaler;
24
25
    /**
26
     * @var UuidFactoryInterface
27
     */
28
    private $uuid;
29
30
    /**
31
     * Driver constructor.
32
     *
33
     * @param \Aws\DynamoDb\DynamoDbClient $connection
34
     */
35 11
    public function __construct(DynamoDbClient $connection, UuidFactoryInterface $uuid)
36
    {
37 11
        $this->connection = $connection;
38 11
        $this->marshaler = new Marshaler();
39 11
        $this->uuid = $uuid;
40 11
    }
41
42
    /**
43
     * @return \Aws\DynamoDb\DynamoDbClient
44
     */
45 6
    public function getConnection()
46
    {
47 6
        return $this->connection;
48
    }
49
50
    /**
51
     * @param \Iterator $cursor
52
     *
53
     * @return array
54
     */
55 1
    public function fetch(\Iterator $cursor)
56
    {
57 1
        $data = [];
58
59 1
        if ($cursor->valid()) {
60 1
            $data = $cursor->current();
61 1
            $cursor->next();
62 1
        }
63
64 1
        return $data;
65
    }
66
67
    /**
68
     * @param array $collection
69
     * @param array $query
70
     *
71
     * @return \Iterator
72
     */
73 2
    public function find($collection, array $query = [])
74
    {
75
        $expression = [
76 2
            'TableName' => $collection,
77 2
        ];
78
79 2
        if (!empty($query)) {
80 2
            $expression['ScanFilter'] = $query;
81 2
        }
82
83 2
        $result = $this->getConnection()->scan($expression);
84
85 2
        return $this->formatResults($result);
86
    }
87
88
    /**
89
     * @param Collection $collection
90
     *
91
     * @return array
92
     */
93 3
    public function generateQuery(Collection $collection)
94
    {
95 3
        return $this->parseConditions($collection);
96
    }
97
98
    /**
99
     * @param Collection $collection
100
     *
101
     * @return array
102
     */
103 3
    protected function parseConditions(Collection $collection)
104
    {
105 3
        $collections = iterator_to_array(
106 3
            CollectionIterator::recursive($collection)
107 3
        );
108
109 3
        $collections = array_slice($collections, 1);
110 3
        $condition = $this->getConditionArray($collection);
111
112 3
        foreach ($collections as $name => $coll) {
113 1
            $condition += $this->getConditionArray($coll);
114 3
        }
115
116 3
        return $condition;
117
    }
118
119
    /**
120
     * @param Collection $collection
121
     *
122
     * @return array
123
     */
124 3
    protected function getConditionArray(Collection $collection)
125
    {
126 3
        $condition = $collection->getCondition();
127
128 3
        if (!is_array($condition)) {
129 2
            $condition = ['_id' => $condition];
130 2
        }
131
132 3
        $conditions = [];
133
134 3
        foreach ($condition as $key => $value) {
135
            $conditions = [
136
                $key => [
137
                    'AttributeValueList' => [
138 2
                        $this->marshaler->marshalValue($value),
139 2
                    ],
140 2
                    'ComparisonOperator' => 'EQ',
141 2
                ],
142 2
            ];
143 3
        }
144
145 3
        return $conditions;
146
    }
147
148
    /**
149
     * @param Collection $collection
150
     * @param $document
151
     */
152 1
    public function insert($collection, $document)
153
    {
154 1
        $document->_id = $this->uuid->uuid4()->toString();
155
156
        $args = [
157 1
            'TableName' => $collection,
158 1
            'Item' => $this->marshaler->marshalItem($document),
159 1
        ];
160
161 1
        $this->getConnection()->putItem($args);
162 1
    }
163
164
    /**
165
     * @param Collection $collection
166
     * @param $criteria
167
     * @param $document
168
     */
169 1
    public function update($collection, $criteria, $document)
170
    {
171
        $args = [
172 1
            'TableName' => $collection,
173 1
            'Key' => $this->marshaler->marshalItem($criteria),
174 1
            'AttributeUpdates' => $this->formatAttributes($document),
175 1
        ];
176
177 1
        $this->getConnection()->updateItem($args);
178 1
    }
179
180
    /**
181
     * @param Collection $collection
182
     * @param $criteria
183
     */
184 1
    public function remove($collection, $criteria)
185
    {
186
        $args = [
187 1
            'TableName' => $collection,
188 1
            'Key' => $this->marshaler->marshalItem($criteria),
189 1
        ];
190
191 1
        $this->getConnection()->deleteItem($args);
192 1
    }
193
194
    /**
195
     * @param $values
196
     *
197
     * @return array
198
     */
199 1
    protected function formatAttributes($values)
200
    {
201 1
        $attributes = [];
202
203 1
        foreach ($values as $key => $value) {
204 1
            $attributes[$key] = [
205 1
                'Value' => $this->marshaler->marshalValue($value),
206
            ];
207 1
        }
208
209 1
        return $attributes;
210
    }
211
212 2
    protected function formatResults(\Aws\Result $result)
213
    {
214 2
        $items = new \ArrayIterator();
215
216 2
        if ($result['Count'] === 0) {
217 1
            return $items;
218
        }
219
220 1
        foreach ($result['Items'] as $item) {
221 1
            $items[] = $this->marshaler->unmarshalItem($item);
222 1
        }
223
224 1
        return $items;
225
    }
226
}
227