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