Completed
Pull Request — development (#546)
by Nick
06:25
created

GlobalContextSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\GlobalContext\Subscriber;
4
5
use Oc\GlobalContext\GlobalContextFactory;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8
use Symfony\Component\HttpKernel\KernelEvents;
9
10
/**
11
 * Class GlobalContextSubscriber
12
 *
13
 * @package Oc\GlobalContext\Subscriber
14
 */
15
class GlobalContextSubscriber implements EventSubscriberInterface
16
{
17
    /**
18
     * @var GlobalContextFactory
19
     */
20
    private $contextFactory;
21
22
    /**
23
     * GlobalContextSubscriber constructor.
24
     *
25
     * @param GlobalContextFactory $contextFactory
26
     */
27
    public function __construct(GlobalContextFactory $contextFactory)
28
    {
29
        $this->contextFactory = $contextFactory;
30
    }
31
32
    /**
33
     * Returns an array of event names this subscriber wants to listen to.
34
     *
35
     * @return array The event names to listen to
36
     */
37
    public static function getSubscribedEvents()
38
    {
39
        return [
40
            KernelEvents::REQUEST => 'onKernelRequest'
41
        ];
42
    }
43
44
    /**
45
     * @param GetResponseEvent $event
46
     */
47
    public function onKernelRequest(GetResponseEvent $event)
48
    {
49
        if (!$event->isMasterRequest()) {
50
            return;
51
        }
52
53
        $request = $event->getRequest();
54
55
        $globalContext = $this->contextFactory->createFromRequest($request);
56
57
        $request->setLocale($globalContext->getLocale());
58
59
        $request->attributes->set('global_context', $globalContext);
60
    }
61
}
62