2023-07-28 16:22:20 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Middleware;
|
|
|
|
|
|
|
|
use Psr\Http\Message\ResponseFactoryInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Slim\Exception\HttpNotFoundException;
|
|
|
|
use Incoviba\Common\Alias\View;
|
2025-05-05 16:43:24 -04:00
|
|
|
use Incoviba\Common\Implement\Exception\{EmptyRedis,EmptyResult};
|
|
|
|
use Incoviba\Exception\ServiceAction\{Create, Delete, Read, Update};
|
2023-07-28 16:22:20 -04:00
|
|
|
|
|
|
|
class NotFound
|
|
|
|
{
|
2025-05-05 16:43:24 -04:00
|
|
|
public function __construct(protected LoggerInterface $logger, protected ResponseFactoryInterface $responseFactory,
|
|
|
|
protected View $view) {}
|
2023-07-28 16:22:20 -04:00
|
|
|
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $handler->handle($request);
|
2025-05-05 16:43:24 -04:00
|
|
|
} catch (HttpNotFoundException |
|
|
|
|
EmptyRedis | EmptyResult | Read | Create | Update | Delete $exception) {
|
2025-06-03 11:37:14 -04:00
|
|
|
$serverFilters = [
|
|
|
|
'HTTP_',
|
|
|
|
'QUERY_',
|
|
|
|
'REDIRECT_',
|
|
|
|
'REMOTE_',
|
|
|
|
'REQUEST_',
|
|
|
|
];
|
|
|
|
$serverParams = array_filter($request->getServerParams(),
|
|
|
|
fn($key) => count(array_filter($serverFilters, fn($prefix) => str_starts_with($key, $prefix))) > 0,
|
|
|
|
ARRAY_FILTER_USE_KEY);
|
2025-06-03 10:24:54 -04:00
|
|
|
$this->logger->notice($exception, [
|
2025-06-03 11:37:14 -04:00
|
|
|
'Server' => $serverParams,
|
2025-06-03 10:24:54 -04:00
|
|
|
'Headers' => $request->getHeaders(),
|
|
|
|
]);
|
2025-05-05 16:43:24 -04:00
|
|
|
$response = $this->responseFactory->createResponse(404, 'Not Found');
|
2023-10-13 10:45:21 -03:00
|
|
|
if (str_contains($request->getUri()->getPath(), '/api')) {
|
|
|
|
return $response;
|
|
|
|
}
|
2023-07-28 16:22:20 -04:00
|
|
|
return $this->view->render($response, 'not_found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|