1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* tubee |
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; |
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 StreamIterator\StreamIterator; |
20
|
|
|
use Tubee\Acl; |
21
|
|
|
use Tubee\Resource\ResourceInterface; |
22
|
|
|
use Zend\Diactoros\Response; |
23
|
|
|
|
24
|
|
|
class Helper |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Entrypoint. |
28
|
|
|
*/ |
29
|
|
|
public static function getAll(ServerRequestInterface $request, Identity $identity, Acl $acl, iterable $cursor): ResponseInterface |
30
|
|
|
{ |
31
|
|
|
$query = $request->getQueryParams(); |
32
|
|
|
|
33
|
|
View Code Duplication |
if (isset($query['watch']) && $query['watch'] !== 'false' && !empty($query['watch'])) { |
|
|
|
|
34
|
|
|
return self::watchAll($request, $identity, $acl, $cursor); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
if (isset($query['stream']) && $query['stream'] !== 'false' && !empty($query['stream'])) { |
|
|
|
|
38
|
|
|
return self::stream($request, $identity, $acl, $cursor); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$body = $acl->filterOutput($request, $identity, $cursor); |
42
|
|
|
$body = Pager::fromRequest($body, $request); |
43
|
|
|
|
44
|
|
|
return new UnformattedResponse( |
45
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_OK), |
46
|
|
|
$body, |
47
|
|
|
['pretty' => isset($query['pretty'])] |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Entrypoint. |
53
|
|
|
*/ |
54
|
|
|
public static function getOne(ServerRequestInterface $request, Identity $identity, ResourceInterface $resource): ResponseInterface |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$query = $request->getQueryParams(); |
57
|
|
|
|
58
|
|
|
return new UnformattedResponse( |
59
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_OK), |
60
|
|
|
$resource->decorate($request), |
61
|
|
|
['pretty' => isset($query['pretty'])] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Watch. |
67
|
|
|
*/ |
68
|
|
|
public static function stream(ServerRequestInterface $request, Identity $identity, Acl $acl, iterable $cursor): ResponseInterface |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
//Stream is valid for 5min, after a new requests needs to be sent |
71
|
|
|
ini_set('max_execution_time', '300'); |
72
|
|
|
|
73
|
|
|
$query = $request->getQueryParams(); |
74
|
|
|
$options = isset($query['pretty']) ? JSON_PRETTY_PRINT : 0; |
75
|
|
|
$error = null; |
|
|
|
|
76
|
|
|
|
77
|
|
|
$stream = new StreamIterator($cursor, function ($resource) use ($request, $options) { |
78
|
|
|
if ($this->tell() === 0) { |
|
|
|
|
79
|
|
|
echo '['; |
80
|
|
|
} else { |
81
|
|
|
echo ','; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
echo json_encode($resource->decorate($request), $options); |
85
|
|
|
|
86
|
|
|
if ($this->eof()) { |
|
|
|
|
87
|
|
|
echo ']'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
flush(); |
91
|
|
View Code Duplication |
}, function (\Throwable $e) { |
|
|
|
|
92
|
|
|
if ($this->tell() === 0) { |
|
|
|
|
93
|
|
|
echo '['; |
94
|
|
|
} else { |
95
|
|
|
echo ','; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
echo json_encode([ |
99
|
|
|
'kind' => 'StreamError', |
100
|
|
|
'error' => $e->getMessage(), |
101
|
|
|
'class' => get_class($e), |
102
|
|
|
]).']'; |
103
|
|
|
|
104
|
|
|
flush(); |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
return (new Response($stream)) |
108
|
|
|
->withHeader('X-Accel-Buffering', 'no') |
109
|
|
|
->withHeader('Content-Type', 'application/json;stream') |
110
|
|
|
->withStatus(StatusCodeInterface::STATUS_OK); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Watch. |
115
|
|
|
*/ |
116
|
|
|
public static function watchAll(ServerRequestInterface $request, Identity $identity, Acl $acl, iterable $cursor): ResponseInterface |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
//Watcher is valid for 5min, after a new requests needs to be sent |
119
|
|
|
ini_set('max_execution_time', '300'); |
120
|
|
|
|
121
|
|
|
$query = $request->getQueryParams(); |
122
|
|
|
$options = isset($query['pretty']) ? JSON_PRETTY_PRINT : 0; |
123
|
|
|
|
124
|
|
|
$stream = new StreamIterator($cursor, function ($event) use ($request, $options) { |
125
|
|
|
if ($this->tell() === 0) { |
|
|
|
|
126
|
|
|
echo '['; |
127
|
|
|
} else { |
128
|
|
|
echo ','; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
echo json_encode([ |
132
|
|
|
$event[0], |
133
|
|
|
$event[1]->decorate($request), |
134
|
|
|
], $options); |
135
|
|
|
|
136
|
|
|
if ($this->eof()) { |
|
|
|
|
137
|
|
|
echo ']'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
flush(); |
141
|
|
View Code Duplication |
}, function (\Throwable $e) { |
|
|
|
|
142
|
|
|
if ($this->tell() === 0) { |
|
|
|
|
143
|
|
|
echo '['; |
144
|
|
|
} else { |
145
|
|
|
echo ','; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
echo json_encode(['ADDED', [ |
149
|
|
|
'kind' => 'StreamError', |
150
|
|
|
'error' => $e->getMessage(), |
151
|
|
|
'class' => get_class($e), |
152
|
|
|
]]).']'; |
153
|
|
|
|
154
|
|
|
flush(); |
155
|
|
|
}); |
156
|
|
|
|
157
|
|
|
return (new Response($stream)) |
158
|
|
|
->withHeader('X-Accel-Buffering', 'no') |
159
|
|
|
->withHeader('Content-Type', 'application/json;stream=watch') |
160
|
|
|
->withStatus(StatusCodeInterface::STATUS_OK); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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.