<commit_before><?php

/*
 * This file is part of the RednoseFrameworkBundle package.
 *
 * (c) RedNose <http://www.rednose.nl>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Rednose\FrameworkBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\Security\Core\SecurityContext;
use Rednose\FrameworkBundle\Model\UserInterface;

/**
 * Changes the current locale based on user preferences.
 */
class LocaleListener
{
    /**
     * @var SecurityContext
     */
    protected $context;

    /**
     * Constructor.
     *
     * @param SecurityContext $context
     */
    public function __construct(SecurityContext $context)
    {
        $this->context = $context;
    }

    /**
     * Sets the locale for the current user.
     *
     * @param GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        if (HttpKernel::MASTER_REQUEST !== $event->getRequestType()) {
            return;
        }

        $user    = $this->context->getToken()->getUser();
        $request = $event->getRequest();

        if (!$user instanceof UserInterface || $user->getLocale() === null || $user->getLocale() === $request->getLocale()) {
            return;
        }

        $request->setLocale($user->getLocale());
        $request->getSession()->set('_locale', $user->getLocale());
    }
}<commit_msg>Bugfix: Crash when a anonymous user requested locale<commit_after><?php

/*
 * This file is part of the RednoseFrameworkBundle package.
 *
 * (c) RedNose <http://www.rednose.nl>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Rednose\FrameworkBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\Security\Core\SecurityContext;
use Rednose\FrameworkBundle\Model\UserInterface;

/**
 * Changes the current locale based on user preferences.
 */
class LocaleListener
{
    /**
     * @var SecurityContext
     */
    protected $context;

    /**
     * Constructor.
     *
     * @param SecurityContext $context
     */
    public function __construct(SecurityContext $context)
    {
        $this->context = $context;
    }

    /**
     * Sets the locale for the current user.
     *
     * @param GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        if (HttpKernel::MASTER_REQUEST !== $event->getRequestType()) {
            return;
        }

        // Anonymous has no locale state
        if ($this->context->getToken() === null) {
            return;
        }

        $user    = $this->context->getToken()->getUser();
        $request = $event->getRequest();

        if (!$user instanceof UserInterface || $user->getLocale() === null || $user->getLocale() === $request->getLocale()) {
            return;
        }

        $request->setLocale($user->getLocale());
        $request->getSession()->set('_locale', $user->getLocale());
    }
}
{'fraction_non_alphanumeric': 0.13097454996896338, 'fraction_numerical': 0.0, 'mean_word_length': 2.7740046838407495, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4129236', 'n_tokens_mistral': 892, 'n_tokens_neox': 873, 'n_words': 214}