Skip to content

Commit

Permalink
Merge 38401d2 into 703ea33
Browse files Browse the repository at this point in the history
  • Loading branch information
johnny-bit committed Nov 1, 2019
2 parents 703ea33 + 38401d2 commit e2d5992
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 80 deletions.
157 changes: 97 additions & 60 deletions src/AfriCC/EPP/AbstractClient.php
Expand Up @@ -24,6 +24,8 @@ abstract class AbstractClient implements ClientInterface
protected $port;
protected $username;
protected $password;
protected $lang;
protected $version;
protected $services;
protected $serviceExtensions;
protected $ssl;
Expand Down Expand Up @@ -98,6 +100,7 @@ public function __construct(array $config, ObjectSpec $objectSpec = null)
$this->prepareCredentials($config);
$this->prepareSSLOptions($config);
$this->prepareEPPServices($config);
$this->prepareEPPVersionLang($lang);
}

/**
Expand All @@ -120,88 +123,122 @@ public function setObjectSpec(ObjectSpec $newObjectSpec)
$this->objectSpec = $newObjectSpec;
}

protected function prepareConnectionOptions(array $config)
/**
* Get config value from config array if set (default otherwise)
*
* Basically, a simple null coallesce operator (since we need to support PHP 5.5)
*
* @param array $config
* @param string $key
* @param mixed $default
* @return mixed
*/
protected function getConfigDefault(array $config, string $key, $default = null)
{
if (!empty($config['host'])) {
$this->host = (string) $config['host'];
}

if (!empty($config['port'])) {
$this->port = (int) $config['port'];
} else {
$this->port = false;
}

if (!empty($config['connect_timeout'])) {
$this->connect_timeout = (int) $config['connect_timeout'];
} else {
$this->connect_timeout = 16;
}

if (!empty($config['timeout'])) {
$this->timeout = (int) $config['timeout'];
} else {
$this->timeout = 32;
if (!empty($config[$key])) {
return $config[$key];
}
return $default;
}

protected function prepareCredentials(array $config)
/**
* Get config value from config array if set (default otherwise)
*
* special version for bool values
*
* @param array $config
* @param string $key
* @param mixed $default
* @return mixed
* @see AbstractClient::getConfigDefault
*/
protected function getConfigDefaultBool(array $config, string $key, $default = null)
{
if (!empty($config['username'])) {
$this->username = (string) $config['username'];
}

if (!empty($config['password'])) {
$this->password = (string) $config['password'];
if (!empty($config[$key]) && is_bool($config[$key])) {
return $config[$key];
}
return $default;
}

protected function prepareSSLOptions(array $config)
/**
* Get config value from config array if set (default otherwise)
*
* special version for aray values
*
* @param array $config
* @param string $key
* @param mixed $default
* @return mixed
* @see AbstractClient::getConfigDefault
*/
protected function getConfigDefaultArray(array $config, string $key, $default = null)
{
if ((!empty($config['ssl']) && is_bool($config['ssl']))) {
$this->ssl = $config['ssl'];
} else {
$this->ssl = false;
if (!empty($config[$key]) && is_array($config[$key])) {
return $config[$key];
}
return $default;
}

if (!empty($config['local_cert'])) {
$this->local_cert = (string) $config['local_cert'];
/**
* Get config value from config array if set (default otherwise)
*
* special version for files
*
* @param array $config
* @param string $key
* @param mixed $default
* @return mixed
* @throws Exception in case file is specified but not readable
* @see AbstractClient::getConfigDefault
*/
protected function getConfigDefaultReadableFile(array $config, string $key, $default = null)
{
if (!empty($config[$key])) {
$return = (string) $config[$key];

if (!is_readable($this->local_cert)) {
throw new \Exception(sprintf('unable to read local_cert: %s', $this->local_cert));
if (!is_readable($return)) {
throw new \Exception(sprintf('unable to read %s: %s',$key, $return));
}
return $return;
}
return $default;
}

if (!empty($config['ca_cert'])) {
$this->ca_cert = (string) $config['ca_cert'];
protected function prepareConnectionOptions(array $config)
{
$this->host = $this->getConfigDefault($config, 'host');
$this->port = $this->getConfigDefault($config, 'port', false);
$this->connect_timeout = (int) $this->getConfigDefault($config, 'connect_timeout', 16);
$this->timeout = (int) $this->getConfigDefault($config, 'timeout', 32);
}

if (!is_readable($this->ca_cert)) {
throw new \Exception(sprintf('unable to read ca_cert: %s', $this->ca_cert));
}
}
protected function prepareCredentials(array $config)
{
$this->username = $this->getConfigDefault($config, 'username');
$this->password = $this->getConfigDefault($config, 'password');
}

if (!empty($config['pk_cert'])) {
$this->pk_cert = (string) $config['pk_cert'];
protected function prepareSSLOptions(array $config)
{
$this->ssl = $this->getConfigDefaultBool($config, 'ssl', false);

if (!is_readable($this->pk_cert)) {
throw new \Exception(sprintf('unable to read pk_cert: %s', $this->pk_cert));
}
}
$this->local_cert = $this->getConfigDefaultReadableFile($config, 'local_cert');
$this->ca_cert = $this->getConfigDefaultReadableFile($config, 'ca_cert');
$this->pk_cert = $this->getConfigDefaultReadableFile($config, 'pk_cert');

if (!empty($config['passphrase'])) {
$this->passphrase = (string) $config['passphrase'];
}
$this->passphrase = $this->getConfigDefault($config, 'passphrase');
}

protected function prepareEPPServices(array $config)
{
if (!empty($config['services']) && is_array($config['services'])) {
$this->services = $config['services'];
$this->services = $this->getConfigDefaultArray($config, 'services');
$this->serviceExtensions = $this->getConfigDefaultArray($config, 'serviceExtensions');
}

if (!empty($config['serviceExtensions']) && is_array($config['serviceExtensions'])) {
$this->serviceExtensions = $config['serviceExtensions'];
}
}
protected function prepareEPPVersionLang(array $config)
{
$this->lang = $this->getConfigDefault($config, 'lang', 'en');
$this->version = $this->getConfigDefault($config, 'version', '1.0');
}

protected function generateClientTransactionId()
Expand All @@ -227,8 +264,8 @@ protected function login($newPassword = false)
if ($newPassword) {
$login->setNewPassword($newPassword);
}
$login->setVersion('1.0');
$login->setLanguage('en');
$login->setVersion($this->version);
$login->setLanguage($this->lang);

if (!empty($this->services) && is_array($this->services)) {
foreach ($this->services as $urn) {
Expand Down
19 changes: 4 additions & 15 deletions src/AfriCC/EPP/Client.php
Expand Up @@ -32,22 +32,11 @@ public function __construct(array $config, ObjectSpec $objectSpec = null)
{
parent::__construct($config, $objectSpec);

if (!empty($config['chunk_size'])) {
$this->chunk_size = (int) $config['chunk_size'];
} else {
$this->chunk_size = 1024;
}

if (isset($config['verify_peer_name'])) {
$this->verify_peer_name = (bool) $config['verify_peer_name'];
} else {
$this->verify_peer_name = true;
}
$this->chunk_size = (int) $this->getConfigDefault($config, 'chunk_size', 1024);
$this->verify_peer_name = $this->getConfigDefaultBool($config, 'verify_peer_name', true);

if ($this->port === false) {
// if not set, default port is 700
$this->port = 700;
}
// override 'port' default to false from AbstractClient::prepareConnectionOptions
$this->port = (int) $this->getConfigDefault($config, 'port', 700);
}

public function __destruct()
Expand Down
6 changes: 1 addition & 5 deletions src/AfriCC/EPP/HTTPClient.php
Expand Up @@ -33,11 +33,7 @@ public function __construct(array $config, ObjectSpec $objectSpec = null)

protected function prepareCookieJar(array $config)
{
if (!empty($config['cookiejar'])) {
$this->cookiejar = $config['cookiejar'];
} else {
$this->cookiejar = tempnam(sys_get_temp_dir(), 'ehc');
}
$this->cookiejar = $this->getConfigDefault($config, 'cookiejar', tempnam(sys_get_temp_dir(), 'ehc'));

if (!is_readable($this->cookiejar) || !is_writable($this->cookiejar)) {
throw new \Exception(
Expand Down

0 comments on commit e2d5992

Please sign in to comment.