LocaleListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 10
dl 0
loc 32
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 3 1
A __construct() 0 3 1
A onKernelRequest() 0 12 3
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\EventListener;
13
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16
use Symfony\Component\HttpKernel\KernelEvents;
17
18
class LocaleListener implements EventSubscriberInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    private $defaultLocale;
24
25
    /**
26
     * @param string $defaultLocale
27
     */
28
    public function __construct(string $defaultLocale)
29
    {
30
        $this->defaultLocale = $defaultLocale;
31
    }
32
33
    public function onKernelRequest(GetResponseEvent $event): void
34
    {
35
        $request = $event->getRequest();
36
        if (!$request->hasPreviousSession()) {
37
            return;
38
        }
39
40
        if ($request->attributes->has('_locale')) {
41
            return;
42
        }
43
44
        $request->setLocale($request->getSession()->get('admin_locale', $this->defaultLocale));
45
    }
46
47
    public static function getSubscribedEvents(): array
48
    {
49
        return [KernelEvents::REQUEST => [['onKernelRequest', 17]]];
50
    }
51
}
52