<commit_before><?php

namespace NetBull\CoreBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use NetBull\CoreBundle\Locale\Events;
use NetBull\CoreBundle\Event\FilterLocaleSwitchEvent;
use NetBull\CoreBundle\Locale\Validator\MetaValidator;

/**
 * Class LocaleController
 * @package NetBull\CoreBundle\Controller
 */
class LocaleController
{
    /**
     * @var EventDispatcherInterface
     */
    private $dispatcher;

    /**
     * @var null|RouterInterface
     */
    private $router;

    /**
     * @var MetaValidator
     */
    private $metaValidator;

    /**
     * @var bool
     */
    private $useReferrer;

    /**
     * @var null
     */
    private $redirectToRoute;

    /**
     * @var string
     */
    private $statusCode;

    /**
     * LocaleController constructor.
     * @param EventDispatcherInterface $dispatcher
     * @param RouterInterface|null $router
     * @param MetaValidator $metaValidator
     * @param bool $useReferrer
     * @param null $redirectToRoute
     * @param string $statusCode
     */
    public function __construct(EventDispatcherInterface $dispatcher, RouterInterface $router = null, MetaValidator $metaValidator, $useReferrer = true, $redirectToRoute = null, $statusCode = '302')
    {
        $this->dispatcher = $dispatcher;
        $this->router = $router;
        $this->metaValidator = $metaValidator;
        $this->useReferrer = $useReferrer;
        $this->redirectToRoute = $redirectToRoute;
        $this->statusCode = $statusCode;
    }

    /**
     * @param Request $request
     * @return RedirectResponse
     */
    public function switchAction(Request $request)
    {
        $_locale = $request->attributes->get('_locale', $request->getLocale());
        $statusCode = $request->attributes->get('statusCode', $this->statusCode);
        $useReferrer = $request->attributes->get('useReferrer', $this->useReferrer);
        $redirectToRoute = $request->attributes->get('route', $this->redirectToRoute);
        $metaValidator = $this->metaValidator;
        if (!$metaValidator->isAllowed($_locale)) {
            throw new \InvalidArgumentException(sprintf('Not allowed to switch to locale %s', $_locale));
        }

        $localeSwitchEvent = new FilterLocaleSwitchEvent($request, $_locale);
        $this->dispatcher->dispatch(Events::ON_LOCALE_CHANGE, $localeSwitchEvent);

        // Redirect the User
        if ($useReferrer && $request->headers->has('referer')) {
            $response = new RedirectResponse($request->headers->get('referer'), $statusCode);
        } elseif ($this->router && $redirectToRoute) {
            $target = $this->router->generate($redirectToRoute, array('_locale' => $_locale));
            if ($request->getQueryString()) {
                if (!strpos($target, '?')) {
                    $target .= '?';
                }
                $target .= $request->getQueryString();
            }
            $response = new RedirectResponse($target, $statusCode);
        } else {
            $response = new RedirectResponse($request->getScheme() . '://' . $request->getHttpHost() . '/', $statusCode);
        }
        return $response;
    }
}<commit_msg>initial commit<commit_after><?php

namespace NetBull\CoreBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use NetBull\CoreBundle\Locale\Events;
use NetBull\CoreBundle\Event\FilterLocaleSwitchEvent;
use NetBull\CoreBundle\Locale\Validator\MetaValidator;

/**
 * Class LocaleController
 * @package NetBull\CoreBundle\Controller
 */
class LocaleController
{
    /**
     * @var EventDispatcherInterface
     */
    private $dispatcher;

    /**
     * @var null|RouterInterface
     */
    private $router;

    /**
     * @var MetaValidator
     */
    private $metaValidator;

    /**
     * @var bool
     */
    private $useReferrer;

    /**
     * @var null
     */
    private $redirectToRoute;

    /**
     * @var string
     */
    private $statusCode;

    /**
     * LocaleController constructor.
     * @param EventDispatcherInterface $dispatcher
     * @param RouterInterface|null $router
     * @param MetaValidator $metaValidator
     * @param bool $useReferrer
     * @param null $redirectToRoute
     * @param string $statusCode
     */
    public function __construct(EventDispatcherInterface $dispatcher, RouterInterface $router = null, MetaValidator $metaValidator, $useReferrer = true, $redirectToRoute = null, $statusCode = '302')
    {
        $this->dispatcher = $dispatcher;
        $this->router = $router;
        $this->metaValidator = $metaValidator;
        $this->useReferrer = $useReferrer;
        $this->redirectToRoute = $redirectToRoute;
        $this->statusCode = $statusCode;
    }

    /**
     * @param Request $request
     * @return RedirectResponse
     */
    public function switchAction(Request $request)
    {
        $_locale = $request->attributes->get('_locale', $request->getLocale());
        $statusCode = $request->attributes->get('statusCode', $this->statusCode);
        $useReferrer = $request->attributes->get('useReferrer', $this->useReferrer);
        $redirectToRoute = $request->attributes->get('route', $this->redirectToRoute);
        $metaValidator = $this->metaValidator;

        if (!$metaValidator->isAllowed($_locale)) {
            throw new \InvalidArgumentException(sprintf('Not allowed to switch to locale %s', $_locale));
        }

        $localeSwitchEvent = new FilterLocaleSwitchEvent($request, $_locale);
        $this->dispatcher->dispatch(Events::ON_LOCALE_CHANGE, $localeSwitchEvent);

        return $this->getResponse($request, $useReferrer, $statusCode, $redirectToRoute);
    }

    /**
     * @param Request $request
     * @param $useReferrer
     * @param $statusCode
     * @param $redirectToRoute
     * @return RedirectResponse
     */
    private function getResponse(Request $request, $useReferrer, $statusCode, $redirectToRoute)
    {
        // Redirect the User
        if ($useReferrer && $request->headers->has('referer')) {
            $response = new RedirectResponse($request->headers->get('referer'), $statusCode);
        } elseif ($this->router && $redirectToRoute) {
            $target = $this->router->generate($redirectToRoute, ['_locale' => $_locale]);
            if ($request->getQueryString()) {
                if (!strpos($target, '?')) {
                    $target .= '?';
                }
                $target .= $request->getQueryString();
            }
            $response = new RedirectResponse($target, $statusCode);
        } else {
            $response = new RedirectResponse($request->getScheme() . '://' . $request->getHttpHost() . '/', $statusCode);
        }

        return $response;
    }
}
{'fraction_non_alphanumeric': 0.13515425983583357, 'fraction_numerical': 0.0008491367110104727, 'mean_word_length': 2.8282773564463706, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7125830', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1946, 'n_words': 366}