<?php

namespace AppBundle\Listener;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use Twig_Environment;

class ErrorRedirect
{

    /**
     * Holds Symfony2 router
     *
     * @var RouterInterface
     */
    protected $router;
    /**
     * @var Twig_Environment
     */
    private $templating;

    /**
     * @param RouterInterface $router
     * @param Twig_Environment $templating
     * @internal param $Router
     */
    public function __construct(RouterInterface $router, Twig_Environment $templating)
    {
        $this->router = $router;
        $this->templating = $templating;
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        /*
        if ($exception instanceof NotFoundHttpException) {
            $route = 'not_found';

            if ($route === $event->getRequest()->get('_route')) {
                return;
            }

            $url = $this->router->generate($route);
            $response = new RedirectResponse($url);
            $event->setResponse($response);
        } elseif ($exception instanceof AccessDeniedHttpException) {
            $route = 'non_autorise';

            if ($route === $event->getRequest()->get('_route')) {
                return;
            }

            $url = $this->router->generate($route);
            $response = new RedirectResponse($url);
            $event->setResponse($response);
//        } else {
//
//            $response = $this->templating->render(
//                '@App/Security/error.html.twig',
//                array('message' => $event->getException()->getMessage())
//            );
//            $event->setResponse(new Response($response));
        }
        */
    }
    
}