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

Commit 281b0d0

Browse files
committed
Minor refactoring
1 parent 785da68 commit 281b0d0

File tree

3 files changed

+58
-30
lines changed

3 files changed

+58
-30
lines changed

src/AbstractReportingCloud.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
2222
use TxTextControl\ReportingCloud\Exception\RuntimeException;
2323
use TxTextControl\ReportingCloud\Filter\Filter;
24+
use TxTextControl\ReportingCloud\Stdlib\ConsoleUtils;
2425
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
2526

2627
/**
@@ -545,7 +546,7 @@ public function setClient(Client $client): self
545546
protected function setDefaultOptions(): self
546547
{
547548
if (null === $this->getBaseUri()) {
548-
$baseUri = $this->getBaseUriFromEnv() ?? self::DEFAULT_BASE_URI;
549+
$baseUri = $this->getBaseUriFromConstOrEnvVar() ?? self::DEFAULT_BASE_URI;
549550
$this->setBaseUri($baseUri);
550551
}
551552

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

571572
/**
572-
* Return the base URI from the environment variable "REPORTING_CLOUD_BASE_URI",
573+
* Return the base URI from the PHP const or environment variable "REPORTING_CLOUD_BASE_URI",
573574
* checking that the hostname and sub-domain match the known hostname and sub-domain.
574575
*
575576
* Return null, if the environment variable has not been set or is empty.
576577
*
577578
* @throws InvalidArgumentException
578579
* @return string|null
579580
*/
580-
protected function getBaseUriFromEnv(): ?string
581+
protected function getBaseUriFromConstOrEnvVar(): ?string
581582
{
582-
$envVarName = 'REPORTING_CLOUD_BASE_URI';
583-
584-
$baseUri = (string) getenv($envVarName);
585-
$baseUri = trim($baseUri);
583+
$baseUri = ConsoleUtils::baseUri();
586584

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

594592
if (!StringUtils::endsWith($envHost, $sdkHost)) {
595593
$format = 'Base URI from environment variable "%s" with value "%s" does not end in "%s"';
596-
$message = sprintf($format, $envVarName, $baseUri, $sdkHost);
594+
$message = sprintf($format, ConsoleUtils::BASE_URI, $baseUri, $sdkHost);
597595
throw new InvalidArgumentException($message);
598596
}
599597

src/Stdlib/ConsoleUtils.php

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class ConsoleUtils extends AbstractStdlib
2929
*/
3030
public const API_KEY = 'REPORTING_CLOUD_API_KEY';
3131

32+
/**
33+
* Name of PHP constant or environmental variable storing base URI
34+
*
35+
* @const REPORTING_CLOUD_BASE_URI
36+
*/
37+
public const BASE_URI = 'REPORTING_CLOUD_BASE_URI';
38+
3239
/**
3340
* Check that either the API key has been defined in environment variables
3441
*
@@ -50,21 +57,17 @@ public static function checkCredentials(): bool
5057
*/
5158
public static function apiKey(): ?string
5259
{
53-
$key = self::API_KEY;
54-
55-
if (defined($key)) {
56-
$ret = (string) constant($key);
57-
$ret = trim($ret);
58-
return $ret;
59-
}
60-
61-
if (getenv($key)) {
62-
$ret = (string) getenv($key);
63-
$ret = trim($ret);
64-
return $ret;
65-
}
60+
return self::getValueFromConstOrEnvVar(self::API_KEY);
61+
}
6662

67-
return null;
63+
/**
64+
* Return the ReportingCloud base URI from a PHP constant or environmental variable
65+
*
66+
* @return string|null
67+
*/
68+
public static function baseUri(): ?string
69+
{
70+
return self::getValueFromConstOrEnvVar(self::BASE_URI);
6871
}
6972

7073
/**
@@ -74,7 +77,7 @@ public static function apiKey(): ?string
7477
*/
7578
public static function errorMessage(): string
7679
{
77-
$format = <<<END
80+
$format = <<<END
7881
7982
Error: ReportingCloud API key not defined.
8083
@@ -150,4 +153,30 @@ public static function writeLn(string $format = '', ...$args): void
150153

151154
echo PHP_EOL;
152155
}
156+
157+
/**
158+
* Return a value from a PHP constant or environmental variable
159+
*
160+
* @param $key
161+
*
162+
* @return string|null
163+
*/
164+
protected static function getValueFromConstOrEnvVar($key): ?string
165+
{
166+
if (defined($key)) {
167+
$ret = (string) constant($key);
168+
$ret = trim($ret);
169+
170+
return $ret;
171+
}
172+
173+
if (getenv($key)) {
174+
$ret = (string) getenv($key);
175+
$ret = trim($ret);
176+
177+
return $ret;
178+
}
179+
180+
return null;
181+
}
153182
}

test/ReportingCloudTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
1818
use TxTextControl\ReportingCloud\ReportingCloud;
19+
use TxTextControl\ReportingCloud\Stdlib\ConsoleUtils;
1920

2021
/**
2122
* Class ReportingCloudTest
@@ -131,8 +132,8 @@ public function testDefaultProperties(): void
131132
$this->assertEmpty($reportingCloud->getUsername());
132133
$this->assertEmpty($reportingCloud->getPassword());
133134

134-
$envName = 'REPORTING_CLOUD_BASE_URI';
135-
$baseUri = getenv($envName);
135+
$envVarName = ConsoleUtils::BASE_URI;
136+
$baseUri = getenv($envVarName);
136137
if (is_string($baseUri) && !empty($baseUri)) {
137138
$expected = $baseUri;
138139
} else {
@@ -149,8 +150,8 @@ public function testDefaultProperties(): void
149150

150151
public function testGetBaseUriFromEnvVar(): void
151152
{
152-
$envVarName = 'REPORTING_CLOUD_BASE_URI';
153-
$baseUri = getenv($envVarName);
153+
$baseUri = ConsoleUtils::baseUri();
154+
154155
if (is_string($baseUri) && !empty($baseUri)) {
155156
$reportingCloud = new ReportingCloud();
156157
$this->assertSame($baseUri, $reportingCloud->getBaseUri());
@@ -160,7 +161,7 @@ public function testGetBaseUriFromEnvVar(): void
160161

161162
public function testGetBaseUriFromEnvVarWithNull(): void
162163
{
163-
$envVarName = 'REPORTING_CLOUD_BASE_URI';
164+
$envVarName = ConsoleUtils::BASE_URI;
164165
$baseUri = getenv($envVarName);
165166

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

175176
public function testGetBaseUriFromEnvVarWithEmptyValue(): void
176177
{
177-
$envVarName = 'REPORTING_CLOUD_BASE_URI';
178+
$envVarName = ConsoleUtils::BASE_URI;
178179
$baseUri = getenv($envVarName);
179180

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

189190
public function testGetBaseUriFromEnvVarWithInvalidValue(): void
190191
{
191-
$envVarName = 'REPORTING_CLOUD_BASE_URI';
192+
$envVarName = ConsoleUtils::BASE_URI;
192193
$baseUri = getenv($envVarName);
193194
if (is_string($baseUri) && !empty($baseUri)) {
194195
putenv("{$envVarName}=https://www.example.com");

0 commit comments

Comments
 (0)