Completed
Push — 2.x ( 14e460...6fcd32 )
by Grégoire
01:25
created

UserLocaleSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\TranslationBundle\EventSubscriber;
13
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\HttpFoundation\Session\SessionInterface;
16
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
17
use Symfony\Component\Security\Http\SecurityEvents;
18
19
/**
20
 * Stores the locale of the user in the session after the
21
 * login. This can be used by the LocaleSubscriber afterwards.
22
 *
23
 * @author Jonathan Vautrin <[email protected]>
24
 */
25
final class UserLocaleSubscriber implements EventSubscriberInterface
26
{
27
    /**
28
     * @var SessionInterface
29
     */
30
    private $session;
31
32
    /**
33
     * @param SessionInterface $session
34
     */
35
    public function __construct(SessionInterface $session)
36
    {
37
        $this->session = $session;
38
    }
39
40
    /**
41
     * @param InteractiveLoginEvent $event
42
     */
43
    public function onInteractiveLogin(InteractiveLoginEvent $event)
44
    {
45
        $user = $event->getAuthenticationToken()->getUser();
46
47
        if (null !== $user->getLocale()) {
48
            $this->session->set('_locale', $user->getLocale());
49
        }
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public static function getSubscribedEvents()
56
    {
57
        return [
58
            SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
59
        ];
60
    }
61
}
62