Completed
Pull Request — development (#546)
by Nick
07:44 queued 58s
created

GlobalContextSubscriber   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 6 1
A onKernelRequest() 0 14 2
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