Browse Source

small refatoring of Client class, now is possible to create object with array of settings, getConfig and wr methods added

tags/0.6
Paul Rock 7 years ago
parent
commit
538a51e16e
  1. 63
      src/Client.php

63
src/Client.php

@ -3,13 +3,11 @@
namespace RouterOS; namespace RouterOS;
use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\ClientException;
use RouterOS\Interfaces\ClientInterface;
use RouterOS\Interfaces\ConfigInterface;
use RouterOS\Exceptions\ConfigException; use RouterOS\Exceptions\ConfigException;
use RouterOS\Interfaces\QueryInterface;
/** /**
* Class Client for RouterOS management * Class Client for RouterOS management
*
* @package RouterOS * @package RouterOS
* @since 0.1 * @since 0.1
*/ */
@ -17,37 +15,46 @@ class Client implements Interfaces\ClientInterface
{ {
/** /**
* Socket resource * Socket resource
*
* @var resource|null * @var resource|null
*/ */
private $_socket; private $_socket;
/** /**
* Code of error * Code of error
*
* @var int * @var int
*/ */
private $_socket_err_num; private $_socket_err_num;
/** /**
* Description of socket error * Description of socket error
*
* @var string * @var string
*/ */
private $_socket_err_str; private $_socket_err_str;
/** /**
* Configuration of connection * Configuration of connection
* @var ConfigInterface
*
* @var \RouterOS\Config
*/ */
private $_config; private $_config;
/** /**
* Client constructor. * Client constructor.
* *
* @param ConfigInterface $config
* @param array|\RouterOS\Config $config
* @throws ConfigException * @throws ConfigException
* @throws ClientException * @throws ClientException
*/ */
public function __construct(ConfigInterface $config)
public function __construct($config)
{ {
// If array then need create object
if (\is_array($config)) {
$config = new Config($config);
}
// Check for important keys // Check for important keys
$this->exceptionIfKeyNotExist(['host', 'user', 'pass'], $config); $this->exceptionIfKeyNotExist(['host', 'user', 'pass'], $config);
@ -64,10 +71,10 @@ class Client implements Interfaces\ClientInterface
* Check for important keys * Check for important keys
* *
* @param array $keys * @param array $keys
* @param ConfigInterface $config
* @param Config $config
* @throws ConfigException * @throws ConfigException
*/ */
private function exceptionIfKeyNotExist(array $keys, ConfigInterface $config)
private function exceptionIfKeyNotExist(array $keys, Config $config)
{ {
$parameters = $config->getParameters(); $parameters = $config->getParameters();
foreach ($keys as $key) { foreach ($keys as $key) {
@ -80,8 +87,9 @@ class Client implements Interfaces\ClientInterface
/** /**
* Get some parameter from config * Get some parameter from config
* *
* @param string $parameter
* @param string $parameter Name of required parameter
* @return mixed * @return mixed
* @throws ConfigException
*/ */
private function config(string $parameter) private function config(string $parameter)
{ {
@ -89,6 +97,17 @@ class Client implements Interfaces\ClientInterface
} }
/** /**
* Return socket resource if is exist
*
* @return \RouterOS\Config
* @since 0.6
*/
public function getConfig(): Config
{
return $this->_config;
}
/**
* Encode given length in RouterOS format * Encode given length in RouterOS format
* *
* @param string $string * @param string $string
@ -173,13 +192,27 @@ class Client implements Interfaces\ClientInterface
} }
/** /**
* Alias for ->write()->read() combination of methods
*
* @param Query $query
* @param bool $parse
* @return array
* @throws ClientException
* @since 0.6
*/
public function wr(Query $query, bool $parse = true): array
{
return $this->write($query)->read($parse);
}
/**
* Send write query to RouterOS (with or without tag) * Send write query to RouterOS (with or without tag)
* *
* @param QueryInterface $query
* @return ClientInterface
* @param Query $query
* @return Client
* @throws ClientException * @throws ClientException
*/ */
public function write(QueryInterface $query): ClientInterface
public function write(Query $query): Client
{ {
// Send commands via loop to router // Send commands via loop to router
foreach ($query->getQuery() as $command) { foreach ($query->getQuery() as $command) {
@ -295,6 +328,7 @@ class Client implements Interfaces\ClientInterface
* *
* @return bool * @return bool
* @throws ClientException * @throws ClientException
* @throws ConfigException
*/ */
private function login(): bool private function login(): bool
{ {
@ -307,8 +341,7 @@ class Client implements Interfaces\ClientInterface
// Now need use this hash for authorization // Now need use this hash for authorization
$query = (new Query('/login')) $query = (new Query('/login'))
->add('=name=' . $this->config('user')) ->add('=name=' . $this->config('user'))
->add('=response=00' . md5(\chr(0) . $this->config('pass') . pack('H*',
$response['after']['ret'])));
->add('=response=00' . md5(\chr(0) . $this->config('pass') . pack('H*', $response['after']['ret'])));
} else { } else {
// Just login with our credentials // Just login with our credentials
$query = (new Query('/login')) $query = (new Query('/login'))
@ -328,6 +361,7 @@ class Client implements Interfaces\ClientInterface
* *
* @return bool * @return bool
* @throws ClientException * @throws ClientException
* @throws ConfigException
*/ */
private function connect(): bool private function connect(): bool
{ {
@ -385,6 +419,7 @@ class Client implements Interfaces\ClientInterface
* Initiate socket session * Initiate socket session
* *
* @throws ClientException * @throws ClientException
* @throws ConfigException
*/ */
private function openSocket() private function openSocket()
{ {

Loading…
Cancel
Save