CurrencySubscriber::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace GGGGino\SkuskuCartBundle\EventSubscriber;
4
5
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
6
use Symfony\Component\HttpKernel\KernelEvents;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
9
/**
10
 * Recognize the currency from the url(if exist) and set it in the section
11
 *
12
 * Class CurrencySubscriber
13
 * @package Allyou\ManagementBundle\EventSubscriber
14
 */
15
class CurrencySubscriber implements EventSubscriberInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $defaultCurrency;
21
22
    /**
23
     * @param string $defaultCurrency
24
     */
25
    public function __construct($defaultCurrency = 'EUR')
26
    {
27
        $this->defaultCurrency = $defaultCurrency;
28
    }
29
30
    /**
31
     * @param GetResponseEvent $event
32
     */
33
    public function onKernelRequest(GetResponseEvent $event)
34
    {
35
        $request = $event->getRequest();
36
37
        // try to see if the currency has been set as a "cu" routing parameter
38
        if ($currency = $request->query->get('cu')) {
39
            $request->attributes->set('skusku_cu', $currency);
40
            $request->getSession()->set('skusku_cu', $currency);
41
        } else {
42
            // if no explicit currency has been set on this request, use one from the session
43
            $sessCurrency = $request->getSession()->get('skusku_cu', $this->defaultCurrency);
44
            $request->attributes->set('skusku_cu', $sessCurrency);
45
        }
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public static function getSubscribedEvents()
52
    {
53
        return array(
54
            // must be registered after the default Locale listener
55
            KernelEvents::REQUEST => array(array('onKernelRequest', 20)),
56
        );
57
    }
58
}