1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LibraryCatalog\Controller\V1; |
6
|
|
|
|
7
|
|
|
use DI\Container; |
8
|
|
|
use LibraryCatalog\Controller\V1\ValueObject\Error as ErrorDto; |
9
|
|
|
use LibraryCatalog\Controller\V1\ValueObject\ErrorWithValidation; |
10
|
|
|
use LibraryCatalog\Controller\V1\ValueObject\Status; |
11
|
|
|
use LibraryCatalog\Exception\HttpUnauthorizedException; |
12
|
|
|
use LibraryCatalog\Service\AclInterface; |
13
|
|
|
use LibraryCatalog\Service\AuthInInterface; |
14
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
15
|
|
|
use Psr\Http\Message\RequestInterface; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
|
18
|
|
|
abstract class AbstractController |
19
|
|
|
{ |
20
|
|
|
/** @var Container */ |
21
|
|
|
protected Container $container; |
22
|
|
|
/** @var Psr17Factory */ |
23
|
|
|
protected Psr17Factory $responseFactory; |
24
|
|
|
/** @var RequestInterface */ |
25
|
|
|
protected RequestInterface $request; |
26
|
|
|
/** |
27
|
|
|
* You can change this value in your controller. |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected bool $needAuth = true; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* AbstractController constructor. |
34
|
|
|
* @param Container $container |
35
|
|
|
* @param RequestInterface $request |
36
|
|
|
* @throws HttpUnauthorizedException |
37
|
|
|
* @throws \DI\DependencyException |
38
|
|
|
* @throws \DI\NotFoundException |
39
|
|
|
*/ |
40
|
17 |
|
public function __construct(Container $container, RequestInterface $request) |
41
|
|
|
{ |
42
|
17 |
|
$this->container = $container; |
43
|
17 |
|
$this->responseFactory = new \Nyholm\Psr7\Factory\Psr17Factory(); |
44
|
17 |
|
$this->request = $request; |
45
|
|
|
|
46
|
|
|
// Check Auth. |
47
|
17 |
|
if ($this->needAuth) { |
48
|
|
|
/** @var AuthInInterface $auth */ |
49
|
15 |
|
$auth = $this->getAuthIn()->setRequest($request); |
50
|
15 |
|
if (!$auth->authenticated()) { |
51
|
1 |
|
throw new HttpUnauthorizedException(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
17 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $uri |
58
|
|
|
* @param string $message |
59
|
|
|
* @param string $code |
60
|
|
|
* @throws \DI\DependencyException |
61
|
|
|
* @throws \DI\NotFoundException |
62
|
|
|
* @return ResponseInterface |
63
|
|
|
*/ |
64
|
|
|
public function systemError(string $uri, string $message = 'System error', string $code = ''): ResponseInterface |
65
|
|
|
{ |
66
|
|
|
return $this->error($uri, 500, $message, $code); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $uri |
71
|
|
|
* @param string $message |
72
|
|
|
* @param string $code |
73
|
|
|
* @throws \DI\DependencyException |
74
|
|
|
* @throws \DI\NotFoundException |
75
|
|
|
* @return ResponseInterface |
76
|
|
|
*/ |
77
|
2 |
|
public function notFoundError(string $uri, string $message = 'Page not found', string $code = ''): ResponseInterface |
78
|
|
|
{ |
79
|
2 |
|
return $this->error($uri, 404, $message, $code); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $uri |
84
|
|
|
* @param string $message |
85
|
|
|
* @param string $code |
86
|
|
|
* @return ResponseInterface |
87
|
|
|
* @throws \DI\DependencyException |
88
|
|
|
* @throws \DI\NotFoundException |
89
|
|
|
*/ |
90
|
1 |
|
public function badRequestError(string $uri, string $message = 'Bad request', string $code = ''): ResponseInterface |
91
|
|
|
{ |
92
|
1 |
|
return $this->error($uri, 400, $message, $code); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $uri |
97
|
|
|
* @param string $message |
98
|
|
|
* @param string $code |
99
|
|
|
* @return ResponseInterface |
100
|
|
|
* @throws \DI\DependencyException |
101
|
|
|
* @throws \DI\NotFoundException |
102
|
|
|
*/ |
103
|
4 |
|
public function forbiddenError(string $uri, string $message = 'Forbidden', string $code = ''): ResponseInterface |
104
|
|
|
{ |
105
|
4 |
|
return $this->error($uri, 403, $message, $code); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $uri |
110
|
|
|
* @param string $message |
111
|
|
|
* @param string $code |
112
|
|
|
* @return ResponseInterface |
113
|
|
|
* @throws \DI\DependencyException |
114
|
|
|
* @throws \DI\NotFoundException |
115
|
|
|
*/ |
116
|
1 |
|
public function unauthorizedError(string $uri, string $message = 'Unauthorized', string $code = ''): ResponseInterface |
117
|
|
|
{ |
118
|
1 |
|
return $this->error($uri, 401, $message, $code); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $uri |
123
|
|
|
* @param array $fields |
124
|
|
|
* @param string $code |
125
|
|
|
* @return ResponseInterface |
126
|
|
|
* @throws \DI\DependencyException |
127
|
|
|
* @throws \DI\NotFoundException |
128
|
|
|
*/ |
129
|
2 |
|
public function validationError(string $uri, array $fields, string $code = ''): ResponseInterface |
|
|
|
|
130
|
|
|
{ |
131
|
2 |
|
return $this->responseFactory->createResponse(400)->withBody( |
132
|
2 |
|
$this->responseFactory->createStream( |
133
|
2 |
|
$this->serialize(ErrorWithValidation::create('Validation error', $code)->withFields($fields)) |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $uri |
140
|
|
|
* @param int $httpStatusCode |
141
|
|
|
* @param string $message |
142
|
|
|
* @param string $code |
143
|
|
|
* @throws \DI\DependencyException |
144
|
|
|
* @throws \DI\NotFoundException |
145
|
|
|
* @return ResponseInterface |
146
|
|
|
*/ |
147
|
8 |
|
public function error(string $uri, int $httpStatusCode = 500, string $message = 'Page not found', string $code = ''): ResponseInterface |
|
|
|
|
148
|
|
|
{ |
149
|
8 |
|
return $this->responseFactory->createResponse($httpStatusCode)->withBody( |
150
|
8 |
|
$this->responseFactory->createStream($this->serialize(ErrorDto::create($message, $code))) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $status |
156
|
|
|
* @return ResponseInterface |
157
|
|
|
* @throws \DI\DependencyException |
158
|
|
|
* @throws \DI\NotFoundException |
159
|
|
|
*/ |
160
|
2 |
|
protected function status(string $status = 'Ok'): ResponseInterface |
161
|
|
|
{ |
162
|
2 |
|
return $this->createResponse(Status::create($status)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $data |
167
|
|
|
* @return ResponseInterface |
168
|
|
|
* @throws \DI\DependencyException |
169
|
|
|
* @throws \DI\NotFoundException |
170
|
|
|
*/ |
171
|
7 |
|
protected function createResponse($data): ResponseInterface |
172
|
|
|
{ |
173
|
7 |
|
return $this->responseFactory->createResponse(200)->withBody( |
174
|
7 |
|
$this->responseFactory->createStream($this->serialize($data)) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param mixed $data |
180
|
|
|
* @return string |
181
|
|
|
* @throws \DI\DependencyException |
182
|
|
|
* @throws \DI\NotFoundException |
183
|
|
|
*/ |
184
|
17 |
|
protected function serialize($data): string |
185
|
|
|
{ |
186
|
17 |
|
return $this->container->get('HttpTransformer')->serialize($data); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return AclInterface |
191
|
|
|
* @throws \DI\DependencyException |
192
|
|
|
* @throws \DI\NotFoundException |
193
|
|
|
*/ |
194
|
13 |
|
protected function getAcl(): AclInterface |
195
|
|
|
{ |
196
|
13 |
|
return $this->container->get('Acl'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return AuthInInterface |
201
|
|
|
* @throws \DI\DependencyException |
202
|
|
|
* @throws \DI\NotFoundException |
203
|
|
|
*/ |
204
|
15 |
|
protected function getAuthIn(): AuthInInterface |
205
|
|
|
{ |
206
|
15 |
|
return $this->container->get('AuthIn'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $resourceId |
211
|
|
|
* @param string $privilege |
212
|
|
|
* @return bool |
213
|
|
|
* @throws \DI\DependencyException |
214
|
|
|
* @throws \DI\NotFoundException |
215
|
|
|
*/ |
216
|
13 |
|
protected function isAllowed(string $resourceId, string $privilege): bool |
217
|
|
|
{ |
218
|
13 |
|
$acl = $this->getAcl(); |
219
|
13 |
|
return $acl->isAllowed($acl->parseUserPayload($this->getAuthIn()->getPayload()), $resourceId, $privilege); |
|
|
|
|
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.