<?php

namespace KupShop\I18nBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;

class DisplayLocalizedController extends AbstractController
{
    /**
     * @Route(path="/zobrazit-v-jazyce/{lang}/{redirectRequestUrl}", requirements={"redirectRequestUrl"=".+"})
     *
     * @param string $lang
     * @param string $redirectRequestUrl
     *
     * @return Response
     *
     * @throws \Exception
     */
    public function displayAction(Request $request, RouterInterface $router, $lang, $redirectRequestUrl)
    {
        if (!getAdminUser()) {
            return new Response('#403 Forbidden', 403);
        }

        $response = $router->subRequestInLanguage($lang, Request::create('/'.$redirectRequestUrl.'?'.$request->getQueryString()));

        if (in_array($response->getStatusCode(), [301, 302])) {
            $response = $router->subRequestInLanguage($lang, Request::create($response->getTargetUrl()));
        }

        $response->send();

        exit;
    }
}
