1 | <?php |
||
2 | |||
3 | namespace App\Application\Middleware; |
||
4 | |||
5 | use App\Application\Data\UserNetworkSessionData; |
||
6 | use App\Application\Responder\JsonResponder; |
||
7 | use App\Domain\Exception\InvalidOperationException; |
||
8 | use Fig\Http\Message\StatusCodeInterface; |
||
9 | use Psr\Http\Message\ResponseFactoryInterface; |
||
10 | use Psr\Http\Message\ResponseInterface; |
||
11 | use Psr\Http\Message\ServerRequestInterface; |
||
12 | use Psr\Http\Server\MiddlewareInterface; |
||
13 | use Psr\Http\Server\RequestHandlerInterface; |
||
14 | use Psr\Log\LoggerInterface; |
||
15 | |||
16 | final readonly class InvalidOperationExceptionMiddleware implements MiddlewareInterface |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
17 | { |
||
18 | 203 | public function __construct( |
|
19 | private ResponseFactoryInterface $responseFactory, |
||
20 | private JsonResponder $jsonResponder, |
||
21 | private UserNetworkSessionData $userNetworkSessionData, |
||
22 | private LoggerInterface $logger, |
||
23 | ) { |
||
24 | 203 | } |
|
25 | |||
26 | 203 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
27 | { |
||
28 | try { |
||
29 | 203 | return $handler->handle($request); |
|
30 | 11 | } catch (InvalidOperationException $exception) { |
|
31 | 5 | $response = $this->responseFactory->createResponse(); |
|
32 | |||
33 | 5 | $this->logger->notice( |
|
34 | 5 | 'Invalid operation from user ' . $this->userNetworkSessionData->userId . ' on ' . |
|
35 | 5 | $request->getUri()->getPath() . ' with message: ' . $exception->getMessage() |
|
36 | 5 | ); |
|
37 | |||
38 | 5 | return $this->jsonResponder->encodeAndAddToResponse( |
|
39 | 5 | $response, |
|
40 | 5 | [ |
|
41 | 5 | 'status' => 'error', |
|
42 | 5 | 'message' => $exception->getMessage(), |
|
43 | 5 | ], |
|
44 | 5 | StatusCodeInterface::STATUS_BAD_REQUEST |
|
45 | 5 | ); |
|
46 | } |
||
47 | } |
||
48 | } |
||
49 |