1
1
<?php
2
2
3
- namespace Struzik \EPPClient \Connection ;
3
+ namespace Struzik \EPPClient \SocketConnection ;
4
4
5
+ use Psr \Log \LoggerInterface ;
6
+ use Struzik \EPPClient \Connection \ConnectionInterface ;
5
7
use Struzik \EPPClient \Exception \ConnectionException ;
8
+ use Struzik \EPPClient \Response \Session \GreetingResponse ;
6
9
use Struzik \ErrorHandler \ErrorHandler ;
7
- use Struzik \ErrorHandler \Processor \IntoExceptionProcessor ;
8
10
use Struzik \ErrorHandler \Exception \ErrorException ;
9
- use Symfony \Component \OptionsResolver \OptionsResolver ;
10
- use Symfony \Component \OptionsResolver \Exception \ExceptionInterface ;
11
- use Psr \Log \LoggerInterface ;
11
+ use Struzik \ErrorHandler \Processor \IntoExceptionProcessor ;
12
12
13
13
/**
14
14
* Connection to EPP server based on stream_socket_client.
@@ -17,17 +17,13 @@ class StreamSocketConnection implements ConnectionInterface
17
17
{
18
18
/**
19
19
* Server connection settings.
20
- *
21
- * @var array
22
20
*/
23
- private $ config ;
21
+ private StreamSocketConfig $ config ;
24
22
25
23
/**
26
24
* Logger object.
27
- *
28
- * @var LoggerInterface
29
25
*/
30
- private $ logger ;
26
+ private LoggerInterface $ logger ;
31
27
32
28
/**
33
29
* Resource of connection to the server.
@@ -36,50 +32,24 @@ class StreamSocketConnection implements ConnectionInterface
36
32
*/
37
33
private $ connection ;
38
34
39
- /**
40
- * Buffer for stream reading.
41
- *
42
- * @var string
43
- */
44
- private $ buffer ;
45
-
46
35
/**
47
36
* Creating connection object to the EPP server.
48
37
*
49
- * @param array $config
50
- * 'uri' - (string) EPP server URI
51
- * 'timeout' - (int) Number of seconds until the connect() system call should timeout.
52
- * 'context' - (array) Value of $options parameter in stream_context_create(). See: https://secure.php.net/manual/en/function.stream-context-create.php
53
- * @param LoggerInterface $logger PSR-3 compatible logger
54
- *
55
- * @throws ConnectionException
38
+ * @param StreamSocketConfig $config connection settings
39
+ * @param LoggerInterface $logger PSR-3 compatible logger
56
40
*/
57
- public function __construct (array $ config = [] , LoggerInterface $ logger )
41
+ public function __construct (StreamSocketConfig $ config , LoggerInterface $ logger )
58
42
{
59
- try {
60
- $ this ->logger = $ logger ;
61
-
62
- $ resolver = new OptionsResolver ();
63
- $ resolver ->setRequired ('uri ' );
64
- $ resolver ->setRequired ('timeout ' );
65
- $ resolver ->setRequired ('context ' );
66
- $ resolver ->setDefault ('context ' , []);
67
- $ resolver ->setAllowedTypes ('uri ' , 'string ' );
68
- $ resolver ->setAllowedTypes ('timeout ' , 'int ' );
69
- $ resolver ->setAllowedTypes ('context ' , 'array ' );
70
-
71
- $ this ->config = $ resolver ->resolve ($ config );
72
- } catch (ExceptionInterface $ e ) {
73
- throw new ConnectionException ('Invalid configuration parameters. See previous exception. ' , 0 , $ e );
74
- }
43
+ $ this ->config = $ config ;
44
+ $ this ->logger = $ logger ;
75
45
}
76
46
77
47
/**
78
48
* {@inheritdoc}
79
49
*
80
50
* @throws ConnectionException
81
51
*/
82
- public function open ()
52
+ public function open (): void
83
53
{
84
54
try {
85
55
// Setting up error handler
@@ -88,8 +58,18 @@ public function open()
88
58
$ errorHandler ->set ();
89
59
90
60
// Trying to open connection
91
- $ context = stream_context_create ($ this ->config ['context ' ]);
92
- $ this ->connection = stream_socket_client ($ this ->config ['uri ' ], $ errno , $ errstr , $ this ->config ['timeout ' ], STREAM_CLIENT_CONNECT , $ context );
61
+ $ context = stream_context_create ($ this ->config ->context );
62
+ $ this ->connection = stream_socket_client ($ this ->config ->uri , $ errno , $ errstr , $ this ->config ->timeout , STREAM_CLIENT_CONNECT , $ context );
63
+
64
+ // Read greeting
65
+ $ greetingXML = $ this ->read ();
66
+ $ this ->logger ->info (sprintf ('Read greeting on connect: %s ' , $ greetingXML ));
67
+
68
+ // Check greeting
69
+ $ greeting = new GreetingResponse ($ greetingXML );
70
+ if (!$ greeting ->isSuccess ()) {
71
+ throw new ConnectionException ('Invalid greeting content. Node <greeting> not found. See log for details. ' );
72
+ }
93
73
94
74
// Restore previous error handler
95
75
$ errorHandler ->restore ();
@@ -102,22 +82,22 @@ public function open()
102
82
/**
103
83
* {@inheritdoc}
104
84
*/
105
- public function isOpened ()
85
+ public function isOpened (): bool
106
86
{
107
87
return is_resource ($ this ->connection ) && !feof ($ this ->connection );
108
88
}
109
89
110
90
/**
111
91
* {@inheritdoc}
112
92
*/
113
- public function close ()
93
+ public function close (): void
114
94
{
115
95
if (!$ this ->isOpened ()) {
116
96
return ;
117
97
}
118
98
119
99
if (fclose ($ this ->connection ) === false ) {
120
- throw new ConnectionException ('An error occured while closing the connection. ' );
100
+ throw new ConnectionException ('An error occurred while closing the connection. ' );
121
101
}
122
102
123
103
$ this ->connection = null ;
@@ -126,11 +106,9 @@ public function close()
126
106
/**
127
107
* {@inheritdoc}
128
108
*
129
- * @return string
130
- *
131
109
* @throws ConnectionException
132
110
*/
133
- public function read ()
111
+ public function read (): string
134
112
{
135
113
// Checking open connection
136
114
if (!$ this ->isOpened ()) {
@@ -145,29 +123,29 @@ public function read()
145
123
146
124
// Trying to read a response
147
125
$ beginTime = microtime (true );
148
- $ this -> buffer = '' ;
126
+ $ readBuffer = '' ;
149
127
$ length = $ this ->readResponseLength ();
150
128
$ this ->logger ->debug (sprintf ('The length of the response body is %s bytes. ' , $ length ));
151
129
if ($ length ) {
152
- for ($ i = 0 ; (strlen ($ this -> buffer ) < $ length ) && ($ i < 25 ); ++$ i ) {
130
+ for ($ i = 0 ; (strlen ($ readBuffer ) < $ length ) && ($ i < 25 ); ++$ i ) {
153
131
usleep ($ i * 100000 ); // 100000 = 1/10 seconds
154
- $ residualLength = $ length - strlen ($ this -> buffer );
132
+ $ residualLength = $ length - strlen ($ readBuffer );
155
133
$ this ->logger ->debug (sprintf ('Trying to read %s bytes of the response body. ' , $ residualLength ), ['iteration-number ' => $ i ]);
156
- $ this -> buffer .= fread ($ this ->connection , $ residualLength );
134
+ $ readBuffer .= fread ($ this ->connection , $ residualLength );
157
135
}
158
136
}
159
137
$ endTime = microtime (true );
160
138
$ this ->logger ->debug (sprintf ('The response time is %s seconds. ' , round ($ endTime - $ beginTime , 3 )));
161
139
162
140
// Checking lengths of the response body
163
- if ($ length !== strlen ($ this -> buffer )) {
141
+ if ($ length !== strlen ($ readBuffer )) {
164
142
throw new ConnectionException ('The number of bytes of a response body is not equal to the number of bytes from header. ' );
165
143
}
166
144
167
145
// Restore previous error handler
168
146
$ errorHandler ->restore ();
169
147
170
- return $ this -> buffer ;
148
+ return $ readBuffer ;
171
149
} catch (ErrorException $ e ) {
172
150
throw new ConnectionException ('An error occurred while trying to read the response. See previous exception. ' , 0 , $ e );
173
151
}
@@ -178,7 +156,7 @@ public function read()
178
156
*
179
157
* @throws ConnectionException
180
158
*/
181
- public function write ($ xml )
159
+ public function write (string $ xml ): void
182
160
{
183
161
// Checking open connection
184
162
if (!$ this ->isOpened ()) {
@@ -214,10 +192,8 @@ public function write($xml)
214
192
* Preparing the request for sending to the EPP server. Adds a header to the command text.
215
193
*
216
194
* @param string $xml RAW-request without header
217
- *
218
- * @return string
219
195
*/
220
- protected function prependHeader ($ xml )
196
+ protected function prependHeader (string $ xml ): string
221
197
{
222
198
$ header = pack ('N ' , strlen ($ xml ) + self ::HEADER_LENGTH );
223
199
@@ -226,10 +202,8 @@ protected function prependHeader($xml)
226
202
227
203
/**
228
204
* Returns the length of the response (without header) in bytes.
229
- *
230
- * @return int
231
205
*/
232
- protected function readResponseLength ()
206
+ protected function readResponseLength (): int
233
207
{
234
208
// Executing several attempt for reading
235
209
$ rawHeader = '' ;
@@ -243,8 +217,7 @@ protected function readResponseLength()
243
217
// Unpack header from binary string
244
218
$ this ->logger ->debug (sprintf ('Number of bytes of a response header: %s ' , strlen ($ rawHeader )));
245
219
$ unpackedHeader = unpack ('N ' , $ rawHeader );
246
- $ length = $ unpackedHeader [1 ] - self ::HEADER_LENGTH ;
247
220
248
- return $ length ;
221
+ return $ unpackedHeader [ 1 ] - self :: HEADER_LENGTH ;
249
222
}
250
223
}
0 commit comments