| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | namespace Clansuite\Capture\Protocol; |
| 14: | |
| 15: | use function is_a; |
| 16: | use function is_string; |
| 17: | use Clansuite\Capture\UnknownProtocolException; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | class ProtocolResolver |
| 23: | { |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | public function __construct(private array $protocolMap) |
| 30: | { |
| 31: | } |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | public function resolve(string $protocol, string $ip, int $port): ProtocolInterface |
| 37: | { |
| 38: | if ($protocol === 'auto') { |
| 39: | $protocol = $this->detectProtocol(); |
| 40: | } |
| 41: | |
| 42: | $class = $this->protocolMap[$protocol] ?? throw new UnknownProtocolException($protocol); |
| 43: | |
| 44: | if (!is_string($class) || !is_a($class, ProtocolInterface::class, true)) { |
| 45: | throw new UnknownProtocolException($protocol); |
| 46: | } |
| 47: | |
| 48: | |
| 49: | return new $class; |
| 50: | } |
| 51: | |
| 52: | private function detectProtocol(): string |
| 53: | { |
| 54: | |
| 55: | return 'source'; |
| 56: | } |
| 57: | } |
| 58: | |