Passed
Push — master ( 33aff7...84dd35 )
by Julito
09:17
created

UserLocaleSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\EventSubscriber;
6
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\Session\SessionInterface;
9
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
10
use Symfony\Component\Security\Http\SecurityEvents;
11
12
/**
13
 * Stores the locale of the user in the session after the
14
 * login. This can be used by the LocaleSubscriber afterwards.
15
 *
16
 * Priority order: platform -> user
17
 * Priority order: platform -> user -> course
18
 */
19
class UserLocaleSubscriber implements EventSubscriberInterface
20
{
21
    private $session;
22
23
    public function __construct(SessionInterface $session)
24
    {
25
        $this->session = $session;
26
    }
27
28
    /**
29
     * Set locale when user enters the platform.
30
     */
31
    public function onInteractiveLogin(InteractiveLoginEvent $event)
32
    {
33
        $user = $event->getAuthenticationToken()->getUser();
34
35
        if (null !== $user->getLocale()) {
36
            $this->session->set('_locale', $user->getLocale());
37
            $this->session->set('_locale_user', $user->getLocale());
38
        }
39
    }
40
41
    public static function getSubscribedEvents()
42
    {
43
        return [
44
            SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
45
        ];
46
    }
47
}
48