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
|
17 |
|
public function __construct($resourceName, $primaryKey, Client $client = null, $databaseName = null) |
19
|
|
|
{ |
20
|
17 |
|
$this->resourceName = $resourceName; |
21
|
17 |
|
$this->primaryKey = $primaryKey; |
22
|
17 |
|
$this->client = $client === null ? new Client : $client; |
23
|
17 |
|
$this->databaseName = $databaseName === null ? getenv('CLUSTERPOINT_DATABASE') : $databaseName; |
24
|
17 |
|
$this->collection = $this->client->database($this->databaseName . '.' . $this->resourceName); |
25
|
17 |
|
} |
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
|
3 |
|
public function patch($resourceId, $newResource) |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
3 |
|
$res = $this->collection->update($resourceId, $newResource->toArray()); |
68
|
2 |
|
$this->failOnError($res); |
69
|
2 |
|
return $this->toObject($res); |
70
|
1 |
|
} catch (\Exception $ex) { |
71
|
1 |
|
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
|
14 |
|
protected function failOnError($res) |
98
|
|
|
{ |
99
|
14 |
|
if (empty($res->error())) { |
100
|
11 |
|
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
|
11 |
|
protected function extractResult($cpResponse) |
115
|
|
|
{ |
116
|
11 |
|
$res = json_decode($cpResponse->rawResponse()); |
117
|
11 |
|
if (!property_exists($res, 'results')) { |
118
|
4 |
|
return null; |
119
|
|
|
} |
120
|
7 |
|
return $res->results; |
121
|
|
|
} |
122
|
|
|
|
123
|
10 |
|
protected function toObject($cpResponse) |
124
|
|
|
{ |
125
|
10 |
|
$res = $this->extractResult($cpResponse); |
126
|
10 |
|
if (count($res) === 1) { |
127
|
4 |
|
return $res[0]; |
128
|
|
|
} |
129
|
6 |
|
return new \StdClass; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
protected function toObjectArray($cpResponse) |
133
|
|
|
{ |
134
|
1 |
|
return $this->extractResult($cpResponse); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|