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

UserLocaleSubscriber   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onInteractiveLogin() 0 8 2
A getSubscribedEvents() 0 6 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