HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/app/SendingServers/DriverRegistry.php
<?php

namespace App\SendingServers;

use App\Model\SendingServer;
use App\SendingServers\Drivers\SendingDriver;
use App\SendingServers\Exceptions\UnknownSendingServerType;
use InvalidArgumentException;

/**
 * Single registry mapping `sending_servers.type` → driver class. Registered
 * in SendingServerServiceProvider::boot() (built-in types) and via
 * `register_sending_server_driver` Hook (plugins).
 *
 * Resolves to a fresh driver instance per call — composition with the model:
 * driver constructor receives the SendingServer, never extends Eloquent.
 *
 * Wave F replacement for TypeRegistry (which mapped type → STI subclass).
 */
final class DriverRegistry
{
    /** @var array<string, class-string<SendingDriver>> */
    private static array $map = [];

    /**
     * @param  class-string<SendingDriver>  $driverClass
     */
    public static function register(string $type, string $driverClass): void
    {
        if ($type === '') {
            throw new InvalidArgumentException('Sending server type cannot be empty');
        }
        if (!is_subclass_of($driverClass, SendingDriver::class)) {
            throw new InvalidArgumentException(
                "{$driverClass} must implement " . SendingDriver::class
            );
        }
        self::$map[$type] = $driverClass;
    }

    public static function resolve(SendingServer $server): SendingDriver
    {
        $type = $server->type ?? '';
        if (!isset(self::$map[$type])) {
            throw new UnknownSendingServerType($type);
        }
        $class = self::$map[$type];
        return new $class($server);
    }

    public static function has(?string $type): bool
    {
        return $type !== null && $type !== '' && isset(self::$map[$type]);
    }

    /**
     * @return array<string, class-string<SendingDriver>>
     */
    public static function all(): array
    {
        return self::$map;
    }

    /**
     * For tests only.
     */
    public static function reset(): void
    {
        self::$map = [];
    }
}