|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RabbitMqModule\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Interop\Container\Exception\ContainerException; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use RabbitMqModule\Service\Connection\ConnectionFactoryInterface; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
|
11
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
|
12
|
|
|
use RabbitMqModule\Options\Connection as ConnectionOptions; |
|
13
|
|
|
use RabbitMqModule\Service\Connection; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ConnectionFactory |
|
17
|
|
|
* @package RabbitMqModule\Service |
|
18
|
|
|
*/ |
|
19
|
|
|
class ConnectionFactory extends AbstractFactory |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $factoryMap = [ |
|
25
|
|
|
'stream' => Connection\StreamConnectionFactory::class, |
|
26
|
|
|
'socket' => Connection\SocketConnectionFactory::class, |
|
27
|
|
|
'ssl' => Connection\SSLConnectionFactory::class, |
|
28
|
|
|
'lazy' => Connection\LazyConnectionFactory::class, |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
3 |
|
public function getFactoryMap() |
|
35
|
|
|
{ |
|
36
|
3 |
|
return $this->factoryMap; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $factoryMap |
|
41
|
|
|
* |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function setFactoryMap(array $factoryMap) |
|
45
|
|
|
{ |
|
46
|
2 |
|
$this->factoryMap = $factoryMap; |
|
47
|
|
|
|
|
48
|
2 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the class name of the options associated with this factory. |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function getOptionsClass() |
|
57
|
|
|
{ |
|
58
|
3 |
|
return ConnectionOptions::class; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param ContainerInterface $serviceLocator |
|
63
|
|
|
* @param string $type |
|
64
|
|
|
* |
|
65
|
|
|
* @return ConnectionFactoryInterface |
|
66
|
|
|
* |
|
67
|
|
|
* @throws InvalidArgumentException |
|
68
|
|
|
* @throws RuntimeException |
|
69
|
|
|
*/ |
|
70
|
3 |
|
protected function getFactory(ContainerInterface $serviceLocator, $type) |
|
71
|
|
|
{ |
|
72
|
3 |
|
$map = $this->getFactoryMap(); |
|
73
|
3 |
|
if (!array_key_exists($type, $map)) { |
|
74
|
1 |
|
throw new InvalidArgumentException(sprintf('Options type "%s" not valid', $type)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
$className = $map[$type]; |
|
78
|
2 |
|
$factory = $serviceLocator->get($className); |
|
79
|
2 |
|
if (!$factory instanceof ConnectionFactoryInterface) { |
|
80
|
1 |
|
throw new RuntimeException( |
|
81
|
1 |
|
sprintf('Factory for type "%s" must be an instance of ConnectionFactoryInterface', $type) |
|
82
|
1 |
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
return $factory; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Create an object |
|
90
|
|
|
* |
|
91
|
|
|
* @param ContainerInterface $container |
|
92
|
|
|
* @param string $requestedName |
|
93
|
|
|
* @param null|array $options |
|
94
|
|
|
* @return \PhpAmqpLib\Channel\AbstractChannel |
|
95
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
|
96
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
97
|
|
|
* creating a service. |
|
98
|
|
|
* @throws ContainerException if any other error occurs |
|
99
|
|
|
*/ |
|
100
|
3 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
101
|
|
|
{ |
|
102
|
|
|
/* @var $options ConnectionOptions */ |
|
103
|
3 |
|
$options = $this->getOptions($container, 'connection'); |
|
104
|
3 |
|
$factory = $this->getFactory($container, $options->getType()); |
|
105
|
|
|
|
|
106
|
1 |
|
return $factory->createConnection($options); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|