Skip to content

Commit 268b17a

Browse files
LCH-6790: Porting maintenance mode changes to 2.x. (#191)
1 parent b3879ae commit 268b17a

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

src/ContentHubClientCommonTrait.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@
1313
*/
1414
trait ContentHubClientCommonTrait {
1515

16+
/**
17+
* The last call's response object.
18+
*
19+
* @var \Psr\Http\Message\ResponseInterface
20+
*/
21+
private $response;
22+
1623
/**
1724
* {@inheritdoc}
1825
*/
1926
public function request(string $method, $uri, array $options = []): ResponseInterface {
2027
try {
21-
return $this->httpClient->request($method, $uri, $options);
28+
$response = $this->httpClient->request($method, $uri, $options);
2229
}
2330
catch (\Exception $e) {
24-
return $this->getExceptionResponse($method, $uri, $e);
31+
$response = $this->getExceptionResponse($method, $uri, $e);
2532
}
33+
$this->response = $response;
34+
return $response;
2635
}
2736

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

88+
/**
89+
* Returns the response object from the last call.
90+
*
91+
* In case further examination needed e.g. status code or error message.
92+
*
93+
* @return \Psr\Http\Message\ResponseInterface
94+
* The response object.
95+
*/
96+
public function getResponse(): ResponseInterface {
97+
return $this->response;
98+
}
99+
79100
}
80101
}
81102
else {
@@ -84,16 +105,37 @@ public function getConfig(?string $option = NULL) {
84105
*/
85106
trait ContentHubClientCommonTrait {
86107

108+
/**
109+
* The last call's response object.
110+
*
111+
* @var \Psr\Http\Message\ResponseInterface
112+
*/
113+
private $response;
114+
87115
/**
88116
* {@inheritdoc}
89117
*/
90118
public function request($method, $uri, array $options = []) {
91119
try {
92-
return $this->httpClient->request($method, $uri, $options);
120+
$response = $this->httpClient->request($method, $uri, $options);
93121
}
94122
catch (\Exception $e) {
95-
return $this->getExceptionResponse($method, $uri, $e);
123+
$response = $this->getExceptionResponse($method, $uri, $e);
96124
}
125+
$this->response = $response;
126+
return $response;
127+
}
128+
129+
/**
130+
* Returns the response object from the last call.
131+
*
132+
* In case further examination needed e.g. status code or error message.
133+
*
134+
* @return \Psr\Http\Message\ResponseInterface
135+
* The response object.
136+
*/
137+
public function getResponse(): ResponseInterface {
138+
return $this->response;
97139
}
98140

99141
/**

src/StatusCodes.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Acquia\ContentHubClient;
4+
5+
/**
6+
* Contains status codes returned by the service.
7+
*/
8+
final class StatusCodes {
9+
10+
/**
11+
* Returned when the service is intentionally configured to be in maintenance.
12+
*/
13+
public const SERVICE_UNDER_MAINTENANCE = 5031;
14+
15+
}

test/ContentHubClientTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Acquia\ContentHubClient\ObjectFactory;
1111
use Acquia\ContentHubClient\SearchCriteria\SearchCriteria;
1212
use Acquia\ContentHubClient\Settings;
13+
use Acquia\ContentHubClient\StatusCodes;
1314
use Acquia\ContentHubClient\Webhook;
1415
use Acquia\Hmac\Guzzle\HmacAuthMiddleware;
1516
use Acquia\Hmac\Key;
@@ -2640,4 +2641,34 @@ public function testCacheRemoteSettings(): void {
26402641
$this->assertSame($response1, $actual);
26412642
}
26422643

2644+
/**
2645+
* @covers ::getResponse
2646+
*/
2647+
public function testGetResponse(): void {
2648+
$resp = new Response(SymfonyResponse::HTTP_OK, [], json_encode(['version' => 'version_number']));
2649+
$this->guzzle_client
2650+
->shouldReceive('get')
2651+
->once()
2652+
->with('ping')
2653+
->andReturn($resp);
2654+
2655+
$actual = $this->ch_client->ping();
2656+
$this->assertSame($resp, $actual);
2657+
2658+
$resp = new Response(SymfonyResponse::HTTP_SERVICE_UNAVAILABLE, [], json_encode([
2659+
'error' => [
2660+
'code' => StatusCodes::SERVICE_UNDER_MAINTENANCE,
2661+
'message' => 'service under maintenance',
2662+
],
2663+
]));
2664+
$this->guzzle_client
2665+
->shouldReceive('get')
2666+
->once()
2667+
->with('ping')
2668+
->andReturn($resp);
2669+
2670+
$actual = $this->ch_client->ping();
2671+
$this->assertSame($resp, $actual, 'Subsequent call returns the new response object.');
2672+
}
2673+
26432674
}

0 commit comments

Comments
 (0)