Skip to content

Commit

Permalink
LCH-6790: Porting maintenance mode changes to 2.x. (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
narendradesai authored Dec 5, 2023
1 parent b3879ae commit 268b17a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/ContentHubClientCommonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@
*/
trait ContentHubClientCommonTrait {

/**
* The last call's response object.
*
* @var \Psr\Http\Message\ResponseInterface
*/
private $response;

/**
* {@inheritdoc}
*/
public function request(string $method, $uri, array $options = []): ResponseInterface {
try {
return $this->httpClient->request($method, $uri, $options);
$response = $this->httpClient->request($method, $uri, $options);
}
catch (\Exception $e) {
return $this->getExceptionResponse($method, $uri, $e);
$response = $this->getExceptionResponse($method, $uri, $e);
}
$this->response = $response;
return $response;
}

/**
Expand Down Expand Up @@ -76,6 +85,18 @@ public function getConfig(?string $option = NULL) {
: ($this->config[$option] ?? NULL);
}

/**
* Returns the response object from the last call.
*
* In case further examination needed e.g. status code or error message.
*
* @return \Psr\Http\Message\ResponseInterface
* The response object.
*/
public function getResponse(): ResponseInterface {
return $this->response;
}

}
}
else {
Expand All @@ -84,16 +105,37 @@ public function getConfig(?string $option = NULL) {
*/
trait ContentHubClientCommonTrait {

/**
* The last call's response object.
*
* @var \Psr\Http\Message\ResponseInterface
*/
private $response;

/**
* {@inheritdoc}
*/
public function request($method, $uri, array $options = []) {
try {
return $this->httpClient->request($method, $uri, $options);
$response = $this->httpClient->request($method, $uri, $options);
}
catch (\Exception $e) {
return $this->getExceptionResponse($method, $uri, $e);
$response = $this->getExceptionResponse($method, $uri, $e);
}
$this->response = $response;
return $response;
}

/**
* Returns the response object from the last call.
*
* In case further examination needed e.g. status code or error message.
*
* @return \Psr\Http\Message\ResponseInterface
* The response object.
*/
public function getResponse(): ResponseInterface {
return $this->response;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/StatusCodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Acquia\ContentHubClient;

/**
* Contains status codes returned by the service.
*/
final class StatusCodes {

/**
* Returned when the service is intentionally configured to be in maintenance.
*/
public const SERVICE_UNDER_MAINTENANCE = 5031;

}
31 changes: 31 additions & 0 deletions test/ContentHubClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Acquia\ContentHubClient\ObjectFactory;
use Acquia\ContentHubClient\SearchCriteria\SearchCriteria;
use Acquia\ContentHubClient\Settings;
use Acquia\ContentHubClient\StatusCodes;
use Acquia\ContentHubClient\Webhook;
use Acquia\Hmac\Guzzle\HmacAuthMiddleware;
use Acquia\Hmac\Key;
Expand Down Expand Up @@ -2640,4 +2641,34 @@ public function testCacheRemoteSettings(): void {
$this->assertSame($response1, $actual);
}

/**
* @covers ::getResponse
*/
public function testGetResponse(): void {
$resp = new Response(SymfonyResponse::HTTP_OK, [], json_encode(['version' => 'version_number']));
$this->guzzle_client
->shouldReceive('get')
->once()
->with('ping')
->andReturn($resp);

$actual = $this->ch_client->ping();
$this->assertSame($resp, $actual);

$resp = new Response(SymfonyResponse::HTTP_SERVICE_UNAVAILABLE, [], json_encode([
'error' => [
'code' => StatusCodes::SERVICE_UNDER_MAINTENANCE,
'message' => 'service under maintenance',
],
]));
$this->guzzle_client
->shouldReceive('get')
->once()
->with('ping')
->andReturn($resp);

$actual = $this->ch_client->ping();
$this->assertSame($resp, $actual, 'Subsequent call returns the new response object.');
}

}

0 comments on commit 268b17a

Please sign in to comment.