Teren-app/app/Services/DecisionEvents/Registry.php
2025-10-22 23:20:04 +02:00

37 lines
1.2 KiB
PHP

<?php
namespace App\Services\DecisionEvents;
use App\Services\DecisionEvents\Contracts\DecisionEventHandler;
use App\Services\DecisionEvents\Handlers\AddSegmentHandler;
use InvalidArgumentException;
class Registry
{
/**
* Map of event keys to handler classes.
*
* @var array<string, class-string<DecisionEventHandler>>
*/
protected static array $map = [
'add_segment' => AddSegmentHandler::class,
'archive_contract' => \App\Services\DecisionEvents\Handlers\ArchiveContractHandler::class,
'end_field_job' => \App\Services\DecisionEvents\Handlers\EndFieldJobHandler::class,
];
public static function resolve(string $key): DecisionEventHandler
{
$key = trim(strtolower($key));
$class = static::$map[$key] ?? null;
if (! $class || ! class_exists($class)) {
throw new InvalidArgumentException("Unknown decision event handler for key: {$key}");
}
$handler = app($class);
if (! $handler instanceof DecisionEventHandler) {
throw new InvalidArgumentException("Handler for key {$key} must implement DecisionEventHandler");
}
return $handler;
}
}