1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* tubee.io |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tubee\Rest\v1; |
13
|
|
|
|
14
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
15
|
|
|
use Lcobucci\ContentNegotiation\UnformattedResponse; |
16
|
|
|
use Micro\Auth\Identity; |
17
|
|
|
use MongoDB\BSON\ObjectIdInterface; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
20
|
|
|
use Rs\Json\Patch; |
21
|
|
|
use Tubee\Acl; |
22
|
|
|
use Tubee\Collection\Factory as CollectionFactory; |
23
|
|
|
use Tubee\DataObject\Factory as DataObjectFactory; |
24
|
|
|
use Tubee\Log\Factory as LogFactory; |
25
|
|
|
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory; |
26
|
|
|
use Tubee\Rest\Helper; |
27
|
|
|
use Tubee\Rest\Pager; |
28
|
|
|
use Zend\Diactoros\Response; |
29
|
|
|
|
30
|
|
|
class Objects |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* namespace factory. |
34
|
|
|
* |
35
|
|
|
* @var ResourceNamespaceFactory |
36
|
|
|
*/ |
37
|
|
|
protected $namespace_factory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* collection factory. |
41
|
|
|
* |
42
|
|
|
* @var CollectionFactory |
43
|
|
|
*/ |
44
|
|
|
protected $collection_factory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Object factory. |
48
|
|
|
* |
49
|
|
|
* @var DataObjectFactory |
50
|
|
|
*/ |
51
|
|
|
protected $object_factory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Acl. |
55
|
|
|
* |
56
|
|
|
* @var Acl |
57
|
|
|
*/ |
58
|
|
|
protected $acl; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Log factory. |
62
|
|
|
* |
63
|
|
|
* @var LogFactory |
64
|
|
|
*/ |
65
|
|
|
protected $log_factory; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Init. |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function __construct(ResourceNamespaceFactory $namespace_factory, CollectionFactory $collection_factory, DataObjectFactory $object_factory, Acl $acl, LogFactory $log_factory) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$this->namespace_factory = $namespace_factory; |
73
|
|
|
$this->collection_factory = $collection_factory; |
74
|
|
|
$this->acl = $acl; |
75
|
|
|
$this->object_factory = $object_factory; |
76
|
|
|
$this->log_factory = $log_factory; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Entrypoint. |
81
|
|
|
*/ |
82
|
|
|
public function getAll(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection): ResponseInterface |
83
|
|
|
{ |
84
|
|
|
$query = array_merge([ |
85
|
|
|
'offset' => 0, |
86
|
|
|
'limit' => 20, |
87
|
|
|
], $request->getQueryParams()); |
88
|
|
|
|
89
|
|
|
$collection = $this->namespace_factory->getOne($namespace)->getCollection($collection); |
90
|
|
|
|
91
|
|
|
if (isset($query['watch'])) { |
92
|
|
|
$cursor = $this->object_factory->watch($collection, null, true, $query['query'], false, (int) $query['offset'], (int) $query['limit'], $query['sort']); |
|
|
|
|
93
|
|
|
|
94
|
|
|
return Helper::watchAll($request, $identity, $this->acl, $cursor); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$objects = $collection->getObjects($query['query'], false, (int) $query['offset'], (int) $query['limit'], $query['sort']); |
|
|
|
|
98
|
|
|
|
99
|
|
|
return Helper::getAll($request, $identity, $this->acl, $objects); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Entrypoint. |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): ResponseInterface |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$collection = $this->namespace_factory->getOne($namespace)->getCollection($collection); |
108
|
|
|
$object = $collection->getObject(['name' => $object], false); |
109
|
|
|
|
110
|
|
|
return Helper::getOne($request, $identity, $object); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create object. |
115
|
|
|
*/ |
116
|
|
|
public function post(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection): ResponseInterface |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$query = $request->getQueryParams(); |
119
|
|
|
|
120
|
|
|
$body = array_merge([ |
121
|
|
|
'data' => [], |
122
|
|
|
'endpoints' => null, |
123
|
|
|
], $request->getParsedBody()); |
124
|
|
|
|
125
|
|
|
$collection = $this->namespace_factory->getOne($namespace)->getCollection($collection); |
126
|
|
|
$id = $collection->createObject($body, false, $body['endpoints']); |
|
|
|
|
127
|
|
|
|
128
|
|
|
return new UnformattedResponse( |
129
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_CREATED), |
130
|
|
|
$collection->getObject(['_id' => $id], false)->decorate($request), |
131
|
|
|
['pretty' => isset($query['pretty'])] |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Entrypoint. |
137
|
|
|
*/ |
138
|
|
|
public function getHistory(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): ResponseInterface |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$query = array_merge([ |
141
|
|
|
'offset' => 0, |
142
|
|
|
'limit' => 20, |
143
|
|
|
'query' => [], |
144
|
|
|
], $request->getQueryParams()); |
145
|
|
|
|
146
|
|
|
$collection = $this->namespace_factory->getOne($namespace)->getCollection($collection); |
147
|
|
|
$object = $collection->getObject(['name' => $object], false); |
148
|
|
|
$history = $object->getHistory(); |
149
|
|
|
$body = Pager::fromRequest($history, $request); |
150
|
|
|
|
151
|
|
|
return new UnformattedResponse( |
152
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_OK), |
153
|
|
|
$body, |
154
|
|
|
['pretty' => isset($query['pretty'])] |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Patch. |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function patch(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): ResponseInterface |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$body = $request->getParsedBody(); |
164
|
|
|
$query = $request->getQueryParams(); |
165
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
166
|
|
|
$collection = $namespace->getCollection($collection); |
167
|
|
|
$object = $collection->getObject(['name' => $object]); |
168
|
|
|
$doc = ['data' => $object->getData()]; |
169
|
|
|
$patch = new Patch(json_encode($doc), json_encode($body)); |
170
|
|
|
$patched = $patch->apply(); |
171
|
|
|
$update = json_decode($patched, true); |
172
|
|
|
|
173
|
|
|
$this->object_factory->update($collection, $object, $update); |
174
|
|
|
|
175
|
|
|
return new UnformattedResponse( |
176
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_OK), |
177
|
|
|
$collection->getObject(['_id' => $object->getId()])->decorate($request), |
178
|
|
|
['pretty' => isset($query['pretty'])] |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Delete. |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): ResponseInterface |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
$collection = $this->namespace_factory->getOne($namespace)->getCollection($collection); |
188
|
|
|
$this->object_factory->deleteOne($collection, $object); |
189
|
|
|
|
190
|
|
|
return(new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Entrypoint. |
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public function getAllLogs(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): ResponseInterface |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
$query = array_merge([ |
199
|
|
|
'offset' => 0, |
200
|
|
|
'limit' => 20, |
201
|
|
|
], $request->getQueryParams()); |
202
|
|
|
|
203
|
|
|
if (isset($query['watch'])) { |
204
|
|
|
$filter = [ |
205
|
|
|
'fullDocument.context.namespace' => $namespace, |
206
|
|
|
'fullDocument.context.collection' => $collection, |
207
|
|
|
'fullDocument.context.object' => $object, |
208
|
|
|
]; |
209
|
|
|
|
210
|
|
|
if (!empty($query['query'])) { |
211
|
|
|
$filter = ['$and' => [$filter, $query['query']]]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$logs = $this->log_factory->watch(null, true, $filter, (int) $query['offset'], (int) $query['limit'], $query['sort']); |
215
|
|
|
|
216
|
|
|
return Helper::watchAll($request, $identity, $this->acl, $logs); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$filter = [ |
220
|
|
|
'context.namespace' => $namespace, |
221
|
|
|
'context.collection' => $collection, |
222
|
|
|
'context.object' => $object, |
223
|
|
|
]; |
224
|
|
|
|
225
|
|
|
if (!empty($query['query'])) { |
226
|
|
|
$filter = ['$and' => [$filter, $query['query']]]; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$logs = $this->log_factory->getAll($filter, (int) $query['offset'], (int) $query['limit'], $query['sort']); |
230
|
|
|
|
231
|
|
|
return Helper::getAll($request, $identity, $this->acl, $logs); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Entrypoint. |
236
|
|
|
*/ |
237
|
|
|
public function getOneLog(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object, ObjectIdInterface $log): ResponseInterface |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$resource = $this->log_factory->getOne($log); |
|
|
|
|
240
|
|
|
|
241
|
|
|
return Helper::getOne($request, $identity, $resource); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.