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

Commit aa28aa2

Browse files
committed
Moved validation of ENV base URI to Assert component
1 parent 281b0d0 commit aa28aa2

File tree

7 files changed

+183
-52
lines changed

7 files changed

+183
-52
lines changed

src/AbstractReportingCloud.php

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
use GuzzleHttp\Exception\TransferException;
1919
use GuzzleHttp\RequestOptions;
2020
use Psr\Http\Message\ResponseInterface;
21+
use TxTextControl\ReportingCloud\Assert\Assert;
2122
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
2223
use TxTextControl\ReportingCloud\Exception\RuntimeException;
2324
use TxTextControl\ReportingCloud\Filter\Filter;
2425
use TxTextControl\ReportingCloud\Stdlib\ConsoleUtils;
25-
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
2626

2727
/**
2828
* Abstract ReportingCloud
@@ -60,35 +60,35 @@ abstract class AbstractReportingCloud
6060
*
6161
* @const DEFAULT_BASE_URI
6262
*/
63-
protected const DEFAULT_BASE_URI = 'https://api.reporting.cloud';
63+
public const DEFAULT_BASE_URI = 'https://api.reporting.cloud';
6464

6565
/**
66-
* Default version string of backend
66+
* Default debug flag of REST client
6767
*
68-
* @const DEFAULT_VERSION
68+
* @const DEFAULT_DEBUG
6969
*/
70-
protected const DEFAULT_VERSION = 'v1';
70+
protected const DEFAULT_DEBUG = false;
7171

7272
/**
73-
* Default timeout of backend in seconds
73+
* Default test flag of backend
7474
*
75-
* @const DEFAULT_TIMEOUT
75+
* @const DEFAULT_TEST
7676
*/
77-
protected const DEFAULT_TIMEOUT = 120;
77+
protected const DEFAULT_TEST = false;
7878

7979
/**
80-
* Default test flag of backend
80+
* Default timeout of backend in seconds
8181
*
82-
* @const DEFAULT_TEST
82+
* @const DEFAULT_TIMEOUT
8383
*/
84-
protected const DEFAULT_TEST = false;
84+
protected const DEFAULT_TIMEOUT = 120;
8585

8686
/**
87-
* Default debug flag of REST client
87+
* Default version string of backend
8888
*
89-
* @const DEFAULT_DEBUG
89+
* @const DEFAULT_VERSION
9090
*/
91-
protected const DEFAULT_DEBUG = false;
91+
protected const DEFAULT_VERSION = 'v1';
9292

9393
// </editor-fold>
9494

@@ -506,7 +506,7 @@ public function getClient(): ?Client
506506
if (!$this->client instanceof Client) {
507507

508508
$headers = [
509-
'Authorization' => $this->getAuthorizationHeader()
509+
'Authorization' => $this->getAuthorizationHeader(),
510510
];
511511

512512
$options = [
@@ -546,7 +546,8 @@ public function setClient(Client $client): self
546546
protected function setDefaultOptions(): self
547547
{
548548
if (null === $this->getBaseUri()) {
549-
$baseUri = $this->getBaseUriFromConstOrEnvVar() ?? self::DEFAULT_BASE_URI;
549+
$baseUri = ConsoleUtils::baseUri() ?? self::DEFAULT_BASE_URI;
550+
Assert::assertBaseUri($baseUri);
550551
$this->setBaseUri($baseUri);
551552
}
552553

@@ -569,35 +570,6 @@ protected function setDefaultOptions(): self
569570
return $this;
570571
}
571572

572-
/**
573-
* Return the base URI from the PHP const or environment variable "REPORTING_CLOUD_BASE_URI",
574-
* checking that the hostname and sub-domain match the known hostname and sub-domain.
575-
*
576-
* Return null, if the environment variable has not been set or is empty.
577-
*
578-
* @throws InvalidArgumentException
579-
* @return string|null
580-
*/
581-
protected function getBaseUriFromConstOrEnvVar(): ?string
582-
{
583-
$baseUri = ConsoleUtils::baseUri();
584-
585-
if (empty($baseUri)) {
586-
return null;
587-
}
588-
589-
$sdkHost = (string) parse_url(self::DEFAULT_BASE_URI, PHP_URL_HOST);
590-
$envHost = (string) parse_url($baseUri, PHP_URL_HOST);
591-
592-
if (!StringUtils::endsWith($envHost, $sdkHost)) {
593-
$format = 'Base URI from environment variable "%s" with value "%s" does not end in "%s"';
594-
$message = sprintf($format, ConsoleUtils::BASE_URI, $baseUri, $sdkHost);
595-
throw new InvalidArgumentException($message);
596-
}
597-
598-
return $baseUri;
599-
}
600-
601573
/**
602574
* Request the URI with options
603575
*

src/Assert/Assert.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Assert extends AbstractAssert
2525
use AssertApiKeyTrait;
2626
use AssertArrayTrait;
2727
use AssertBase64DataTrait;
28+
use AssertBaseUriTrait;
2829
use AssertBooleanTrait;
2930
use AssertCultureTrait;
3031
use AssertDateTimeTrait;

src/Assert/AssertBaseUriTrait.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* ReportingCloud PHP SDK
6+
*
7+
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8+
*
9+
* @link https://www.reporting.cloud to learn more about ReportingCloud
10+
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11+
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12+
* @copyright © 2019 Text Control GmbH
13+
*/
14+
15+
namespace TxTextControl\ReportingCloud\Assert;
16+
17+
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
18+
use TxTextControl\ReportingCloud\ReportingCloud;
19+
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
20+
21+
/**
22+
* Trait AssertBaseUriTrait
23+
*
24+
* @package TxTextControl\ReportingCloud
25+
* @author Jonathan Maron (@JonathanMaron)
26+
*/
27+
trait AssertBaseUriTrait
28+
{
29+
/**
30+
* @param mixed $value
31+
*
32+
* @return string
33+
*/
34+
abstract protected static function valueToString($value): string;
35+
36+
/**
37+
* Check value is a known base URI
38+
*
39+
* @param mixed $value
40+
* @param string $message
41+
*/
42+
public static function assertBaseUri($value, string $message = ''): void
43+
{
44+
$baseUri = ReportingCloud::DEFAULT_BASE_URI;
45+
46+
$host1 = (string) parse_url($baseUri, PHP_URL_HOST);
47+
$host2 = (string) parse_url($value, PHP_URL_HOST);
48+
49+
if (!StringUtils::endsWith($host2, $host1)) {
50+
$format = $message ?: 'Expected base URI to end in %2$s. Got %1$s';
51+
$message = sprintf($format, self::valueToString($value), self::valueToString($host1));
52+
throw new InvalidArgumentException($message);
53+
}
54+
}
55+
}

src/Stdlib/ConsoleUtils.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,57 @@ public static function writeLn(string $format = '', ...$args): void
161161
*
162162
* @return string|null
163163
*/
164-
protected static function getValueFromConstOrEnvVar($key): ?string
164+
private static function getValueFromConstOrEnvVar($key): ?string
165+
{
166+
$ret = self::getValueFromConst($key);
167+
168+
if (null !== $ret) {
169+
return $ret;
170+
}
171+
172+
$ret = self::getValueFromEnvVar($key);
173+
174+
if (null !== $ret) {
175+
return $ret;
176+
}
177+
178+
return null;
179+
}
180+
181+
/**
182+
* Return a value from a PHP constant
183+
*
184+
* @param $key
185+
*
186+
* @return string|null
187+
*/
188+
private static function getValueFromConst($key): ?string
165189
{
166190
if (defined($key)) {
167191
$ret = (string) constant($key);
168192
$ret = trim($ret);
169-
170-
return $ret;
193+
if (!empty($ret)) {
194+
return $ret;
195+
}
171196
}
172197

198+
return null;
199+
}
200+
/**
201+
* Return a value from an environmental variable
202+
*
203+
* @param $key
204+
*
205+
* @return string|null
206+
*/
207+
private static function getValueFromEnvVar($key): ?string
208+
{
173209
if (getenv($key)) {
174210
$ret = (string) getenv($key);
175211
$ret = trim($ret);
176-
177-
return $ret;
212+
if (!empty($ret)) {
213+
return $ret;
214+
}
178215
}
179216

180217
return null;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* ReportingCloud PHP SDK
6+
*
7+
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8+
*
9+
* @link https://www.reporting.cloud to learn more about ReportingCloud
10+
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11+
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12+
* @copyright © 2019 Text Control GmbH
13+
*/
14+
15+
namespace TxTextControlTest\ReportingCloud\Assert;
16+
17+
use TxTextControl\ReportingCloud\Assert\Assert;
18+
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
19+
20+
/**
21+
* Trait AssertBaseUriTestTrait
22+
*
23+
* @package TxTextControlTest\ReportingCloud
24+
* @author Jonathan Maron (@JonathanMaron)
25+
*/
26+
trait AssertBaseUriTestTrait
27+
{
28+
// <editor-fold desc="Abstract methods">
29+
30+
/**
31+
* @param mixed $condition
32+
* @param string $message
33+
*/
34+
abstract public static function assertTrue($condition, string $message = ''): void;
35+
36+
// </editor-fold>
37+
38+
public function testAssertBaseUri(): void
39+
{
40+
Assert::assertBaseUri('https://phpunit-api.reporting.cloud');
41+
42+
$this->assertTrue(true);
43+
}
44+
45+
/**
46+
* @expectedException InvalidArgumentException
47+
* @expectedExceptionMessage Expected base URI to end in "api.reporting.cloud". Got "https://api.example.com"
48+
*/
49+
public function testAssertBaseUriWithInvalidBaseUri(): void
50+
{
51+
Assert::assertBaseUri('https://api.example.com');
52+
53+
$this->assertTrue(true);
54+
}
55+
56+
/**
57+
* @expectedException InvalidArgumentException
58+
* @expectedExceptionMessage Expected base URI to end in "api.reporting.cloud". Got "https://api.reporting.cloud.de"
59+
*/
60+
public function testAssertBaseUriInvalidBaseUriKnownHost(): void
61+
{
62+
Assert::assertBaseUri('https://api.reporting.cloud.de');
63+
64+
$this->assertTrue(true);
65+
}
66+
}

test/Assert/AssertTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AssertTest extends TestCase
2828
{
2929
use AssertApiKeyTestTrait;
3030
use AssertBase64DataTestTrait;
31+
use AssertBaseUriTestTrait;
3132
use AssertCultureTestTrait;
3233
use AssertDateTimeTestTrait;
3334
use AssertDocumentDividerTestTrait;

test/ReportingCloudTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ public function testGetBaseUriFromEnvVarWithInvalidValue(): void
197197
$reportingCloud = new ReportingCloud();
198198
} catch (InvalidArgumentException $e) {
199199
putenv("{$envVarName}={$baseUri}");
200-
$expected = 'Base URI from environment variable "REPORTING_CLOUD_BASE_URI" with value ';
201-
$expected .= '"https://www.example.com" does not end in "api.reporting.cloud"';
200+
$expected = 'Expected base URI to end in "api.reporting.cloud". Got "https://www.example.com"';
202201
$this->assertSame($expected, $e->getMessage());
203202
}
204203
unset($reportingCloud);

0 commit comments

Comments
 (0)