|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Doctrine; |
| 4 | + |
| 5 | +use Enqueue\ConnectionFactoryFactoryInterface; |
| 6 | +use Enqueue\Dbal\ManagerRegistryConnectionFactory; |
| 7 | +use Enqueue\Dsn\Dsn; |
| 8 | +use Interop\Queue\ConnectionFactory; |
| 9 | +use Symfony\Bridge\Doctrine\RegistryInterface; |
| 10 | + |
| 11 | +class DoctrineConnectionFactoryFactory implements ConnectionFactoryFactoryInterface |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var RegistryInterface |
| 15 | + */ |
| 16 | + private $doctrine; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var ConnectionFactoryFactoryInterface |
| 20 | + */ |
| 21 | + private $fallbackFactory; |
| 22 | + |
| 23 | + public function __construct(RegistryInterface $doctrine, ConnectionFactoryFactoryInterface $fallbackFactory) |
| 24 | + { |
| 25 | + $this->doctrine = $doctrine; |
| 26 | + $this->fallbackFactory = $fallbackFactory; |
| 27 | + } |
| 28 | + |
| 29 | + public function create($config): ConnectionFactory |
| 30 | + { |
| 31 | + if (is_string($config)) { |
| 32 | + $config = ['dsn' => $config]; |
| 33 | + } |
| 34 | + |
| 35 | + if (false == is_array($config)) { |
| 36 | + throw new \InvalidArgumentException('The config must be either array or DSN string.'); |
| 37 | + } |
| 38 | + |
| 39 | + if (false == array_key_exists('dsn', $config)) { |
| 40 | + throw new \InvalidArgumentException('The config must have dsn key set.'); |
| 41 | + } |
| 42 | + |
| 43 | + $dsn = Dsn::parseFirst($config['dsn']); |
| 44 | + |
| 45 | + if ('doctrine' === $dsn->getScheme()) { |
| 46 | + $config = $dsn->getQuery(); |
| 47 | + $config['connection_name'] = $dsn->getHost(); |
| 48 | + |
| 49 | + return new ManagerRegistryConnectionFactory($this->doctrine, $config); |
| 50 | + } |
| 51 | + |
| 52 | + return $this->fallbackFactory->create($config); |
| 53 | + } |
| 54 | +} |
0 commit comments