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\ObjectId; |
18
|
|
|
use MongoDB\BSON\ObjectIdInterface; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
21
|
|
|
use Tubee\Acl; |
22
|
|
|
use Tubee\Log\Factory as LogFactory; |
23
|
|
|
use Tubee\Process\Factory as ProcessFactory; |
24
|
|
|
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory; |
25
|
|
|
use Tubee\Rest\Helper; |
26
|
|
|
use Zend\Diactoros\Response; |
27
|
|
|
|
28
|
|
|
class Processes |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Namespace factory. |
32
|
|
|
* |
33
|
|
|
* @var ResourceNamespaceFactory |
34
|
|
|
*/ |
35
|
|
|
protected $namespace_factory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Process factory. |
39
|
|
|
* |
40
|
|
|
* @var ProcessFactory |
41
|
|
|
*/ |
42
|
|
|
protected $process_factory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Acl. |
46
|
|
|
* |
47
|
|
|
* @var Acl |
48
|
|
|
*/ |
49
|
|
|
protected $acl; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Log factory. |
53
|
|
|
* |
54
|
|
|
* @var LogFactory |
55
|
|
|
*/ |
56
|
|
|
protected $log_factory; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Init. |
60
|
|
|
*/ |
61
|
|
|
public function __construct(ProcessFactory $process_factory, Acl $acl, ResourceNamespaceFactory $namespace_factory, LogFactory $log_factory) |
62
|
|
|
{ |
63
|
|
|
$this->process_factory = $process_factory; |
64
|
|
|
$this->acl = $acl; |
65
|
|
|
$this->namespace_factory = $namespace_factory; |
66
|
|
|
$this->log_factory = $log_factory; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Entrypoint. |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function getAll(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$query = array_merge([ |
75
|
|
|
'offset' => 0, |
76
|
|
|
'limit' => 20, |
77
|
|
|
], $request->getQueryParams()); |
78
|
|
|
|
79
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
80
|
|
|
|
81
|
|
|
if (isset($query['watch'])) { |
82
|
|
|
$cursor = $this->process_factory->watch($namespace, null, true, $query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']); |
83
|
|
|
|
84
|
|
|
return Helper::watchAll($request, $identity, $this->acl, $cursor); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$processes = $this->process_factory->getAll($namespace, $query['query'], $query['offset'], $query['limit'], $query['sort']); |
88
|
|
|
|
89
|
|
|
return Helper::getAll($request, $identity, $this->acl, $processes); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Entrypoint. |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace, ObjectId $process): ResponseInterface |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
98
|
|
|
$resource = $this->process_factory->getOne($namespace, $process); |
99
|
|
|
|
100
|
|
|
return Helper::getOne($request, $identity, $resource); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Force trigger process. |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function post(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$body = $request->getParsedBody(); |
109
|
|
|
$query = $request->getQueryParams(); |
110
|
|
|
|
111
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
112
|
|
|
$id = $this->process_factory->create($namespace, $body); |
113
|
|
|
$process = $this->process_factory->getOne($namespace, $id); |
114
|
|
|
|
115
|
|
|
return new UnformattedResponse( |
116
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_ACCEPTED), |
117
|
|
|
$process->decorate($request), |
118
|
|
|
['pretty' => isset($query['pretty'])] |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Stop process. |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, ObjectId $process): ResponseInterface |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
128
|
|
|
$this->process_factory->deleteOne($namespace, $process); |
129
|
|
|
|
130
|
|
|
return (new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Entrypoint. |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function getAllLogs(ServerRequestInterface $request, Identity $identity, string $namespace, string $process): ResponseInterface |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$query = array_merge([ |
139
|
|
|
'offset' => 0, |
140
|
|
|
'limit' => 20, |
141
|
|
|
], $request->getQueryParams()); |
142
|
|
|
|
143
|
|
|
if (isset($query['watch'])) { |
144
|
|
|
$filter = [ |
145
|
|
|
'fullDocument.context.namespace' => $namespace, |
146
|
|
|
'fullDocument.context.process' => $process, |
147
|
|
|
]; |
148
|
|
|
|
149
|
|
|
if (!empty($query['query'])) { |
150
|
|
|
$filter = ['$and' => [$filter, $query['query']]]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$logs = $this->log_factory->watch(null, true, $filter, (int) $query['offset'], (int) $query['limit'], $query['sort']); |
154
|
|
|
|
155
|
|
|
return Helper::watchAll($request, $identity, $this->acl, $logs); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$filter = [ |
159
|
|
|
'context.namespace' => $namespace, |
160
|
|
|
'context.process' => $process, |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
if (!empty($query['query'])) { |
164
|
|
|
$filter = ['$and' => [$filter, $query['query']]]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$logs = $this->log_factory->getAll($filter, (int) $query['offset'], (int) $query['limit'], $query['sort']); |
168
|
|
|
|
169
|
|
|
return Helper::getAll($request, $identity, $this->acl, $logs); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Entrypoint. |
174
|
|
|
*/ |
175
|
|
|
public function getOneLog(ServerRequestInterface $request, Identity $identity, string $namespace, string $process, ObjectIdInterface $log): ResponseInterface |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$resource = $this->log_factory->getOne($log); |
|
|
|
|
178
|
|
|
|
179
|
|
|
return Helper::getOne($request, $identity, $resource); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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.