k911/swoole-bundle

View on GitHub
src/Bridge/Symfony/ErrorHandler/ErrorResponder.php

Summary

Maintainability
A
0 mins
Test Coverage
C
71%
<?php

declare(strict_types=1);

namespace K911\Swoole\Bridge\Symfony\ErrorHandler;

use Symfony\Component\ErrorHandler\ErrorHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Throwable;

final class ErrorResponder
{
    /**
     * @var ErrorHandler
     */
    private $errorHandler;

    /**
     * @var ExceptionHandlerFactory
     */
    private $handlerFactory;

    public function __construct(ErrorHandler $errorHandler, ExceptionHandlerFactory $handlerFactory)
    {
        $this->errorHandler = $errorHandler;
        $this->handlerFactory = $handlerFactory;
    }

    public function processErroredRequest(Request $request, Throwable $throwable): Response
    {
        $exceptionHandler = $this->handlerFactory->newExceptionHandler($request);
        $this->errorHandler->setExceptionHandler($exceptionHandler);

        return $this->errorHandler->handleException($throwable);
    }
}