Completed
Pull Request — 2.x (#251)
by
unknown
01:50
created

UserLocaleSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
class UserLocaleSubscriber implements EventSubscriberInterface
26
{
27
    /**
28
     * The default locale.
29
     *
30
     * @var SessionInterface
31
     */
32
    private $session;
33
34
    /**
35
     * @param SessionInterface $session
36
     */
37
    public function __construct(SessionInterface $session)
38
    {
39
        $this->session = $session;
40
    }
41
42
    /**
43
     * Interactive login.
44
     *
45
     * @param InteractiveLoginEvent $event
46
     */
47
    public function onInteractiveLogin(InteractiveLoginEvent $event)
48
    {
49
        $user = $event->getAuthenticationToken()->getUser();
50
51
        if (null !== $user->getLocale()) {
52
            $this->session->set('_locale', $user->getLocale());
53
        }
54
    }
55
56
    /**
57
     * Subscribed events.
58
     *
59
     * @return array
60
     */
61
    public static function getSubscribedEvents()
62
    {
63
        return array(
64
            SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
65
        );
66
    }
67
}
68