| Conditions | 8 |
| Paths | 6 |
| Total Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 18 | public static function create(string $connection): RelayInterface |
||
| 19 | { |
||
| 20 | if (!preg_match(self::CONNECTION, strtolower($connection), $match)) { |
||
| 21 | throw new Exceptions\RelayFactoryException('unsupported connection format'); |
||
| 22 | } |
||
| 23 | |||
| 24 | switch ($match['protocol']) { |
||
| 25 | case self::TCP_SOCKET: |
||
| 26 | //fall through |
||
| 27 | case self::UNIX_SOCKET: |
||
| 28 | return new SocketRelay( |
||
| 29 | $match['arg1'], |
||
| 30 | isset($match['arg2']) ? (int)$match['arg2'] : null, |
||
| 31 | $match['protocol'] === self::TCP_SOCKET ? SocketRelay::SOCK_TCP : SocketRelay::SOCK_UNIX |
||
| 32 | ); |
||
| 33 | |||
| 34 | case self::STREAM: |
||
| 35 | if (!isset($match['arg2'])) { |
||
| 36 | throw new Exceptions\RelayFactoryException('unsupported stream connection format'); |
||
| 37 | } |
||
| 38 | |||
| 39 | return new StreamRelay( |
||
| 40 | fopen("php://{$match['arg1']}", 'rb'), |
||
| 41 | fopen("php://{$match['arg2']}", 'wb') |
||
| 42 | ); |
||
| 43 | default: |
||
| 44 | throw new Exceptions\RelayFactoryException('unknown connection protocol'); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 |