1313
1414class Client
1515{
16- private $ connectionsOptions ;
17- private $ currentConnection ;
18- private $ currentConnectionName ;
16+ private $ connections ;
1917 private $ timeout ;
2018 private $ debug ;
2119 private $ logger ;
20+ private $ currentConnection ;
21+ private $ currentConnectionName ;
2222
2323 /**
24- * @param array $connectionsOptions
25- * @param int $timeout
26- * @param bool $debug
27- * @param LoggerInterface $logger
24+ * @param int $timeout
25+ * @param bool $debug
2826 */
29- public function __construct (array $ connectionsOptions , $ timeout = 10000 , $ debug = false , LoggerInterface $ logger = null )
27+ public function __construct (array $ connections , $ timeout = 10000 , $ debug = false , LoggerInterface $ logger = null )
3028 {
31- if (empty ( $ connectionsOptions ) ) {
29+ if (! $ connections ) {
3230 throw new BadConfigurationException ('At least one connection is required. ' );
3331 }
3432
35- foreach ($ connectionsOptions as $ connectionName => $ connectionOptions ) {
36- $ this ->connectionsOptions [$ connectionName ] = $ this ->resolveConnectionOptions ($ connectionOptions );
33+ foreach ($ connections as $ name => $ connection ) {
34+ $ this ->connections [$ name ] = [
35+ 'remote_socket ' => $ connection ,
36+ 'retries ' => 2 ,
37+ ];
3738 }
3839
3940 $ this ->timeout = $ timeout ;
@@ -94,36 +95,6 @@ public function log(Request $request, Response $response)
9495 }
9596 }
9697
97- private function resolveConnectionOptions (array $ options = [])
98- {
99- if (isset ($ options ['remote_socket ' ])) {
100- $ remoteSocket = explode (': ' , $ options ['remote_socket ' ]);
101-
102- if (!isset ($ remoteSocket [0 ]) || isset ($ remoteSocket [2 ])) {
103- throw new BadConfigurationException ('The option "remote_socket" should have "/link/to/agent/socket" or "ip_agent:port" format. ' );
104- }
105-
106- if (!isset ($ remoteSocket [1 ])) {
107- $ options ['remote_socket ' ] = 'unix:// ' .$ remoteSocket [0 ];
108- } else {
109- $ options ['remote_socket ' ] = sprintf ('tcp://%s:%s ' , $ remoteSocket [0 ], $ remoteSocket [1 ]);
110- }
111-
112- $ options ['retries ' ] = 2 ;
113-
114- return $ options ;
115- }
116-
117- if (!isset ($ options ['host ' ]) || !isset ($ options ['port ' ])) {
118- throw new BadConfigurationException ('The required options "host", "port" are missing. ' );
119- }
120-
121- $ options ['remote_socket ' ] = sprintf ('tcp://%s:%s ' , $ options ['host ' ], $ options ['port ' ]);
122- $ options ['retries ' ] = 2 ;
123-
124- return $ options ;
125- }
126-
12798 private function request ($ command , $ context )
12899 {
129100 $ connection = $ this ->getConnection ();
@@ -134,10 +105,10 @@ private function request($command, $context)
134105 // if the pipe is broken, `fwrite` will throw a Notice
135106 if (false === $ sent ) {
136107 $ this ->logger ->debug ('Impossible to send content to the connection. ' , [
137- 'options ' => $ this ->connectionsOptions [$ this ->currentConnectionName ],
108+ 'options ' => $ this ->connections [$ this ->currentConnectionName ],
138109 ]);
139110
140- --$ this ->connectionsOptions [$ this ->currentConnectionName ]['retries ' ];
111+ --$ this ->connections [$ this ->currentConnectionName ]['retries ' ];
141112 $ this ->currentConnection = null ;
142113
143114 return $ this ->request ($ command , $ context );
@@ -148,10 +119,10 @@ private function request($command, $context)
148119 // false: the persistent connection is stale
149120 if (false === $ received ) {
150121 $ this ->logger ->debug ('Impossible to get content from the connection. ' , [
151- 'options ' => $ this ->connectionsOptions [$ this ->currentConnectionName ],
122+ 'options ' => $ this ->connections [$ this ->currentConnectionName ],
152123 ]);
153124
154- --$ this ->connectionsOptions [$ this ->currentConnectionName ]['retries ' ];
125+ --$ this ->connections [$ this ->currentConnectionName ]['retries ' ];
155126 $ this ->currentConnection = null ;
156127
157128 return $ this ->request ($ command , $ context );
@@ -166,31 +137,31 @@ private function getConnection()
166137 return $ this ->currentConnection ;
167138 }
168139
169- foreach ($ this ->connectionsOptions as $ name => $ connectionOptions ) {
170- if ($ connectionOptions ['retries ' ] <= 0 ) {
140+ foreach ($ this ->connections as $ name => $ connection ) {
141+ if ($ connection ['retries ' ] <= 0 ) {
171142 continue ;
172143 }
173144
174145 $ this ->logger ->debug ('New connection chosen. Trying to connect. ' , [
175- 'connectionOptions ' => $ connectionOptions ,
146+ 'connection ' => $ connection ,
176147 'name ' => $ name ,
177148 ]);
178149
179- $ connection = $ this ->box ('doConnect ' , false , [$ connectionOptions ]);
150+ $ connection = $ this ->box ('doConnect ' , false , [$ connection ]);
180151
181152 if (false === $ connection ) {
182153 $ this ->logger ->debug ('Impossible to connect to the connection. ' , [
183- 'connectionOptions ' => $ connectionOptions ,
154+ 'connection ' => $ connection ,
184155 'name ' => $ name ,
185156 ]);
186157
187- $ this ->connectionsOptions [$ name ]['retries ' ] = 0 ;
158+ $ this ->connections [$ name ]['retries ' ] = 0 ;
188159
189160 continue ;
190161 }
191162
192163 $ this ->logger ->debug ('New connection approved. ' , [
193- 'connectionOptions ' => $ connectionOptions ,
164+ 'connection ' => $ connection ,
194165 'name ' => $ name ,
195166 ]);
196167
@@ -203,7 +174,7 @@ private function getConnection()
203174 }
204175
205176 $ this ->logger ->error ('Can not find an agent. ' , [
206- 'connections_options ' => $ this ->connectionsOptions ,
177+ 'connections_options ' => $ this ->connections ,
207178 ]);
208179
209180 throw new AgentNotFoundException ();
0 commit comments