<?php

namespace KupShop\OrderingBundle\EventListener;

use KupShop\KupShopBundle\Context\PriceLevelContext;
use KupShop\KupShopBundle\Util\Contexts;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;

class RequestListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            ControllerEvent::class => ['handlePersistentPriceLevel'],
        ];
    }

    public function handlePersistentPriceLevel(ControllerEvent $event)
    {
        $priceLevel = $event->getRequest()->getSession()->get(PriceLevelContext::PERSISTENT_KEY);

        if ($priceLevel) {
            Contexts::get(PriceLevelContext::class)->activate($priceLevel);
        }
    }
}
