<?php
namespace App\ModuleHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ModuleHandlerManager
{
private $handlers;
public function __construct(iterable $handlers)
{
$this->handlers = $handlers;
}
public function handle(string $module, string $slug): array
{
foreach ($this->handlers as $handler) {
if ($handler instanceof ModuleHandlerInterface) {
if (strtolower((new \ReflectionClass($handler))->getShortName()) === strtolower($module.'Service')) {
return $handler->handle($slug);
}
}
}
throw new NotFoundHttpException('No handler found for module: ' . $module);
}
}