Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmaron committed Apr 29, 2019
1 parent 7bf067f commit 785da68
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 101 deletions.
167 changes: 99 additions & 68 deletions src/AbstractReportingCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,26 @@ abstract class AbstractReportingCloud
private $password;

/**
* When true, API call does not count against quota
* "TEST MODE" watermark is added to document
* Backend base URI
*
* @var bool|null
* @var string|null
*/
private $test;
private $baseUri;

/**
* Backend base URI
* Debug flag of REST client
*
* @var string|null
* @var bool|null
*/
private $baseUri;
private $debug;

/**
* Backend version string
* When true, API call does not count against quota
* "TEST MODE" watermark is added to document
*
* @var string|null
* @var bool|null
*/
private $version;
private $test;

/**
* Backend timeout in seconds
Expand All @@ -286,11 +286,11 @@ abstract class AbstractReportingCloud
private $timeout;

/**
* Debug flag of REST client
* Backend version string
*
* @var bool|null
* @var string|null
*/
private $debug;
private $version;

/**
* REST client to backend
Expand Down Expand Up @@ -400,73 +400,73 @@ public function setBaseUri(string $baseUri): self
}

/**
* Get the timeout (in seconds) of the backend web service
* Return the debug flag
*
* @return int|null
* @return bool|null
*/
public function getTimeout(): ?int
public function getDebug(): ?bool
{
return $this->timeout;
return $this->debug;
}

/**
* Set the timeout (in seconds) of the backend web service
* Set the debug flag
*
* @param int $timeout
* @param bool $debug Debug flag
*
* @return AbstractReportingCloud
*/
public function setTimeout(int $timeout): self
public function setDebug(bool $debug): self
{
$this->timeout = $timeout;
$this->debug = $debug;

return $this;
}

/**
* Return the debug flag
* Return the test flag
*
* @return bool|null
*/
public function getDebug(): ?bool
public function getTest(): ?bool
{
return $this->debug;
return $this->test;
}

/**
* Set the debug flag
* Set the test flag
*
* @param bool $debug Debug flag
* @param bool $test
*
* @return AbstractReportingCloud
*/
public function setDebug(bool $debug): self
public function setTest(bool $test): self
{
$this->debug = $debug;
$this->test = $test;

return $this;
}

/**
* Return the test flag
* Get the timeout (in seconds) of the backend web service
*
* @return bool|null
* @return int|null
*/
public function getTest(): ?bool
public function getTimeout(): ?int
{
return $this->test;
return $this->timeout;
}

/**
* Set the test flag
* Set the timeout (in seconds) of the backend web service
*
* @param bool $test
* @param int $timeout
*
* @return AbstractReportingCloud
*/
public function setTest(bool $test): self
public function setTimeout(int $timeout): self
{
$this->test = $test;
$this->timeout = $timeout;

return $this;
}
Expand Down Expand Up @@ -537,6 +537,69 @@ public function setClient(Client $client): self
return $this;
}

/**
* Assign default values to option properties
*
* @return ReportingCloud
*/
protected function setDefaultOptions(): self
{
if (null === $this->getBaseUri()) {
$baseUri = $this->getBaseUriFromEnv() ?? self::DEFAULT_BASE_URI;
$this->setBaseUri($baseUri);
}

if (null === $this->getDebug()) {
$this->setDebug(self::DEFAULT_DEBUG);
}

if (null === $this->getTest()) {
$this->setTest(self::DEFAULT_TEST);
}

if (null === $this->getTimeout()) {
$this->setTimeout(self::DEFAULT_TIMEOUT);
}

if (null === $this->getVersion()) {
$this->setVersion(self::DEFAULT_VERSION);
}

return $this;
}

/**
* Return the base URI from the environment variable "REPORTING_CLOUD_BASE_URI",
* checking that the hostname and sub-domain match the known hostname and sub-domain.
*
* Return null, if the environment variable has not been set or is empty.
*
* @throws InvalidArgumentException
* @return string|null
*/
protected function getBaseUriFromEnv(): ?string
{
$envVarName = 'REPORTING_CLOUD_BASE_URI';

$baseUri = (string) getenv($envVarName);
$baseUri = trim($baseUri);

if (empty($baseUri)) {
return null;
}

$sdkHost = (string) parse_url(self::DEFAULT_BASE_URI, PHP_URL_HOST);
$envHost = (string) parse_url($baseUri, PHP_URL_HOST);

if (!StringUtils::endsWith($envHost, $sdkHost)) {
$format = 'Base URI from environment variable "%s" with value "%s" does not end in "%s"';
$message = sprintf($format, $envVarName, $baseUri, $sdkHost);
throw new InvalidArgumentException($message);
}

return $baseUri;
}

/**
* Request the URI with options
*
Expand Down Expand Up @@ -611,37 +674,5 @@ private function getAuthorizationHeader(): string
throw new InvalidArgumentException($message);
}

/**
* Return the base URI from the environment variable "REPORTING_CLOUD_BASE_URI",
* checking that the hostname and sub-domain match the known hostname and sub-domain.
*
* Return null, if the environment variable has not been set or is empty.
*
* @throws InvalidArgumentException
* @return string|null
*/
protected function getBaseUriFromEnv(): ?string
{
$envVarName = 'REPORTING_CLOUD_BASE_URI';

$baseUri = (string) getenv($envVarName);
$baseUri = trim($baseUri);

if (empty($baseUri)) {
return null;
}

$sdkHost = (string) parse_url(self::DEFAULT_BASE_URI, PHP_URL_HOST);
$envHost = (string) parse_url($baseUri, PHP_URL_HOST);

if (!StringUtils::endsWith($envHost, $sdkHost)) {
$format = 'Base URI from environment variable "%s" with value "%s" does not end in "%s"';
$message = sprintf($format, $envVarName, $baseUri, $sdkHost);
throw new InvalidArgumentException($message);
}

return $baseUri;
}

// </editor-fold>
}
35 changes: 2 additions & 33 deletions src/ReportingCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(?array $options = null)
// Credentials (deprecated, use 'api_key' only)
'username' => 'setUsername',
'password' => 'setPassword',
// Settings
// Options
'base_uri' => 'setBaseUri',
'debug' => 'setDebug',
'test' => 'setTest',
Expand All @@ -60,38 +60,7 @@ public function __construct(?array $options = null)
}
}

$this->setDefaultSettings();
}

/**
* Assign default values to settings properties
*
* @return ReportingCloud
*/
private function setDefaultSettings(): self
{
if (null === $this->getBaseUri()) {
$baseUri = $this->getBaseUriFromEnv() ?? self::DEFAULT_BASE_URI;
$this->setBaseUri($baseUri);
}

if (null === $this->getDebug()) {
$this->setDebug(self::DEFAULT_DEBUG);
}

if (null === $this->getTest()) {
$this->setTest(self::DEFAULT_TEST);
}

if (null === $this->getTimeout()) {
$this->setTimeout(self::DEFAULT_TIMEOUT);
}

if (null === $this->getVersion()) {
$this->setVersion(self::DEFAULT_VERSION);
}

return $this;
$this->setDefaultOptions();
}

// </editor-fold>
Expand Down

0 comments on commit 785da68

Please sign in to comment.