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\ResourceNamespace\Factory as ResourceNamespaceFactory; |
22
|
|
|
use Tubee\Rest\Helper; |
23
|
|
|
use Zend\Diactoros\Response; |
24
|
|
|
|
25
|
|
View Code Duplication |
class ResourceNamespaces |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* ResourceNamespace factory. |
29
|
|
|
* |
30
|
|
|
* @var ResourceNamespaceFactory |
31
|
|
|
*/ |
32
|
|
|
protected $namespace_factory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Acl. |
36
|
|
|
* |
37
|
|
|
* @var Acl |
38
|
|
|
*/ |
39
|
|
|
protected $acl; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Init. |
43
|
|
|
*/ |
44
|
|
|
public function __construct(ResourceNamespaceFactory $namespace, Acl $acl) |
45
|
|
|
{ |
46
|
|
|
$this->namespace_factory = $namespace; |
47
|
|
|
$this->acl = $acl; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Entrypoint. |
52
|
|
|
*/ |
53
|
|
|
public function getAll(ServerRequestInterface $request, Identity $identity): ResponseInterface |
54
|
|
|
{ |
55
|
|
|
$query = array_merge([ |
56
|
|
|
'offset' => 0, |
57
|
|
|
'limit' => 20, |
58
|
|
|
], $request->getQueryParams()); |
59
|
|
|
|
60
|
|
|
if (isset($query['watch'])) { |
61
|
|
|
$cursor = $this->namespace_factory->watch(null, true, $query['query'], $query['offset'], $query['limit'], $query['sort']); |
62
|
|
|
|
63
|
|
|
return Helper::watchAll($request, $identity, $this->acl, $cursor); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$namespaces = $this->namespace_factory->getAll($query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']); |
67
|
|
|
|
68
|
|
|
return Helper::getAll($request, $identity, $this->acl, $namespaces); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Entrypoint. |
73
|
|
|
*/ |
74
|
|
|
public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface |
75
|
|
|
{ |
76
|
|
|
$resource = $this->namespace_factory->getOne($namespace); |
77
|
|
|
|
78
|
|
|
return Helper::getOne($request, $identity, $resource); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Delete. |
83
|
|
|
*/ |
84
|
|
|
public function delete(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$this->namespace_factory->deleteOne($namespace); |
87
|
|
|
|
88
|
|
|
return (new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Patch. |
93
|
|
|
*/ |
94
|
|
|
public function patch(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$body = $request->getParsedBody(); |
97
|
|
|
$query = $request->getQueryParams(); |
98
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
99
|
|
|
$doc = ['data' => $namespace->getData()]; |
100
|
|
|
|
101
|
|
|
$patch = new Patch(json_encode($doc), json_encode($body)); |
102
|
|
|
$patched = $patch->apply(); |
103
|
|
|
$update = json_decode($patched, true); |
104
|
|
|
$this->namespace_factory->update($namespace, $update); |
105
|
|
|
|
106
|
|
|
return new UnformattedResponse( |
107
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_OK), |
108
|
|
|
$this->namespace_factory->getOne($namespace->getName())->decorate($request), |
109
|
|
|
['pretty' => isset($query['pretty'])] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create. |
115
|
|
|
*/ |
116
|
|
|
public function post(ServerRequestInterface $request, Identity $identity): ResponseInterface |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$body = (array) $request->getParsedBody(); |
119
|
|
|
$id = $this->namespace_factory->add($body); |
|
|
|
|
120
|
|
|
$query = $request->getQueryParams(); |
121
|
|
|
|
122
|
|
|
return new UnformattedResponse( |
123
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_CREATED), |
124
|
|
|
$this->namespace_factory->getOne($body['name'])->decorate($request), |
125
|
|
|
['pretty' => isset($query['pretty'])] |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.