Test Failed
Pull Request — develop (#43)
by
unknown
01:46
created

Resource::patch()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

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