|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PamiModule\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Interop\Container\Exception\ContainerException; |
|
7
|
|
|
use PAMI\Client\Impl\ClientImpl; |
|
8
|
|
|
use PamiModule\Event\EventForwarder; |
|
9
|
|
|
use PamiModule\Options\Client as ClientOptions; |
|
10
|
|
|
use PamiModule\Options\Connection; |
|
11
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
|
12
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
|
13
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ClientFactory. |
|
17
|
|
|
*/ |
|
18
|
|
|
class ClientFactory extends AbstractFactory |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Get the class name of the options associated with this factory. |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getOptionsClass() |
|
26
|
|
|
{ |
|
27
|
|
|
return \PamiModule\Options\Client::class; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create an object. |
|
32
|
|
|
* |
|
33
|
|
|
* @param ContainerInterface $container |
|
34
|
|
|
* @param string $requestedName |
|
35
|
|
|
* @param null|array $options |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \RuntimeException |
|
38
|
|
|
* @throws \Interop\Container\Exception\NotFoundException |
|
39
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service |
|
40
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
41
|
|
|
* creating a service |
|
42
|
|
|
* @throws ContainerException if any other error occurs |
|
43
|
|
|
* |
|
44
|
|
|
* @return Client |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$options = null; |
|
49
|
|
|
$connectionName = $this->getName(); |
|
50
|
|
|
|
|
51
|
|
|
if ($this->hasOptions($container, 'client', $this->getName())) { |
|
52
|
|
|
/** @var ClientOptions $options */ |
|
53
|
|
|
$options = $this->getOptions($container, 'client'); |
|
54
|
|
|
$connectionName = $options->getConnection() ?: $this->getName(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$connectionFactory = new ConnectionFactory($connectionName); |
|
58
|
|
|
/** @var Connection $connectionOptions */ |
|
59
|
|
|
$connectionOptions = $connectionFactory->getOptions($container, 'connection'); |
|
60
|
|
|
|
|
61
|
|
|
/** @var ClientImpl $connection */ |
|
62
|
|
|
$connection = $container->get(sprintf('pami.connection.%s', $connectionName)); |
|
63
|
|
|
|
|
64
|
|
|
$client = new Client($connectionOptions->getHost(), $connection); |
|
65
|
|
|
|
|
66
|
|
|
if ($options) { |
|
67
|
|
|
$client->setParams($options->getParams()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$eventForwarder = new EventForwarder($client); |
|
71
|
|
|
$client->getConnection()->registerEventListener($eventForwarder); |
|
72
|
|
|
|
|
73
|
|
|
return $client; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create service. |
|
78
|
|
|
* |
|
79
|
|
|
* @param ServiceLocatorInterface $serviceLocator Service locator |
|
80
|
|
|
* |
|
81
|
|
|
* @return Client |
|
82
|
|
|
*/ |
|
83
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this($serviceLocator, Client::class); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|