Passed
Push — master ( 0d7615...e53071 )
by Csaba
38s
created

QueryConstructor::createPostQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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