diff --git a/src/Api/ContactsApi.php b/src/Api/ContactsApi.php index 5f9b29a..fe1179d 100644 --- a/src/Api/ContactsApi.php +++ b/src/Api/ContactsApi.php @@ -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 { @@ -110,7 +111,7 @@ public function update( ?string $state = null, ?string $fax = null, ?array $disclosedFields = null - ): void { + ): RealtimeRegisterResponse { $payload = [ 'customer' => $customer, 'handle' => $handle, @@ -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); } /** diff --git a/src/Domain/AcmeSubscriptionResponse.php b/src/Domain/AcmeSubscriptionResponse.php index 9fa572c..0f46ef0 100644 --- a/src/Domain/AcmeSubscriptionResponse.php +++ b/src/Domain/AcmeSubscriptionResponse.php @@ -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'] diff --git a/src/Support/AuthorizedClient.php b/src/Support/AuthorizedClient.php index cd4e81d..a9e1b4e 100644 --- a/src/Support/AuthorizedClient.php +++ b/src/Support/AuthorizedClient.php @@ -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) { diff --git a/src/Support/RealtimeRegisterResponse.php b/src/Support/RealtimeRegisterResponse.php index d9f2048..86750e5 100644 --- a/src/Support/RealtimeRegisterResponse.php +++ b/src/Support/RealtimeRegisterResponse.php @@ -10,10 +10,13 @@ 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 @@ -21,9 +24,9 @@ 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 @@ -46,4 +49,9 @@ public function headers(): array { return $this->headers; } + + public function resultCode(): int + { + return $this->resultCode; + } } diff --git a/tests/ResponseObjectTest.php b/tests/ResponseObjectTest.php index 3ecc44d..05744d9 100644 --- a/tests/ResponseObjectTest.php +++ b/tests/ResponseObjectTest.php @@ -11,7 +11,7 @@ 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()); @@ -19,14 +19,14 @@ public function test_get_text(): void 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()); @@ -34,7 +34,7 @@ public function test_parse_json(): void 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();