1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PamiModule\Service; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use PAMI\Client\Impl\ClientImpl; |
8
|
|
|
use PamiModule\Options\Connection as ConnectionOptions; |
9
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ConnectionFactory. |
13
|
|
|
*/ |
14
|
|
|
class ConnectionFactory extends AbstractFactory |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Get the class name of the options associated with this factory. |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
3 |
|
public function getOptionsClass() |
22
|
|
|
{ |
23
|
3 |
|
return \PamiModule\Options\Connection::class; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param ContainerInterface $container |
28
|
|
|
* @param string $requestedName |
29
|
|
|
* @param array|null $options |
30
|
|
|
* |
31
|
|
|
* @throws \RuntimeException |
32
|
|
|
* @throws \InvalidArgumentException |
33
|
|
|
* |
34
|
|
|
* @return ClientImpl |
35
|
|
|
*/ |
36
|
3 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
37
|
|
|
{ |
38
|
|
|
/* @var $options ConnectionOptions */ |
39
|
3 |
|
$options = $this->getOptions($container, 'connection'); |
40
|
|
|
|
41
|
3 |
|
return $this->createConnection($options); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create service. |
46
|
|
|
* |
47
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
48
|
|
|
* |
49
|
|
|
* @return ClientImpl |
50
|
|
|
*/ |
51
|
1 |
|
public function createService(ServiceLocatorInterface $serviceLocator) |
52
|
|
|
{ |
53
|
1 |
|
return $this($serviceLocator, ClientImpl::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ConnectionOptions $options |
58
|
|
|
* |
59
|
|
|
* @throws InvalidArgumentException |
60
|
|
|
* |
61
|
|
|
* @return ClientImpl |
62
|
|
|
*/ |
63
|
3 |
|
protected function createConnection(ConnectionOptions $options) |
64
|
|
|
{ |
65
|
3 |
|
$eventMask = $options->getEventMask(); |
66
|
|
|
|
67
|
3 |
|
if (is_array($eventMask) && 0 === count($eventMask)) { |
68
|
|
|
$eventMask = 'off'; |
69
|
3 |
|
} elseif (is_array($eventMask)) { |
70
|
|
|
$eventMask = implode(', ', $eventMask); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$clientOptions = [ |
74
|
3 |
|
'host' => $options->getHost(), |
75
|
3 |
|
'port' => $options->getPort(), |
76
|
3 |
|
'username' => $options->getUsername(), |
77
|
3 |
|
'secret' => $options->getSecret(), |
78
|
3 |
|
'connect_timeout' => $options->getConnectTimeout(), |
79
|
3 |
|
'read_timeout' => $options->getReadTimeout(), |
80
|
3 |
|
'scheme' => $options->getScheme(), |
81
|
3 |
|
'event_mask' => $eventMask, |
82
|
3 |
|
]; |
83
|
|
|
|
84
|
|
|
// Disable logging for version <2.0 |
85
|
3 |
|
$clientOptions['log4php.properties'] = [ |
86
|
|
|
'rootLogger' => [ |
87
|
3 |
|
'appenders' => ['default'], |
88
|
3 |
|
], |
89
|
|
|
'appenders' => [ |
90
|
|
|
'default' => [ |
91
|
3 |
|
'class' => 'LoggerAppenderNull', |
92
|
3 |
|
], |
93
|
3 |
|
], |
94
|
|
|
]; |
95
|
|
|
|
96
|
3 |
|
return new ClientImpl($clientOptions); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: