From ab74b7f788a38efa491c36af7ddc174d12b36db8 Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Sun, 17 Feb 2019 14:04:18 +0300 Subject: [PATCH] some methods for work with socket moved to SocketTrait --- src/Client.php | 55 +++++---------------------------------------------- src/Config.php | 2 +- src/SocketTrait.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 51 deletions(-) create mode 100644 src/SocketTrait.php diff --git a/src/Client.php b/src/Client.php index a3c5b0e..b20eba9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -15,6 +15,8 @@ use RouterOS\Helpers\ArrayHelper; */ class Client implements Interfaces\ClientInterface { + use SocketTrait; + /** * Socket resource * @@ -431,10 +433,12 @@ class Client implements Interfaces\ClientInterface * Save socket resource to static variable * * @param resource $socket + * @return Client */ - private function setSocket($socket) + private function setSocket($socket): Client { $this->_socket = $socket; + return $this; } /** @@ -446,53 +450,4 @@ class Client implements Interfaces\ClientInterface { return $this->_socket; } - - /** - * Initiate socket session - * - * @throws \RouterOS\Exceptions\ClientException - * @throws \RouterOS\Exceptions\ConfigException - */ - private function openSocket() - { - // Default: Context for ssl - $context = stream_context_create([ - 'ssl' => [ - 'ciphers' => 'ADH:ALL', - 'verify_peer' => false, - 'verify_peer_name' => false - ] - ]); - - // Default: Proto tcp:// but for ssl we need ssl:// - $proto = $this->config('ssl') ? 'ssl://' : ''; - - // Initiate socket client - $socket = @stream_socket_client( - $proto . $this->config('host') . ':' . $this->config('port'), - $this->_socket_err_num, - $this->_socket_err_str, - $this->config('timeout'), - STREAM_CLIENT_CONNECT, - $context - ); - - // Throw error is socket is not initiated - if (!$socket) { - throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str); - } - - // Save socket to static variable - return $this->setSocket($socket); - } - - /** - * Close socket session - * - * @return bool - */ - private function closeSocket(): bool - { - return fclose($this->_socket); - } } diff --git a/src/Config.php b/src/Config.php index 132af99..edf8d37 100644 --- a/src/Config.php +++ b/src/Config.php @@ -111,7 +111,7 @@ class Config implements ConfigInterface * * @param string $name * @return mixed - * @throws ConfigException + * @throws \RouterOS\Exceptions\ConfigException */ public function get(string $name) { diff --git a/src/SocketTrait.php b/src/SocketTrait.php new file mode 100644 index 0000000..1dbc366 --- /dev/null +++ b/src/SocketTrait.php @@ -0,0 +1,57 @@ + [ + 'ciphers' => 'ADH:ALL', + 'verify_peer' => false, + 'verify_peer_name' => false + ] + ]); + + // Default: Proto tcp:// but for ssl we need ssl:// + $proto = $this->config('ssl') ? 'ssl://' : ''; + + // Initiate socket client + $socket = @stream_socket_client( + $proto . $this->config('host') . ':' . $this->config('port'), + $this->_socket_err_num, + $this->_socket_err_str, + $this->config('timeout'), + STREAM_CLIENT_CONNECT, + $context + ); + + // Throw error is socket is not initiated + if (!$socket) { + throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str); + } + + // Save socket to static variable + return $this->setSocket($socket); + } + + /** + * Close socket session + * + * @return bool + */ + private function closeSocket(): bool + { + return fclose($this->_socket); + } +}