Browse Source

Responses/parameters of some methods in Client class changed from other classes to interfaces, consts moved from ClientInterface to Config class

pull/40/head
Paul Rock 6 years ago
parent
commit
75a201e0f2
  1. 18
      src/Client.php
  2. 56
      src/Config.php
  3. 57
      src/Interfaces/ClientInterface.php
  4. 2
      src/Interfaces/QueryInterface.php

18
src/Client.php

@ -6,6 +6,7 @@ use DivineOmega\SSHConnection\SSHConnection;
use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\ClientException;
use RouterOS\Exceptions\ConfigException; use RouterOS\Exceptions\ConfigException;
use RouterOS\Exceptions\QueryException; use RouterOS\Exceptions\QueryException;
use RouterOS\Interfaces\ClientInterface;
use RouterOS\Interfaces\QueryInterface; use RouterOS\Interfaces\QueryInterface;
use RouterOS\Helpers\ArrayHelper; use RouterOS\Helpers\ArrayHelper;
use function array_keys; use function array_keys;
@ -97,13 +98,13 @@ class Client implements Interfaces\ClientInterface
/** /**
* Send write query to RouterOS * Send write query to RouterOS
* *
* @param string|array|\RouterOS\Query $query
* @param string|array|\RouterOS\Interfaces\QueryInterface $query
* *
* @return \RouterOS\Client
* @return \RouterOS\Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
* @deprecated * @deprecated
*/ */
public function write($query): Client
public function write($query): ClientInterface
{ {
if (is_string($query)) { if (is_string($query)) {
$query = new Query($query); $query = new Query($query);
@ -133,7 +134,7 @@ class Client implements Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @since 1.0.0 * @since 1.0.0
*/ */
public function query($endpoint, array $where = null, string $operations = null, string $tag = null): Client
public function query($endpoint, array $where = null, string $operations = null, string $tag = null): ClientInterface
{ {
// If endpoint is string then build Query object // If endpoint is string then build Query object
$query = ($endpoint instanceof Query) $query = ($endpoint instanceof Query)
@ -175,8 +176,8 @@ class Client implements Interfaces\ClientInterface
* @param \RouterOS\Interfaces\QueryInterface $query * @param \RouterOS\Interfaces\QueryInterface $query
* *
* @return \RouterOS\Query * @return \RouterOS\Query
* @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
* @throws \RouterOS\Exceptions\ClientException
*/ */
private function preQuery(array $item, QueryInterface $query): QueryInterface private function preQuery(array $item, QueryInterface $query): QueryInterface
{ {
@ -197,6 +198,7 @@ class Client implements Interfaces\ClientInterface
break; break;
default: default:
throw new ClientException('From 1 to 3 parameters of "where" condition is allowed'); throw new ClientException('From 1 to 3 parameters of "where" condition is allowed');
break;
} }
return $query->where($key, $operator, $value); return $query->where($key, $operator, $value);
@ -205,13 +207,13 @@ class Client implements Interfaces\ClientInterface
/** /**
* Send write query object to RouterOS * Send write query object to RouterOS
* *
* @param \RouterOS\Query $query
* @param \RouterOS\Interfaces\QueryInterface $query
* *
* @return \RouterOS\Client
* @return \RouterOS\Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
* @since 1.0.0 * @since 1.0.0
*/ */
private function writeRAW(Query $query): Client
private function writeRAW(QueryInterface $query): ClientInterface
{ {
// Send commands via loop to router // Send commands via loop to router
foreach ($query->getQuery() as $command) { foreach ($query->getQuery() as $command) {

56
src/Config.php

@ -17,6 +17,46 @@ use function gettype;
class Config implements ConfigInterface class Config implements ConfigInterface
{ {
/** /**
* By default legacy login on RouterOS pre-6.43 is not supported
*/
public const LEGACY = false;
/**
* Default port number
*/
public const PORT = 8728;
/**
* Default ssl port number
*/
public const PORT_SSL = 8729;
/**
* Do not use SSL by default
*/
public const SSL = false;
/**
* Max timeout for answer from router
*/
public const TIMEOUT = 10;
/**
* Count of reconnect attempts
*/
public const ATTEMPTS = 10;
/**
* Delay between attempts in seconds
*/
public const ATTEMPTS_DELAY = 1;
/**
* Delay between attempts in seconds
*/
public const SSH_PORT = 22;
/**
* List of allowed parameters of config * List of allowed parameters of config
*/ */
public const ALLOWED = [ public const ALLOWED = [
@ -48,12 +88,12 @@ class Config implements ConfigInterface
* @var array * @var array
*/ */
private $_parameters = [ private $_parameters = [
'legacy' => Client::LEGACY,
'ssl' => Client::SSL,
'timeout' => Client::TIMEOUT,
'attempts' => Client::ATTEMPTS,
'delay' => Client::ATTEMPTS_DELAY,
'ssh_port' => Client::SSH_PORT,
'legacy' => self::LEGACY,
'ssl' => self::SSL,
'timeout' => self::TIMEOUT,
'attempts' => self::ATTEMPTS,
'delay' => self::ATTEMPTS_DELAY,
'ssh_port' => self::SSH_PORT,
]; ];
/** /**
@ -111,8 +151,8 @@ class Config implements ConfigInterface
if ($parameter === 'port' && (!isset($this->_parameters['port']) || null === $this->_parameters['port'])) { if ($parameter === 'port' && (!isset($this->_parameters['port']) || null === $this->_parameters['port'])) {
// then use default with or without ssl encryption // then use default with or without ssl encryption
return (isset($this->_parameters['ssl']) && $this->_parameters['ssl']) return (isset($this->_parameters['ssl']) && $this->_parameters['ssl'])
? Client::PORT_SSL
: Client::PORT;
? self::PORT_SSL
: self::PORT;
} }
return null; return null;
} }

57
src/Interfaces/ClientInterface.php

@ -2,9 +2,6 @@
namespace RouterOS\Interfaces; namespace RouterOS\Interfaces;
use RouterOS\Client;
use RouterOS\Query;
/** /**
* Interface ClientInterface * Interface ClientInterface
* *
@ -14,46 +11,6 @@ use RouterOS\Query;
interface ClientInterface interface ClientInterface
{ {
/** /**
* By default legacy login on RouterOS pre-6.43 is not supported
*/
public const LEGACY = false;
/**
* Default port number
*/
public const PORT = 8728;
/**
* Default ssl port number
*/
public const PORT_SSL = 8729;
/**
* Do not use SSL by default
*/
public const SSL = false;
/**
* Max timeout for answer from router
*/
public const TIMEOUT = 10;
/**
* Count of reconnect attempts
*/
public const ATTEMPTS = 10;
/**
* Delay between attempts in seconds
*/
public const ATTEMPTS_DELAY = 1;
/**
* Delay between attempts in seconds
*/
public const SSH_PORT = 22;
/**
* Return socket resource if is exist * Return socket resource if is exist
* *
* @return resource * @return resource
@ -78,21 +35,21 @@ interface ClientInterface
/** /**
* Send write query to RouterOS * Send write query to RouterOS
* *
* @param string|array|\RouterOS\Query $query
* @param string|array|\RouterOS\Interfaces\QueryInterface $query
* *
* @return \RouterOS\Client
* @return \RouterOS\Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
* @deprecated * @deprecated
*/ */
public function write($query): Client;
public function write($query): ClientInterface;
/** /**
* Send write query to RouterOS (modern version of write) * Send write query to RouterOS (modern version of write)
* *
* @param string|Query $endpoint Path of API query or Query object
* @param array|null $where List of where filters
* @param string|null $operations Some operations which need make on response
* @param string|null $tag Mark query with tag
* @param string|\RouterOS\Interfaces\QueryInterface $endpoint Path of API query or Query object
* @param array|null $where List of where filters
* @param string|null $operations Some operations which need make on response
* @param string|null $tag Mark query with tag
* *
* @return \RouterOS\Interfaces\ClientInterface * @return \RouterOS\Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException

2
src/Interfaces/QueryInterface.php

@ -18,7 +18,7 @@ interface QueryInterface
* @param bool|string|int $operator It may be one from list [-,=,>,<] * @param bool|string|int $operator It may be one from list [-,=,>,<]
* *
* @return \RouterOS\Interfaces\QueryInterface * @return \RouterOS\Interfaces\QueryInterface
* @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\QueryException
* @since 1.0.0 * @since 1.0.0
*/ */
public function where(string $key, $operator = '=', $value = null): QueryInterface; public function where(string $key, $operator = '=', $value = null): QueryInterface;

Loading…
Cancel
Save