diff --git a/src/AbstractReportingCloud.php b/src/AbstractReportingCloud.php index 389a0907..a17e838f 100644 --- a/src/AbstractReportingCloud.php +++ b/src/AbstractReportingCloud.php @@ -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; /** @@ -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); } @@ -569,7 +570,7 @@ 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. @@ -577,12 +578,9 @@ protected function setDefaultOptions(): self * @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; @@ -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); } diff --git a/src/Stdlib/ConsoleUtils.php b/src/Stdlib/ConsoleUtils.php index dd72f9ba..a3d574bd 100644 --- a/src/Stdlib/ConsoleUtils.php +++ b/src/Stdlib/ConsoleUtils.php @@ -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 * @@ -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); } /** @@ -74,7 +77,7 @@ public static function apiKey(): ?string */ public static function errorMessage(): string { - $format = <<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 { @@ -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()); @@ -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}"); @@ -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}="); @@ -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");