|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RabbitMqModule\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Interop\Container\Exception\ContainerException; |
|
7
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
|
8
|
|
|
use RabbitMqModule\RpcClient; |
|
9
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
|
10
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
|
11
|
|
|
use RabbitMqModule\Options\RpcClient as Options; |
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class RpcClientFactory |
|
16
|
|
|
* @package RabbitMqModule\Service |
|
17
|
|
|
*/ |
|
18
|
|
|
class RpcClientFactory extends AbstractFactory |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Get the class name of the options associated with this factory. |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function getOptionsClass() |
|
26
|
|
|
{ |
|
27
|
1 |
|
return Options::class; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ContainerInterface $serviceLocator |
|
32
|
|
|
* @param Options $options |
|
33
|
|
|
* |
|
34
|
|
|
* @return RpcClient |
|
35
|
|
|
* |
|
36
|
|
|
* @throws InvalidArgumentException |
|
37
|
|
|
*/ |
|
38
|
1 |
|
protected function createClient(ContainerInterface $serviceLocator, Options $options) |
|
39
|
|
|
{ |
|
40
|
|
|
/** @var AbstractConnection $connection */ |
|
41
|
1 |
|
$connection = $serviceLocator->get(sprintf('%s.connection.%s', $this->configKey, $options->getConnection())); |
|
42
|
1 |
|
$producer = new RpcClient($connection); |
|
43
|
1 |
|
$producer->setSerializer($options->getSerializer()); |
|
44
|
|
|
|
|
45
|
1 |
|
return $producer; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Create an object |
|
50
|
|
|
* |
|
51
|
|
|
* @param ContainerInterface $container |
|
52
|
|
|
* @param string $requestedName |
|
53
|
|
|
* @param null|array $options |
|
54
|
|
|
* @return RpcClient |
|
55
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
|
56
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
57
|
|
|
* creating a service. |
|
58
|
|
|
* @throws ContainerException if any other error occurs |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
61
|
|
|
{ |
|
62
|
|
|
/* @var $options Options */ |
|
63
|
1 |
|
$options = $this->getOptions($container, 'rpc_client'); |
|
64
|
|
|
|
|
65
|
1 |
|
return $this->createClient($container, $options); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|