diff --git a/src/BusProNet/ApiClient.php b/src/BusProNet/ApiClient.php index c3c08d9..0676ee0 100644 --- a/src/BusProNet/ApiClient.php +++ b/src/BusProNet/ApiClient.php @@ -21,6 +21,8 @@ use Symfony\Component\Serializer\SerializerInterface; class ApiClient { + use ApiClientTrait; + public const TYPE_NOTIFICATION = 'HINWEIS'; public const TYPE_CUSTOMER_DATA = 'KUNDENKONTO'; public const TYPE_BASE_DATA_COUNTRIES = 'STAMMLAENDER'; @@ -279,77 +281,6 @@ class ApiClient return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data); } - /** - * @throws ApiClientException - */ - private function connect() - { - $tries = 1; - $errNo = $errStr = ''; - $maxRetries = $this->config['max_retries']; - $errorCodesForRetry = [ - SOCKET_ECONNREFUSED, - SOCKET_EBADF, - ]; - - $openSocket = function (&$errNo, &$errStr) { - return @fsockopen( - $this->config['bpn_api_ip'], - $this->config['bpn_api_port'], - $errNo, - $errStr, - 10 - ); - }; - - $socket = $openSocket($errNo, $errStr); - - while (false === $socket && true === in_array($errNo, $errorCodesForRetry) && $maxRetries > $tries) { - $this->logger->warning('Could not connect to socket, retrying', [ - 'error_message' => $errStr, - 'error_number' => $errNo, - ]); - ++$tries; - sleep(1); - $socket = $openSocket($errNo, $errStr); - } - - if (false !== $socket) { - stream_set_timeout($socket, 60); - } else { - $this->logger->error('Unable to open socket', [ - 'error_message' => $errStr, - 'error_number' => $errNo, - ]); - throw new ApiClientException('Unable to open socket'); - } - - return $socket; - } - - private function send($socket, string $data): void - { - // message length is prepended to actual message - $send = sprintf('%010s', strlen($data)) . $data; - fwrite($socket, $send); - } - - private function receive($socket): string - { - $response = ''; - - while (false === feof($socket)) { - $response .= fread($socket, 4096); - } - - return $response; - } - - private function disconnect($socket): void - { - @fclose($socket); - } - /** * @throws ApiClientException * @throws ResponseParserException @@ -371,12 +302,17 @@ class ApiClient $this->dumpXmlToFile('request', $requestId, $body); } - $socket = $this->connect(); + $socket = $this->connect( + $this->config['bpn_api_ip'], + $this->config['bpn_api_port'], + $this->config['max_retries'] + ); $this->send($socket, $body); $response = $this->receive($socket); + $this->disconnect($socket); + // message length (10 bytes) is prepended to actual message $xml = substr($response, 10); - $this->disconnect($socket); if (true === $this->config['debug']) { $this->dumpXmlToFile('response', $requestId, $xml); diff --git a/src/BusProNet/ApiClientTrait.php b/src/BusProNet/ApiClientTrait.php new file mode 100644 index 0000000..124b140 --- /dev/null +++ b/src/BusProNet/ApiClientTrait.php @@ -0,0 +1,78 @@ + $tries) { + $this->logger->warning('Could not connect to socket, retrying', [ + 'error_message' => $errStr, + 'error_number' => $errNo, + ]); + ++$tries; + sleep(1); + $socket = $openSocket($errNo, $errStr); + } + + if (false !== $socket) { + stream_set_timeout($socket, 60); + } else { + $this->logger->error('Unable to open socket', [ + 'error_message' => $errStr, + 'error_number' => $errNo, + ]); + throw new ApiClientException('Unable to open socket'); + } + + return $socket; + } + + private function send($socket, string $data): void + { + // message length is prepended to actual message + $send = sprintf('%010s', strlen($data)) . $data; + fwrite($socket, $send); + } + + private function receive($socket): string + { + $response = ''; + + while (false === feof($socket)) { + $response .= fread($socket, 4096); + } + + return $response; + } + + private function disconnect($socket): void + { + @fclose($socket); + } +} \ No newline at end of file