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

UserLocaleSubscriber   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 43
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
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