Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Api/ContactsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use RealtimeRegister\Domain\ContactCollection;
use RealtimeRegister\Domain\Country;
use RealtimeRegister\Domain\CountryCollection;
use RealtimeRegister\Support\RealtimeRegisterResponse;

final class ContactsApi extends AbstractApi
{
Expand Down Expand Up @@ -110,7 +111,7 @@ public function update(
?string $state = null,
?string $fax = null,
?array $disclosedFields = null
): void {
): RealtimeRegisterResponse {
$payload = [
'customer' => $customer,
'handle' => $handle,
Expand Down Expand Up @@ -153,7 +154,7 @@ public function update(
$payload['disclosedFields'] = $disclosedFields;
}

$this->client->post(sprintf('v2/customers/%s/contacts/%s/update', urlencode($customer), urlencode($handle)), $payload);
return $this->client->post(sprintf('v2/customers/%s/contacts/%s/update', urlencode($customer), urlencode($handle)), $payload);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/AcmeSubscriptionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function toArray(): array
public static function fromArray(array $json): AcmeSubscriptionResponse
{
return new AcmeSubscriptionResponse(
$json['id'],
$json['id'] ?? null,
$json['directoryUrl'],
$json['accountKey'],
$json['hmacKey']
Expand Down
2 changes: 1 addition & 1 deletion src/Support/AuthorizedClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function handleResponse(ResponseInterface $response, ?int $expectedRespo

// Parse response
if ($this->isResponseValid($response, $expectedResponse)) {
return RealtimeRegisterResponse::fromString((string) $response->getBody(), $response->getHeaders());
return RealtimeRegisterResponse::fromString((string) $response->getBody(), $response->getHeaders(), $response->getStatusCode());
} elseif ($response->getStatusCode() === 400) {
throw new BadRequestException('Bad Request: ' . $response->getBody());
} elseif ($response->getStatusCode() === 401) {
Expand Down
14 changes: 11 additions & 3 deletions src/Support/RealtimeRegisterResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ class RealtimeRegisterResponse

private array $headers;

public function __construct(string $response, array $headers)
private int $resultCode;

public function __construct(string $response, array $headers, int $resultCode)
{
$this->response = $response;
$this->headers = $headers;
$this->resultCode = $resultCode;
}

public function __toString(): string
{
return $this->text();
}

public static function fromString(string $response, array $headers): RealtimeRegisterResponse
public static function fromString(string $response, array $headers, int $resultCode = -1): RealtimeRegisterResponse
{
return new RealtimeRegisterResponse($response, array_change_key_case($headers));
return new RealtimeRegisterResponse($response, array_change_key_case($headers), $resultCode);
}

public function json(): array
Expand All @@ -46,4 +49,9 @@ public function headers(): array
{
return $this->headers;
}

public function resultCode(): int
{
return $this->resultCode;
}
}
8 changes: 4 additions & 4 deletions tests/ResponseObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ class ResponseObjectTest extends TestCase
{
public function test_get_text(): void
{
$response = RealtimeRegisterResponse::fromString('This is text', ['foo' => 'bar']);
$response = RealtimeRegisterResponse::fromString('This is text', ['foo' => 'bar'], 200);

$this->assertSame('This is text', $response->text());
$this->assertSame(['foo' => 'bar'], $response->headers());
}

public function test_to_string(): void
{
$response = RealtimeRegisterResponse::fromString('This is text', ['foo' => 'bar']);
$response = RealtimeRegisterResponse::fromString('This is text', ['foo' => 'bar'], 200);

$this->assertSame('This is text', (string) $response);
}

public function test_parse_json(): void
{
$response = RealtimeRegisterResponse::fromString('{"foo": "bar"}', ['foo' => 'bar']);
$response = RealtimeRegisterResponse::fromString('{"foo": "bar"}', ['foo' => 'bar'], 200);

$this->assertSame(['foo' => 'bar'], $response->json());
$this->assertSame(['foo' => 'bar'], $response->headers());
}

public function test_invalid_json(): void
{
$response = RealtimeRegisterResponse::fromString('{"foo": <><>>>>><<<< {{{{{{{{{{{{) "bar"}', ['foo' => 'bar']);
$response = RealtimeRegisterResponse::fromString('{"foo": <><>>>>><<<< {{{{{{{{{{{{) "bar"}', ['foo' => 'bar'], 400);

$this->expectException(UnexpectedValueException::class);
$response->json();
Expand Down