-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathResult.php
More file actions
145 lines (125 loc) · 3.72 KB
/
Result.php
File metadata and controls
145 lines (125 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace AsyncAws\Core;
use AsyncAws\Core\Exception\Http\HttpException;
use AsyncAws\Core\Exception\Http\NetworkException;
/**
* Base class for all return values from a Api Client methods.
* Example: `FooClient::bar(): Result`.
*/
class Result
{
/**
* @var AbstractApi|null
*/
protected $awsClient;
/**
* Input used to build the API request that generate this Result.
*
* @var object|null
*/
protected $input;
/**
* @var bool
*/
private $initialized = false;
/**
* @var Response
*/
private $response;
/**
* @var self[]
*/
private $prefetchResults = [];
public function __construct(Response $response, ?AbstractApi $awsClient = null, ?object $request = null)
{
$this->response = $response;
$this->awsClient = $awsClient;
$this->input = $request;
}
public function __destruct()
{
while (!empty($this->prefetchResults)) {
array_shift($this->prefetchResults)->cancel();
}
}
/**
* Make sure the actual request is executed.
*
* @param float|null $timeout Duration in seconds before aborting. When null wait until the end of execution.
*
* @return bool whether the request is executed or not
*
* @throws NetworkException
* @throws HttpException
*/
final public function resolve(?float $timeout = null): bool
{
return $this->response->resolve($timeout);
}
/**
* Make sure all provided requests are executed.
* This only work if the http responses are produced by the same HTTP client.
* See https://symfony.com/doc/current/components/http_client.html#multiplexing-responses.
*
* @param self[] $results
* @param float|null $timeout Duration in seconds before aborting. When null wait
* until the end of execution. Using 0 means non-blocking
* @param bool $downloadBody Wait until receiving the entire response body or only the first bytes
*
* @return iterable<self>
*
* @throws NetworkException
* @throws HttpException
*/
final public static function wait(iterable $results, ?float $timeout = null, bool $downloadBody = false): iterable
{
$resultMap = [];
$responses = [];
foreach ($results as $index => $result) {
$responses[$index] = $result->response;
$resultMap[$index] = $result;
}
foreach (Response::wait($responses, $timeout, $downloadBody) as $index => $response) {
yield $index => $resultMap[$index];
}
}
/**
* Returns info on the current request.
*
* @return array{
* resolved: bool,
* body_downloaded: bool,
* response: \Symfony\Contracts\HttpClient\ResponseInterface,
* status: int,
* }
*/
final public function info(): array
{
return $this->response->info();
}
final public function cancel(): void
{
$this->response->cancel();
}
final protected function registerPrefetch(self $result): void
{
$this->prefetchResults[spl_object_id($result)] = $result;
}
final protected function unregisterPrefetch(self $result): void
{
unset($this->prefetchResults[spl_object_id($result)]);
}
final protected function initialize(): void
{
if ($this->initialized) {
return;
}
$this->resolve();
$this->initialized = true;
$this->populateResult($this->response);
}
protected function populateResult(Response $response): void
{
}
}