Resource::patch()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
c 1
b 0
f 0
ccs 0
cts 9
cp 0
rs 9.2
cc 4
eloc 10
nc 6
nop 2
crap 20
1
<?php
2
namespace Fathomminds\Rest\Database\Clusterpoint;
3
4
use Clusterpoint\Client;
5
use Fathomminds\Rest\Database\Clusterpoint\Connection;
6
use Fathomminds\Rest\Exceptions\RestException;
7
use Fathomminds\Rest\Contracts\IResource;
8
9
class Resource implements IResource
10
{
11
    protected $MAX_LIMIT = 2147483647;
12
    protected $client;
13
    protected $databaseName;
14
    protected $collection;
15
    protected $resourceName;
16
    protected $primaryKey;
17
18
    public function __construct($resourceName, $primaryKey, Client $client = null, $databaseName = null)
19
    {
20
        $this->resourceName = $resourceName;
21
        $this->primaryKey = $primaryKey;
22
        $this->client = $client === null ? new Client : $client;
23
        $this->databaseName = $databaseName === null ? getenv('CLUSTERPOINT_DATABASE') : $databaseName;
24
        $this->collection = $this->client->database($this->databaseName . '.' . $this->resourceName);
25
    }
26
27
    public function get($resourceId = null)
28
    {
29
        if ($resourceId !== null) {
30
            return $this->getOne($resourceId);
31
        }
32
        return $this->getAll();
33
    }
34
35
    protected function getOne($resourceId)
36
    {
37
        $res = $this->collection->find($resourceId);
38
        $this->failOnError($res);
39
        return $this->toObject($res);
40
    }
41
42
    protected function getAll()
43
    {
44
        $res = $this->collection->limit($this->MAX_LIMIT)->get();
45
        $this->failOnError($res);
46
        return $this->toObjectArray($res);
47
    }
48
49
    public function post($newResource)
50
    {
51
        try {
52
            $res = $this->collection->insertOne($newResource);
53
            $this->failOnError($res);
54
            $object = $this->toObject($res);
55
            $newResource->{$this->primaryKey} = empty($object->{$this->primaryKey}) ?
56
                null :
57
                $object->{$this->primaryKey};
58
            return $newResource;
59
        } catch (\Exception $ex) {
60
            throw new RestException($ex->getMessage(), ['result'=>empty($res) ? null : $res]);
61
        }
62
    }
63
64
    public function patch($resourceId, $newResource)
65
    {
66
        try {
67
            if (isset($newResource->{$this->primaryKey})) {
68
                unset($newResource->{$this->primaryKey});
69
            }
70
            $res = $this->collection->updateExisting($resourceId, $newResource->toArray());
71
            $this->failOnError($res);
72
            $newResource->{$this->primaryKey} = $resourceId;
73
            return $newResource;
74
        } catch (\Exception $ex) {
75
            throw new RestException($ex->getMessage(), ['result'=>empty($res) ? null : $res]);
76
        }
77
    }
78
79
    public function put($resourceId, $newResource)
80
    {
81
        try {
82
            if (isset($newResource->{$this->primaryKey})) {
83
                unset($newResource->{$this->primaryKey});
84
            }
85
            $res = $this->collection->replace($resourceId, $newResource);
86
            $this->failOnError($res);
87
            $newResource->{$this->primaryKey} = $resourceId;
88
            return $newResource;
89
        } catch (\Exception $ex) {
90
            throw new RestException($ex->getMessage(), ['result'=>empty($res) ? null : $res]);
91
        }
92
    }
93
94
    public function delete($resourceId)
95
    {
96
        try {
97
            $res = $this->collection->delete($resourceId);
98
            $this->failOnError($res);
99
            return $this->toObject($res);
100
        } catch (\Exception $ex) {
101
            throw new RestException($ex->getMessage(), ['result'=>empty($res) ? null : $res]);
102
        }
103
    }
104
105
    protected function failOnError($res)
106
    {
107
        if (empty($res->error())) {
108
            return;
109
        }
110
        $message = $res->error()[0]->message === 'Requested document does not exist' ?
111
            'Resource does not exist' : 'Database operation failed';
112
        throw new RestException(
113
            $message,
114
            [
115
                'error' => $res->error(),
116
                'res' => $res,
117
            ]
118
        );
119
    }
120
121
    protected function extractResult($cpResponse)
122
    {
123
        $res = json_decode($cpResponse->rawResponse());
124
        if (!property_exists($res, 'results')) {
125
            return null;
126
        }
127
        return $res->results;
128
    }
129
130
    protected function toObject($cpResponse)
131
    {
132
        $res = $this->extractResult($cpResponse);
133
        if (count($res) === 1) {
134
            return $res[0];
135
        }
136
        return new \StdClass;
137
    }
138
139
    protected function toObjectArray($cpResponse)
140
    {
141
        return $this->extractResult($cpResponse);
142
    }
143
}
144