| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Services; |
| 4 |
|
| 5 |
use AATXT\App\AltTextGeneratorInterface; |
| 6 |
use AATXT\App\Domain\Exceptions\UnsupportedGeneratorException; |
| 7 |
|
| 8 |
/** |
| 9 |
* Configuration-based implementation of AltTextGeneratorFactory. |
| 10 |
* |
| 11 |
* This factory allows registering generator creators (callables) for different |
| 12 |
* types and then creating instances on demand. This implements the Factory Pattern |
| 13 |
* and follows the Open/Closed Principle. |
| 14 |
* |
| 15 |
* Usage: |
| 16 |
* ```php |
| 17 |
* $factory = new ConfigBasedGeneratorFactory(); |
| 18 |
* $factory->register('openai', fn() => new AltTextGeneratorAi(new OpenAIVision(...))); |
| 19 |
* $generator = $factory->create('openai'); |
| 20 |
* ``` |
| 21 |
*/ |
| 22 |
final class ConfigBasedGeneratorFactory implements AltTextGeneratorFactory |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Registered generator creators. |
| 26 |
* |
| 27 |
* Maps generator type strings to callables that create generator instances. |
| 28 |
* |
| 29 |
* @var array<string, callable> |
| 30 |
*/ |
| 31 |
private $generators; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor |
| 35 |
*/ |
| 36 |
public function __construct() |
| 37 |
{ |
| 38 |
$this->generators = []; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Register a generator creator for a specific type. |
| 43 |
* |
| 44 |
* The creator callable should return an instance of AltTextGeneratorInterface. |
| 45 |
* |
| 46 |
* @param string $type The generator type identifier |
| 47 |
* @param callable $creator A callable that returns an AltTextGeneratorInterface instance |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function register(string $type, callable $creator): void |
| 51 |
{ |
| 52 |
$this->generators[$type] = $creator; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Create an alt text generator instance for the given type. |
| 57 |
* |
| 58 |
* @param string $type The generator type identifier |
| 59 |
* @return AltTextGeneratorInterface The generator instance |
| 60 |
* @throws UnsupportedGeneratorException If the type is not registered |
| 61 |
*/ |
| 62 |
public function create(string $type): AltTextGeneratorInterface |
| 63 |
{ |
| 64 |
if (!isset($this->generators[$type])) { |
| 65 |
throw new UnsupportedGeneratorException($type); |
| 66 |
} |
| 67 |
|
| 68 |
$creator = $this->generators[$type]; |
| 69 |
return $creator(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Check if a generator type is registered. |
| 74 |
* |
| 75 |
* @param string $type The generator type to check |
| 76 |
* @return bool True if registered, false otherwise |
| 77 |
*/ |
| 78 |
public function has(string $type): bool |
| 79 |
{ |
| 80 |
return isset($this->generators[$type]); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get all registered generator types. |
| 85 |
* |
| 86 |
* @return array<string> List of registered types |
| 87 |
*/ |
| 88 |
public function getRegisteredTypes(): array |
| 89 |
{ |
| 90 |
return array_keys($this->generators); |
| 91 |
} |
| 92 |
} |
| 93 |
|