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 Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Rs\Json\Patch; |
20
|
|
|
use Tubee\Acl; |
21
|
|
|
use Tubee\DataObjectRelation\Factory as DataObjectRelationFactory; |
22
|
|
|
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory; |
23
|
|
|
use Tubee\Rest\Helper; |
24
|
|
|
use Zend\Diactoros\Response; |
25
|
|
|
|
26
|
|
|
class ObjectRelations |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* namespace factory. |
30
|
|
|
* |
31
|
|
|
* @var ResourceNamespaceFactory |
32
|
|
|
*/ |
33
|
|
|
protected $namespace_factory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Dataobject relation factory. |
37
|
|
|
* |
38
|
|
|
* @var DataObjectRelationFactory |
39
|
|
|
*/ |
40
|
|
|
protected $relation_factory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Acl. |
44
|
|
|
* |
45
|
|
|
* @var Acl |
46
|
|
|
*/ |
47
|
|
|
protected $acl; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Init. |
51
|
|
|
*/ |
52
|
|
|
public function __construct(ResourceNamespaceFactory $namespace_factory, DataObjectRelationFactory $relation_factory, Acl $acl) |
53
|
|
|
{ |
54
|
|
|
$this->namespace_factory = $namespace_factory; |
55
|
|
|
$this->relation_factory = $relation_factory; |
56
|
|
|
$this->acl = $acl; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Entrypoint. |
61
|
|
|
*/ |
62
|
|
|
public function getAll(ServerRequestInterface $request, Identity $identity, string $namespace, ?string $collection = null, ?string $object = null): ResponseInterface |
63
|
|
|
{ |
64
|
|
|
$query = array_merge([ |
65
|
|
|
'offset' => 0, |
66
|
|
|
'limit' => 20, |
67
|
|
|
], $request->getQueryParams()); |
68
|
|
|
|
69
|
|
|
if ($object !== null) { |
70
|
|
|
$collection = $this->namespace_factory->getOne($namespace)->getCollection($collection); |
71
|
|
|
$object = $collection->getObject(['name' => $object]); |
72
|
|
|
$relatives = $object->getRelations($query['query'], false, (int) $query['offset'], (int) $query['limit'], $query['sort']); |
73
|
|
|
|
74
|
|
|
return Helper::getAll($request, $identity, $this->acl, $relatives); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
78
|
|
|
$result = $this->relation_factory->getAll($namespace, $query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']); |
79
|
|
|
|
80
|
|
|
return Helper::getAll($request, $identity, $this->acl, $result); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Entrypoint. |
85
|
|
|
*/ |
86
|
|
|
public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace, ?string $collection = null, ?string $object = null, ?string $relation = null): ResponseInterface |
87
|
|
|
{ |
88
|
|
|
if ($object != null) { |
89
|
|
|
$collection = $this->namespace_factory->getOne($namespace)->getCollection($collection); |
90
|
|
|
$object = $collection->getObject(['name' => $object], false); |
91
|
|
|
$relative = $object->getRelation($relation); |
92
|
|
|
|
93
|
|
|
return Helper::getOne($request, $identity, $relative); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
97
|
|
|
$relative = $this->relation_factory->getOne($namespace, $relation); |
98
|
|
|
|
99
|
|
|
return Helper::getOne($request, $identity, $relative); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Create object. |
104
|
|
|
*/ |
105
|
|
|
public function post(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$body = $request->getParsedBody(); |
108
|
|
|
$query = $request->getQueryParams(); |
109
|
|
|
|
110
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
111
|
|
|
$this->relation_factory->add($namespace, $body); |
112
|
|
|
|
113
|
|
|
return new UnformattedResponse( |
114
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_CREATED), |
115
|
|
|
$this->relation_factory->getOne($namespace, $body['name'])->decorate($request), |
116
|
|
|
['pretty' => isset($query['pretty'])] |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Delete. |
122
|
|
|
*/ |
123
|
|
|
public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, string $relation): ResponseInterface |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$collection = $this->namespace_factory->getOne($namespace); |
|
|
|
|
126
|
|
|
$this->relation_factory->deleteOne($namespace, $relation); |
|
|
|
|
127
|
|
|
|
128
|
|
|
return(new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Patch. |
133
|
|
|
*/ |
134
|
|
|
public function patch(ServerRequestInterface $request, Identity $identity, string $namespace, string $relation): ResponseInterface |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$body = $request->getParsedBody(); |
137
|
|
|
$query = $request->getQueryParams(); |
138
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
139
|
|
|
$relation = $this->relation_factory->getOne($namespace, $relation); |
140
|
|
|
$doc = ['data' => $relation->getData()]; |
141
|
|
|
|
142
|
|
|
$patch = new Patch(json_encode($doc), json_encode($body)); |
143
|
|
|
$patched = $patch->apply(); |
144
|
|
|
$update = json_decode($patched, true); |
145
|
|
|
|
146
|
|
|
$this->relation_factory->update($relation, $update); |
147
|
|
|
|
148
|
|
|
return new UnformattedResponse( |
149
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_OK), |
150
|
|
|
$this->relation_factory->getOne($namespace, $relation->getName())->decorate($request), |
151
|
|
|
['pretty' => isset($query['pretty'])] |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Watch. |
157
|
|
|
*/ |
158
|
|
|
public function watchAll(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface |
159
|
|
|
{ |
160
|
|
|
$query = array_merge([ |
161
|
|
|
'offset' => null, |
162
|
|
|
'limit' => null, |
163
|
|
|
'existing' => true, |
164
|
|
|
], $request->getQueryParams()); |
165
|
|
|
|
166
|
|
|
$object = $this->namespace_factory->getOne($namespace); |
|
|
|
|
167
|
|
|
$cursor = $this->relation_factory->watch($namespace, $query['query'], $query['offset'], $query['limit'], $query['sort']); |
|
|
|
|
168
|
|
|
|
169
|
|
|
return Helper::watchAll($request, $identity, $this->acl, $cursor); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.