LocaleSubscriber   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 7
dl 0
loc 35
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 7 1
A onConsoleCommand() 0 6 1
A onKernelRequest() 0 17 4
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
namespace WellCommerce\Bundle\AppBundle\EventListener;
13
14
use Symfony\Component\Console\ConsoleEvents;
15
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16
use Symfony\Component\HttpKernel\HttpKernelInterface;
17
use Symfony\Component\HttpKernel\KernelEvents;
18
use WellCommerce\Bundle\CoreBundle\EventListener\AbstractEventSubscriber;
19
20
/**
21
 * Class LocaleSubscriber
22
 *
23
 * @author  Adam Piotrowski <[email protected]>
24
 */
25
class LocaleSubscriber extends AbstractEventSubscriber
26
{
27
    public static function getSubscribedEvents()
28
    {
29
        return [
30
            KernelEvents::REQUEST  => ['onKernelRequest', 15],
31
            ConsoleEvents::COMMAND => ['onConsoleCommand', 0],
32
        ];
33
    }
34
35
    public function onConsoleCommand()
36
    {
37
        $filter = $this->getDoctrineHelper()->enableFilter('locale');
38
        $locale = $this->container->getParameter('locale');
39
        $filter->setParameter('locale', $locale);
40
    }
41
42
    public function onKernelRequest(GetResponseEvent $event)
43
    {
44
        if ($event->getRequestType() == HttpKernelInterface::SUB_REQUEST) {
45
            return;
46
        }
47
48
        $request = $event->getRequest();
49
50
        if ($locale = $request->attributes->get('_locale')) {
51
            $request->getSession()->set('_locale', $locale);
52
        } else {
53
            if ($request->hasSession()) {
54
                $currentLocale = $request->getSession()->get('_locale', $request->getDefaultLocale());
55
                $request->setLocale($currentLocale);
56
            }
57
        }
58
    }
59
}
60