|
5 | 5 | use Enqueue\Dsn\Dsn; |
6 | 6 | use Interop\Queue\PsrConnectionFactory; |
7 | 7 |
|
8 | | -class ConnectionFactoryFactory |
| 8 | +final class ConnectionFactoryFactory implements ConnectionFactoryFactoryInterface |
9 | 9 | { |
10 | | - /** |
11 | | - * @param string |
12 | | - * |
13 | | - * @return PsrConnectionFactory |
14 | | - */ |
15 | 10 | public function create(string $dsn): PsrConnectionFactory |
16 | 11 | { |
17 | 12 | $dsn = new Dsn($dsn); |
18 | 13 |
|
19 | | - $availableSchemes = Resources::getAvailableSchemes(); |
20 | | - |
21 | | - if (false == array_key_exists($dsn->getScheme(), $availableSchemes)) { |
22 | | - $knownSchemes = Resources::getKnownSchemes(); |
23 | | - if (array_key_exists($dsn->getScheme(), $knownSchemes)) { |
24 | | - $knownConnections = Resources::getKnownConnections(); |
25 | | - |
26 | | - throw new \LogicException(sprintf( |
27 | | - 'A transport "%s" is not installed. Run "composer req %s" to add it.', |
28 | | - $knownSchemes[$dsn->getScheme()], |
29 | | - $knownConnections['package'] |
30 | | - )); |
31 | | - } |
| 14 | + if ($factoryClass = $this->findFactory($dsn, Resources::getAvailableConnections())) { |
| 15 | + return new $factoryClass((string) $dsn); |
| 16 | + } |
32 | 17 |
|
| 18 | + $knownConnections = Resources::getKnownConnections(); |
| 19 | + if ($factoryClass = $this->findFactory($dsn, $knownConnections)) { |
33 | 20 | throw new \LogicException(sprintf( |
34 | | - 'A transport is not known. Make sure you registered it with "%s" if it is custom one.', |
35 | | - Resources::class |
| 21 | + 'To use given scheme "%s" a package has to be installed. Run "composer req %s" to add it.', |
| 22 | + $dsn->getScheme(), |
| 23 | + $knownConnections[$factoryClass]['package'] |
36 | 24 | )); |
37 | 25 | } |
38 | 26 |
|
39 | | - $dsnSchemeExtensions = $dsn->getSchemeExtensions(); |
40 | | - if (false == $dsnSchemeExtensions) { |
41 | | - $factoryClass = $availableSchemes[$dsn->getScheme()]; |
42 | | - |
43 | | - return new $factoryClass((string) $dsn); |
44 | | - } |
| 27 | + throw new \LogicException(sprintf( |
| 28 | + 'A given scheme "%s" is not supported. Maybe it is a custom connection, make sure you registered it with "%s::addConnection".', |
| 29 | + $dsn->getScheme(), |
| 30 | + Resources::class |
| 31 | + )); |
| 32 | + } |
45 | 33 |
|
| 34 | + private function findFactory(Dsn $dsn, array $factories): ?string |
| 35 | + { |
46 | 36 | $protocol = $dsn->getSchemeProtocol(); |
47 | | - foreach ($availableSchemes as $driverClass => $info) { |
| 37 | + foreach ($factories as $connectionClass => $info) { |
48 | 38 | if (false == in_array($protocol, $info['schemes'], true)) { |
49 | 39 | continue; |
50 | 40 | } |
51 | 41 |
|
| 42 | + if (false == $dsn->getSchemeExtensions()) { |
| 43 | + return $connectionClass; |
| 44 | + } |
| 45 | + |
52 | 46 | if (empty($info['supportedSchemeExtensions'])) { |
53 | 47 | continue; |
54 | 48 | } |
55 | 49 |
|
56 | | - $diff = array_diff($dsnSchemeExtensions, $info['supportedSchemeExtensions']); |
| 50 | + $diff = array_diff($dsn->getSchemeExtensions(), $info['supportedSchemeExtensions']); |
57 | 51 | if (empty($diff)) { |
58 | | - $factoryClass = $availableSchemes[$dsn->getScheme()]; |
59 | | - |
60 | | - return new $factoryClass((string) $dsn); |
| 52 | + return $connectionClass; |
61 | 53 | } |
62 | 54 | } |
63 | 55 |
|
64 | | - throw new \LogicException(sprintf('There is no factory that supports scheme "%s"', $dsn->getScheme())); |
| 56 | + return null; |
65 | 57 | } |
66 | 58 | } |
0 commit comments