<?php

namespace KupShop\LocalePrefixBundle\EventListener;

use KupShop\LocalePrefixBundle\Util\LocalePrefixUtil;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class LanguageSetterSubscriber implements EventSubscriberInterface
{
    public function __construct(private LocalePrefixUtil $localePrefixUtil)
    {
    }

    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::REQUEST => [
                ['setLanguage', 9999],
            ],
        ];
    }

    public function setLanguage(KernelEvent $event)
    {
        $request = $event->getRequest();
        $this->localePrefixUtil->setRequestAttribute($request);
        $this->localePrefixUtil->setLanguageFromLocalePrefix($request->getPathInfo());
    }
}
