<?php

/*
* Analyses a request, created the right Controller passing it the request
*/

class Dispatcher
{
    public static function dispatch($request)
    {
        return static::dispatchToController($request->getControllerName(), $request);
    }

    public static function dispatchToController($controllerName, $request)
    {
        $controllerClassName = ucfirst($controllerName) . 'Controller';

        if (!class_exists($controllerClassName)) {
            throw(new Exception("Class $controllerName does not exist"));
        }
        
        return new $controllerClassName($controllerName, $request);
    }
}
