<?php

namespace KupShop\KupShopBundle\Controller;

use KupShop\KupShopBundle\Event\DynamicRouteEvent;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class DynamicRouteController extends AbstractController
{
    public function dynamicRouteAction(Request $request, $path, EventDispatcherInterface $eventDispatcher, HttpKernelInterface $kernel)
    {
        $event = new DynamicRouteEvent($kernel, $request, $path);
        $event = $eventDispatcher->dispatch($event, DynamicRouteEvent::NAME);

        if ($response = $event->getResponse()) {
            return $response;
        }

        throw new NotFoundHttpException(sprintf('No route found for "%s"', $path));
    }
}
