Completed
Pull Request — development (#546)
by Nick
07:44 queued 58s
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
 * @author Nick Lubisch <[email protected]>
15
 */
16
class GlobalContextSubscriber implements EventSubscriberInterface
17
{
18
    /**
19
     * @var GlobalContextFactory
20
     */
21
    private $contextFactory;
22
23
    /**
24
     * GlobalContextSubscriber constructor.
25
     *
26
     * @param GlobalContextFactory $contextFactory
27
     */
28
    public function __construct(GlobalContextFactory $contextFactory)
29
    {
30
        $this->contextFactory = $contextFactory;
31
    }
32
33
    /**
34
     * Returns an array of event names this subscriber wants to listen to.
35
     *
36
     * @return array The event names to listen to
37
     */
38
    public static function getSubscribedEvents()
39
    {
40
        return [
41
            KernelEvents::REQUEST => 'onKernelRequest'
42
        ];
43
    }
44
45
    /**
46
     * @param GetResponseEvent $event
47
     */
48
    public function onKernelRequest(GetResponseEvent $event)
49
    {
50
        if (!$event->isMasterRequest()) {
51
            return;
52
        }
53
54
        $request = $event->getRequest();
55
56
        $globalContext = $this->contextFactory->createFromRequest($request);
57
58
        $request->setLocale($globalContext->getLocale());
59
60
        $request->attributes->set('global_context', $globalContext);
61
    }
62
}
63