<?php
namespace Can\RestBundle\Controller;
use Can\RestBundle\Caching\CacheHeader;
use Can\RestBundle\EventListener\ErrorListener;
use Can\RestBundle\Problem\ProblemConfiguration;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Exception;
use Throwable;
/**
* Fallback controller.
*
* This controller is used when a problem occurs handling a request which is
* outside the scope of the REST service.
*
* Most often, if the REST service does not live together with other services
* in the same application, all request to undefined endpoints (which will
* result in a 404 response) are forwarded here.
*
* Feel free to extend this controller to implement a more elaborated error
* handling logic.
*
* @see ErrorListener
* @see ProblemConfiguration::$catchAll
*
* @package can/rest-bundle
* @author lechecacharro <lechecacharro@gmail.com>
*/
class FallbackController
{
/**
* @noinspection PhpUnusedParameterInspection
*
* Fallback Action.
*
* The default implementation just returns an immutable 410 (Gone)
* response.
*
* @see ErrorListener
*
* @param Request $request
* @param FlattenException | Exception | Throwable $exception
* @param DebugLoggerInterface | null $logger
*
* @return Response
*/
public function index(Request $request, $exception, DebugLoggerInterface $logger = null): Response
{
return $this->gone();
}
/**
* @return Response
*/
protected function gone(): Response
{
// Response is not going to change over time
$response = new Response('', 410);
$response->headers->set(CacheHeader::CACHE_CONTROL, 'public, max-age=31536000, immutable');
$response->headers->set(CacheHeader::EXPIRES, date('r', strtotime('+1 year')));
return $response;
}
}