Passed
Pull Request — master (#84)
by Csaba
02:32
created

QueryConstructor::createGetQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
c 1
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
crap 2
1
<?php
2
namespace Fathomminds\Rest\Database\DynamoDb;
3
4
use Aws\DynamoDb\DynamoDbClient;
5
use Aws\DynamoDb\Marshaler;
6
7
class QueryConstructor
8
{
9
    public function marshalItem($resource)
10
    {
11
        $marshaler = new Marshaler;
12
        $toMarshal = $resource;
13
        if (gettype($resource) === 'object') {
14
            $toMarshal = new \StdClass;
15
            foreach (get_object_vars($resource) as $name => $value) {
16
                $toMarshal->$name = $value;
17
            }
18
        }
19
        return $marshaler->marshalItem($toMarshal);
20
    }
21
22
    public function unmarshalItem($item)
23
    {
24
        $marshaler = new Marshaler;
25
        if ($item === null) {
26
            return new \StdClass;
27
        }
28
        return $marshaler->unmarshalItem($item, true);
29
    }
30
31
    public function unmarshalBatch($itemList)
32
    {
33
        if ($itemList === null) {
34
            return [];
35
        }
36
        $list = [];
37
        foreach ($itemList as $item) {
38
            $list[] = $this->unmarshalItem($item);
39
        }
40
        return $list;
41
    }
42
43
    public function createGetQuery($tableName, $primaryKey, $resourceId)
44
    {
45
        $query = [
46
            'TableName' => $tableName,
47
            'Key' => $this->marshalItem([$primaryKey => $resourceId]),
48
        ];
49
        return $query;
50
    }
51
52
    public function createScanQuery($tableName)
53
    {
54
        $query = [
55
            'TableName' => $tableName,
56
        ];
57
        return $query;
58
    }
59
60
    public function createPostQuery($tableName, $primaryKey, $newResource)
61
    {
62
        $query = [
63
            'TableName' => $tableName,
64
            'Item' => $this->marshalItem($newResource),
65
            'ConditionExpression' => 'attribute_not_exists(#pk)',
66
            "ExpressionAttributeNames" => ["#pk" => $primaryKey],
67
        ];
68
        return $query;
69
    }
70
71
    public function createPutQuery($tableName, $primaryKey, $newResource)
72
    {
73
        $query = [
74
            'TableName' => $tableName,
75
            'Item' => $this->marshalItem($newResource),
76
            'ConditionExpression' => 'attribute_exists(#pk)',
77
            "ExpressionAttributeNames" => ["#pk" => $primaryKey],
78
        ];
79
        return $query;
80
    }
81
82
    public function createDeleteQuery($tableName, $primaryKey, $resourceId)
83
    {
84
        $query = [
85
            'TableName' => $tableName,
86
            'Key' => $this->marshalItem([$primaryKey => $resourceId]),
87
        ];
88
        return $query;
89
    }
90
}
91