|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Enqueue\Wamp; |
| 6 | + |
| 7 | +use Enqueue\Dsn\Dsn; |
| 8 | +use Interop\Queue\ConnectionFactory; |
| 9 | +use Interop\Queue\Context; |
| 10 | +use Thruway\Peer\Client; |
| 11 | +use Thruway\Transport\PawlTransportProvider; |
| 12 | + |
| 13 | +class WampConnectionFactory implements ConnectionFactory |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var array |
| 17 | + */ |
| 18 | + private $config; |
| 19 | + |
| 20 | + /** |
| 21 | + * The config could be an array, string DSN or null. In case of null it will attempt to connect to Ratchet localhost. |
| 22 | + * |
| 23 | + * $config = [ |
| 24 | + * 'lazy' => true, |
| 25 | + * 'dsn' => 'wamp://127.0.0.1:9090', |
| 26 | + * 'host' => '127.0.0.1', |
| 27 | + * 'port' => '9090', |
| 28 | + * 'max_retries' => 15, |
| 29 | + * 'initial_retry_delay' => 1.5, |
| 30 | + * 'max_retry_delay' => 300, |
| 31 | + * 'retry_delay_growth' => 1.5, |
| 32 | + * ] |
| 33 | + * |
| 34 | + * or |
| 35 | + * |
| 36 | + * wamp://127.0.0.1:9090?max_retries=10 |
| 37 | + * |
| 38 | + * @param array|string|null $config |
| 39 | + */ |
| 40 | + public function __construct($config = 'wamp:') |
| 41 | + { |
| 42 | + if (empty($config)) { |
| 43 | + $config = $this->parseDsn('wamp:'); |
| 44 | + } elseif (is_string($config)) { |
| 45 | + $config = $this->parseDsn($config); |
| 46 | + } elseif (is_array($config)) { |
| 47 | + $config = empty($config['dsn']) ? $config : $this->parseDsn($config['dsn']); |
| 48 | + } else { |
| 49 | + throw new \LogicException('The config must be either an array of options, a DSN string or null'); |
| 50 | + } |
| 51 | + |
| 52 | + $config = array_replace([ |
| 53 | + 'lazy' => true, |
| 54 | + 'host' => '127.0.0.1', |
| 55 | + 'port' => '9090', |
| 56 | + 'max_retries' => 15, |
| 57 | + 'initial_retry_delay' => 1.5, |
| 58 | + 'max_retry_delay' => 300, |
| 59 | + 'retry_delay_growth' => 1.5, |
| 60 | + ], $config); |
| 61 | + |
| 62 | + $this->config = $config; |
| 63 | + } |
| 64 | + |
| 65 | + public function createContext(): Context |
| 66 | + { |
| 67 | + if ($this->config['lazy']) { |
| 68 | + return new WampContext(function () { |
| 69 | + return $this->establishConnection(); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + return new WampContext($this->establishConnection()); |
| 74 | + } |
| 75 | + |
| 76 | + private function establishConnection(): Client |
| 77 | + { |
| 78 | + $uri = sprintf('ws://%s:%s', $this->config['host'], $this->config['port']); |
| 79 | + |
| 80 | + $client = new Client('realm1'); |
| 81 | + $client->addTransportProvider(new PawlTransportProvider($uri)); |
| 82 | + $client->setReconnectOptions([ |
| 83 | + 'max_retries' => $this->config['max_retries'], |
| 84 | + 'initial_retry_delay' => $this->config['initial_retry_delay'], |
| 85 | + 'max_retry_delay' => $this->config['max_retry_delay'], |
| 86 | + 'retry_delay_growth' => $this->config['retry_delay_growth'], |
| 87 | + ]); |
| 88 | + |
| 89 | + return $client; |
| 90 | + } |
| 91 | + |
| 92 | + private function parseDsn(string $dsn): array |
| 93 | + { |
| 94 | + $dsn = new Dsn($dsn); |
| 95 | + |
| 96 | + if ('wamp' !== $dsn->getSchemeProtocol()) { |
| 97 | + throw new \LogicException(sprintf( |
| 98 | + 'The given scheme protocol "%s" is not supported. It must be "wamp"', |
| 99 | + $dsn->getSchemeProtocol() |
| 100 | + )); |
| 101 | + } |
| 102 | + |
| 103 | + return array_filter(array_replace($dsn->getQuery(), [ |
| 104 | + 'host' => $dsn->getHost(), |
| 105 | + 'port' => $dsn->getPort(), |
| 106 | + 'max_retries' => $dsn->getInt('max_retries'), |
| 107 | + 'initial_retry_delay' => $dsn->getFloat('initial_retry_delay'), |
| 108 | + 'max_retry_delay' => $dsn->getInt('max_retry_delay'), |
| 109 | + 'retry_delay_growth' => $dsn->getFloat('retry_delay_growth'), |
| 110 | + ]), function ($value) { return null !== $value; }); |
| 111 | + } |
| 112 | +} |
0 commit comments