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 785da68 commit 281b0d0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
14 changes: 6 additions & 8 deletions src/AbstractReportingCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
use TxTextControl\ReportingCloud\Exception\RuntimeException;
use TxTextControl\ReportingCloud\Filter\Filter;
use TxTextControl\ReportingCloud\Stdlib\ConsoleUtils;
use TxTextControl\ReportingCloud\Stdlib\StringUtils;

/**
Expand Down Expand Up @@ -545,7 +546,7 @@ public function setClient(Client $client): self
protected function setDefaultOptions(): self
{
if (null === $this->getBaseUri()) {
$baseUri = $this->getBaseUriFromEnv() ?? self::DEFAULT_BASE_URI;
$baseUri = $this->getBaseUriFromConstOrEnvVar() ?? self::DEFAULT_BASE_URI;
$this->setBaseUri($baseUri);
}

Expand All @@ -569,20 +570,17 @@ protected function setDefaultOptions(): self
}

/**
* Return the base URI from the environment variable "REPORTING_CLOUD_BASE_URI",
* Return the base URI from the PHP const or 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
protected function getBaseUriFromConstOrEnvVar(): ?string
{
$envVarName = 'REPORTING_CLOUD_BASE_URI';

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

if (empty($baseUri)) {
return null;
Expand All @@ -593,7 +591,7 @@ protected function getBaseUriFromEnv(): ?string

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);
$message = sprintf($format, ConsoleUtils::BASE_URI, $baseUri, $sdkHost);
throw new InvalidArgumentException($message);
}

Expand Down
59 changes: 44 additions & 15 deletions src/Stdlib/ConsoleUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class ConsoleUtils extends AbstractStdlib
*/
public const API_KEY = 'REPORTING_CLOUD_API_KEY';

/**
* Name of PHP constant or environmental variable storing base URI
*
* @const REPORTING_CLOUD_BASE_URI
*/
public const BASE_URI = 'REPORTING_CLOUD_BASE_URI';

/**
* Check that either the API key has been defined in environment variables
*
Expand All @@ -50,21 +57,17 @@ public static function checkCredentials(): bool
*/
public static function apiKey(): ?string
{
$key = self::API_KEY;

if (defined($key)) {
$ret = (string) constant($key);
$ret = trim($ret);
return $ret;
}

if (getenv($key)) {
$ret = (string) getenv($key);
$ret = trim($ret);
return $ret;
}
return self::getValueFromConstOrEnvVar(self::API_KEY);
}

return null;
/**
* Return the ReportingCloud base URI from a PHP constant or environmental variable
*
* @return string|null
*/
public static function baseUri(): ?string
{
return self::getValueFromConstOrEnvVar(self::BASE_URI);
}

/**
Expand All @@ -74,7 +77,7 @@ public static function apiKey(): ?string
*/
public static function errorMessage(): string
{
$format = <<<END
$format = <<<END
Error: ReportingCloud API key not defined.
Expand Down Expand Up @@ -150,4 +153,30 @@ public static function writeLn(string $format = '', ...$args): void

echo PHP_EOL;
}

/**
* Return a value from a PHP constant or environmental variable
*
* @param $key
*
* @return string|null
*/
protected static function getValueFromConstOrEnvVar($key): ?string
{
if (defined($key)) {
$ret = (string) constant($key);
$ret = trim($ret);

return $ret;
}

if (getenv($key)) {
$ret = (string) getenv($key);
$ret = trim($ret);

return $ret;
}

return null;
}
}
15 changes: 8 additions & 7 deletions test/ReportingCloudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
use TxTextControl\ReportingCloud\ReportingCloud;
use TxTextControl\ReportingCloud\Stdlib\ConsoleUtils;

/**
* Class ReportingCloudTest
Expand Down Expand Up @@ -131,8 +132,8 @@ public function testDefaultProperties(): void
$this->assertEmpty($reportingCloud->getUsername());
$this->assertEmpty($reportingCloud->getPassword());

$envName = 'REPORTING_CLOUD_BASE_URI';
$baseUri = getenv($envName);
$envVarName = ConsoleUtils::BASE_URI;
$baseUri = getenv($envVarName);
if (is_string($baseUri) && !empty($baseUri)) {
$expected = $baseUri;
} else {
Expand All @@ -149,8 +150,8 @@ public function testDefaultProperties(): void

public function testGetBaseUriFromEnvVar(): void
{
$envVarName = 'REPORTING_CLOUD_BASE_URI';
$baseUri = getenv($envVarName);
$baseUri = ConsoleUtils::baseUri();

if (is_string($baseUri) && !empty($baseUri)) {
$reportingCloud = new ReportingCloud();
$this->assertSame($baseUri, $reportingCloud->getBaseUri());
Expand All @@ -160,7 +161,7 @@ public function testGetBaseUriFromEnvVar(): void

public function testGetBaseUriFromEnvVarWithNull(): void
{
$envVarName = 'REPORTING_CLOUD_BASE_URI';
$envVarName = ConsoleUtils::BASE_URI;
$baseUri = getenv($envVarName);

putenv("{$envVarName}");
Expand All @@ -174,7 +175,7 @@ public function testGetBaseUriFromEnvVarWithNull(): void

public function testGetBaseUriFromEnvVarWithEmptyValue(): void
{
$envVarName = 'REPORTING_CLOUD_BASE_URI';
$envVarName = ConsoleUtils::BASE_URI;
$baseUri = getenv($envVarName);

putenv("{$envVarName}=");
Expand All @@ -188,7 +189,7 @@ public function testGetBaseUriFromEnvVarWithEmptyValue(): void

public function testGetBaseUriFromEnvVarWithInvalidValue(): void
{
$envVarName = 'REPORTING_CLOUD_BASE_URI';
$envVarName = ConsoleUtils::BASE_URI;
$baseUri = getenv($envVarName);
if (is_string($baseUri) && !empty($baseUri)) {
putenv("{$envVarName}=https://www.example.com");
Expand Down

0 comments on commit 281b0d0

Please sign in to comment.