58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
<?php
|
|
namespace ProVM\Common\Middleware;
|
|
|
|
use ProVM\Common\Exception\Database\BlankResult;
|
|
use ProVM\Common\Exception\Mailbox\Stateless;
|
|
use ProVM\Common\Exception\Request\MissingArgument;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ResponseFactoryInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Safe\Exceptions\JsonException;
|
|
|
|
class CustomExceptions
|
|
{
|
|
public function __construct(ResponseFactoryInterface $factory, LoggerInterface $logger)
|
|
{
|
|
$this->setResponseFactory($factory)
|
|
->setLogger($logger);
|
|
}
|
|
|
|
protected ResponseFactoryInterface $factory;
|
|
protected LoggerInterface $logger;
|
|
public function getResponseFactory(): ResponseFactoryInterface
|
|
{
|
|
return $this->factory;
|
|
}
|
|
public function getLogger(): LoggerInterface
|
|
{
|
|
return $this->logger;
|
|
}
|
|
public function setResponseFactory(ResponseFactoryInterface $factory): CustomExceptions
|
|
{
|
|
$this->factory = $factory;
|
|
return $this;
|
|
}
|
|
public function setLogger(LoggerInterface $logger): CustomExceptions
|
|
{
|
|
$this->logger = $logger;
|
|
return $this;
|
|
}
|
|
|
|
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
{
|
|
try {
|
|
return $handler->handle($request);
|
|
} catch (BlankResult $e) {
|
|
$this->getLogger()->error($e);
|
|
return $this->getResponseFactory()->createResponse(204);
|
|
} catch (JsonException $e) {
|
|
$this->getLogger()->error($e);
|
|
return $this->getResponseFactory()->createResponse(415, 'Only JSON Media Type is supported for this request');
|
|
} catch (MissingArgument $e) {
|
|
$this->getLogger()->error($e);
|
|
return $this->getResponseFactory()->createResponse(400, $e->getMessage());
|
|
}
|
|
}
|
|
} |