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\Job; |
23
|
|
|
use Tubee\Job\Factory as JobFactory; |
24
|
|
|
use Tubee\Log\Factory as LogFactory; |
25
|
|
|
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory; |
26
|
|
|
use Tubee\Rest\Helper; |
27
|
|
|
use Zend\Diactoros\Response; |
28
|
|
|
|
29
|
|
|
class Jobs |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* namespace factory. |
33
|
|
|
* |
34
|
|
|
* @var ResourceNamespaceFactory |
35
|
|
|
*/ |
36
|
|
|
protected $namespace_factory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Job factory. |
40
|
|
|
* |
41
|
|
|
* @var JobFactory |
42
|
|
|
*/ |
43
|
|
|
protected $job_factory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Acl. |
47
|
|
|
* |
48
|
|
|
* @var Acl |
49
|
|
|
*/ |
50
|
|
|
protected $acl; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Log factory. |
54
|
|
|
* |
55
|
|
|
* @var LogFactory |
56
|
|
|
*/ |
57
|
|
|
protected $log_factory; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Init. |
61
|
|
|
*/ |
62
|
|
|
public function __construct(JobFactory $job_factory, Acl $acl, ResourceNamespaceFactory $namespace_factory, LogFactory $log_factory) |
63
|
|
|
{ |
64
|
|
|
$this->job_factory = $job_factory; |
65
|
|
|
$this->acl = $acl; |
66
|
|
|
$this->namespace_factory = $namespace_factory; |
67
|
|
|
$this->log_factory = $log_factory; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Entrypoint. |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function getAll(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$query = array_merge([ |
76
|
|
|
'offset' => 0, |
77
|
|
|
'limit' => 20, |
78
|
|
|
], $request->getQueryParams()); |
79
|
|
|
|
80
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
81
|
|
|
|
82
|
|
|
if (isset($query['watch'])) { |
83
|
|
|
$cursor = $this->job_factory->watch($namespace, null, true, $query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']); |
84
|
|
|
|
85
|
|
|
return Helper::watchAll($request, $identity, $this->acl, $cursor); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$jobs = $this->job_factory->getAll($namespace, $query['query'], $query['offset'], $query['limit'], $query['sort']); |
89
|
|
|
|
90
|
|
|
return Helper::getAll($request, $identity, $this->acl, $jobs); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Entrypoint. |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace, string $job): ResponseInterface |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
99
|
|
|
$resource = $this->job_factory->getOne($namespace, $job); |
100
|
|
|
|
101
|
|
|
return Helper::getOne($request, $identity, $resource); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Delete job. |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, string $job): ResponseInterface |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
110
|
|
|
$this->job_factory->deleteOne($namespace, $job); |
111
|
|
|
|
112
|
|
|
return (new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add new job. |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function post(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$body = $request->getParsedBody(); |
121
|
|
|
$query = $request->getQueryParams(); |
122
|
|
|
$job = array_merge(['namespaces' => []], $body); |
123
|
|
|
|
124
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
125
|
|
|
$this->job_factory->create($namespace, $job); |
126
|
|
|
|
127
|
|
|
return new UnformattedResponse( |
128
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_ACCEPTED), |
129
|
|
|
$this->job_factory->getOne($namespace, $job['name'])->decorate($request), |
|
|
|
|
130
|
|
|
['pretty' => isset($query['pretty'])] |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Patch. |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function patch(ServerRequestInterface $request, Identity $identity, string $namespace, string $job): ResponseInterface |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$body = $request->getParsedBody(); |
140
|
|
|
$query = $request->getQueryParams(); |
141
|
|
|
|
142
|
|
|
$namespace = $this->namespace_factory->getOne($namespace); |
143
|
|
|
$job = $this->job_factory->getOne($namespace, $job); |
144
|
|
|
$doc = ['data' => $job->getData()]; |
145
|
|
|
|
146
|
|
|
$patch = new Patch(json_encode($doc), json_encode($body)); |
147
|
|
|
$patched = $patch->apply(); |
148
|
|
|
$update = json_decode($patched, true); |
149
|
|
|
$this->job_factory->update($job, $update); |
150
|
|
|
|
151
|
|
|
return new UnformattedResponse( |
152
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_OK), |
153
|
|
|
$this->job_factory->getOne($namespace, $job->getName())->decorate($request), |
154
|
|
|
['pretty' => isset($query['pretty'])] |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Entrypoint. |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function getAllLogs(ServerRequestInterface $request, Identity $identity, string $namespace, string $job): ResponseInterface |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$query = array_merge([ |
164
|
|
|
'offset' => 0, |
165
|
|
|
'limit' => 20, |
166
|
|
|
], $request->getQueryParams()); |
167
|
|
|
|
168
|
|
|
if (isset($query['watch'])) { |
169
|
|
|
$filter = [ |
170
|
|
|
'fullDocument.context.namespace' => $namespace, |
171
|
|
|
'fullDocument.context.job' => $job, |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
if (!empty($query['query'])) { |
175
|
|
|
$filter = ['$and' => [$filter, $query['query']]]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$logs = $this->log_factory->watch(null, true, $filter, (int) $query['offset'], (int) $query['limit'], $query['sort']); |
179
|
|
|
|
180
|
|
|
return Helper::watchAll($request, $identity, $this->acl, $logs); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$filter = [ |
184
|
|
|
'context.namespace' => $namespace, |
185
|
|
|
'context.job' => $job, |
186
|
|
|
]; |
187
|
|
|
|
188
|
|
|
if (!empty($query['query'])) { |
189
|
|
|
$filter = ['$and' => [$filter, $query['query']]]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$logs = $this->log_factory->getAll($filter, (int) $query['offset'], (int) $query['limit'], $query['sort']); |
193
|
|
|
|
194
|
|
|
return Helper::getAll($request, $identity, $this->acl, $logs); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Entrypoint. |
199
|
|
|
*/ |
200
|
|
|
public function getOneLog(ServerRequestInterface $request, Identity $identity, string $namespace, string $job, ObjectIdInterface $log): ResponseInterface |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
$resource = $this->log_factory->getOne($log); |
|
|
|
|
203
|
|
|
|
204
|
|
|
return Helper::getOne($request, $identity, $resource); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
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.