Skip to content

Commit 1d1bdd9

Browse files
committed
prevent the builder from creating invalid clients
1 parent 1cfd360 commit 1d1bdd9

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ CHANGELOG
44
0.3.0
55
-----
66

7+
* Fixed creating `XApiClient` instances in an invalid state. The `XApiClientBuilder`
8+
now throws a `\LogicException` when the `build()` method is called before
9+
a base URI was configured.
10+
711
* Removed the `ApiClient` class. The `$requestHandler` and `$version` attributes
812
have been moved to the former child classes of the `ApiClient` class and
913
their visibility has been changed to `private`.

spec/XApiClientBuilderSpec.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ function it_is_an_xapi_client_builder()
1313

1414
function it_creates_an_xapi_client()
1515
{
16+
$this->setBaseUrl('http://example.com/xapi/');
1617
$this->build()->shouldHaveType('Xabbuh\XApi\Client\XApiClientInterface');
1718
}
19+
20+
function it_throws_an_exception_if_the_base_uri_is_not_configured()
21+
{
22+
$this->shouldThrow('\LogicException')->during('build');
23+
}
1824
}

src/XApiClientBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $toke
8989
*/
9090
public function build()
9191
{
92+
if (null === $this->baseUrl) {
93+
throw new \LogicException('Base URI value was not configured.');
94+
}
95+
9296
$httpClient = new Client($this->baseUrl);
9397

9498
if (is_array($this->oAuthCredentials)) {

src/XApiClientBuilderInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $toke
6262
* Builds the xAPI client.
6363
*
6464
* @return XApiClientInterface The xAPI client
65+
*
66+
* @throws \LogicException if no base URI was configured
6567
*/
6668
public function build();
6769
}

0 commit comments

Comments
 (0)