Skip to content

Commit 64e6e8a

Browse files
authored
Minor cleanup, adding comments and renaming properties (#353)
* Minor cleanup * Added docs * Added some more comments * Comments * cs
1 parent 9168aad commit 64e6e8a

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

src/AbstractApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Symfony\Contracts\HttpClient\HttpClientInterface;
2222

2323
/**
24-
* Base class most APIs are inheriting.
24+
* Base class all API clients are inheriting.
2525
*
2626
* @author Tobias Nyholm <[email protected]>
2727
* @author Jérémy Derussé <[email protected]>

src/Response.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@
2525
*/
2626
class Response
2727
{
28-
private $response;
28+
private $httpResponse;
2929

3030
private $httpClient;
3131

3232
/**
3333
* A Result can be resolved many times. This variable contains the last resolve result.
34+
* Null means that the result has never been resolved.
3435
*
3536
* @var bool|NetworkException|HttpException|null
3637
*/
3738
private $resolveResult;
3839

3940
public function __construct(ResponseInterface $response, HttpClientInterface $httpClient)
4041
{
41-
$this->response = $response;
42+
$this->httpResponse = $response;
4243
$this->httpClient = $httpClient;
4344
}
4445

@@ -52,7 +53,8 @@ public function __destruct()
5253
/**
5354
* Make sure the actual request is executed.
5455
*
55-
* @param float|null $timeout Duration in seconds before aborting. When null wait until the end of execution.
56+
* @param float|null $timeout Duration in seconds before aborting. When null wait
57+
* until the end of execution. Using 0 means non-blocking
5658
*
5759
* @return bool whether the request is executed or not
5860
*
@@ -74,7 +76,7 @@ public function resolve(?float $timeout = null): bool
7476
}
7577

7678
try {
77-
foreach ($this->httpClient->stream($this->response, $timeout) as $chunk) {
79+
foreach ($this->httpClient->stream($this->httpResponse, $timeout) as $chunk) {
7880
if ($chunk->isTimeout()) {
7981
return false;
8082
}
@@ -83,21 +85,21 @@ public function resolve(?float $timeout = null): bool
8385
}
8486
}
8587

86-
$statusCode = $this->response->getStatusCode();
88+
$statusCode = $this->httpResponse->getStatusCode();
8789
} catch (TransportExceptionInterface $e) {
8890
throw $this->resolveResult = new NetworkException('Could not contact remote server.', 0, $e);
8991
}
9092

9193
if (500 <= $statusCode) {
92-
throw $this->resolveResult = new ServerException($this->response);
94+
throw $this->resolveResult = new ServerException($this->httpResponse);
9395
}
9496

9597
if (400 <= $statusCode) {
96-
throw $this->resolveResult = new ClientException($this->response);
98+
throw $this->resolveResult = new ClientException($this->httpResponse);
9799
}
98100

99101
if (300 <= $statusCode) {
100-
throw $this->resolveResult = new RedirectionException($this->response);
102+
throw $this->resolveResult = new RedirectionException($this->httpResponse);
101103
}
102104

103105
return $this->resolveResult = true;
@@ -116,45 +118,45 @@ public function info(): array
116118
{
117119
return [
118120
'resolved' => null !== $this->resolveResult,
119-
'response' => $this->response,
120-
'status' => (int) $this->response->getInfo('http_code'),
121+
'response' => $this->httpResponse,
122+
'status' => (int) $this->httpResponse->getInfo('http_code'),
121123
];
122124
}
123125

124126
public function cancel(): void
125127
{
126-
$this->response->cancel();
128+
$this->httpResponse->cancel();
127129
$this->resolveResult = false;
128130
}
129131

130132
public function getHeaders(): array
131133
{
132134
$this->resolve();
133135

134-
return $this->response->getHeaders(false);
136+
return $this->httpResponse->getHeaders(false);
135137
}
136138

137139
public function getContent(): string
138140
{
139141
$this->resolve();
140142

141-
return $this->response->getContent(false);
143+
return $this->httpResponse->getContent(false);
142144
}
143145

144146
public function toArray(): array
145147
{
146148
$this->resolve();
147149

148-
return $this->response->toArray(false);
150+
return $this->httpResponse->toArray(false);
149151
}
150152

151153
public function getStatusCode(): int
152154
{
153-
return $this->response->getStatusCode();
155+
return $this->httpResponse->getStatusCode();
154156
}
155157

156158
public function toStream(): ResultStream
157159
{
158-
return new ResponseBodyStream($this->httpClient->stream($this->response));
160+
return new ResponseBodyStream($this->httpClient->stream($this->httpResponse));
159161
}
160162
}

src/Result.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ class Result
2626

2727
protected $initialized = false;
2828

29-
/**
30-
* @var Response
31-
*/
3229
private $response;
3330

3431
/**

src/Waiter.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ final public function getState(): string
122122
*
123123
* @param float|null $timeout Duration in seconds before aborting. When null wait until the end of execution.
124124
*
125+
* @return bool false on timeout. True if the response has returned with as status code.
126+
*
125127
* @throws NetworkException
126128
*/
127129
final public function resolve(?float $timeout = null): bool
@@ -159,6 +161,8 @@ final public function cancel(): void
159161
*
160162
* @param float $timeout Duration in seconds before aborting
161163
* @param float $delay Duration in seconds between each check
164+
*
165+
* @return bool true if a final state was reached
162166
*/
163167
final public function wait(float $timeout = null, float $delay = null): bool
164168
{
@@ -167,19 +171,21 @@ final public function wait(float $timeout = null, float $delay = null): bool
167171

168172
$start = \microtime(true);
169173
while (true) {
174+
// If request times out
170175
if (!$this->resolve($timeout - (\microtime(true) - $start))) {
171176
break;
172177
}
173178

174-
switch ($this->getState()) {
175-
case self::STATE_SUCCESS:
176-
case self::STATE_FAILURE:
177-
return true;
179+
// If we reached a final state
180+
if (\in_array($this->getState(), [self::STATE_SUCCESS, self::STATE_FAILURE])) {
181+
return true;
178182
}
179183

184+
// If the timeout will expire during our sleep, then exit early.
180185
if ($delay > $timeout - (\microtime(true) - $start)) {
181186
break;
182187
}
188+
183189
\usleep((int) ceil($delay * 1000000));
184190
$this->stealResponse($this->refreshState());
185191
}

0 commit comments

Comments
 (0)