Skip to content

Commit 85625cd

Browse files
committed
Refactor Client::runMulti method
1 parent 3bfd36f commit 85625cd

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/Client.php

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,39 +116,41 @@ public function runMulti(array $requests) : Generator
116116
do {
117117
$status = \curl_multi_exec($multiHandle, $stillRunning);
118118
$message = \curl_multi_info_read($multiHandle);
119-
if ($message) {
120-
foreach ($handles as $id => $handle) {
121-
if ($message['handle'] === $handle) {
122-
$info = [];
123-
if ($requests[$id]->isGettingInfo()) {
124-
$info = (array) \curl_getinfo($handle);
125-
}
126-
$objectId = \spl_object_id($handle);
127-
if (!isset($this->parsed[$objectId])) {
128-
yield $id => new ResponseError(
129-
$requests[$id],
130-
\curl_error($handle),
131-
\curl_errno($handle),
132-
$info
133-
);
134-
unset($handles[$id]);
135-
break;
136-
}
137-
yield $id => new Response(
138-
$requests[$id],
139-
$this->parsed[$objectId]['protocol'],
140-
$this->parsed[$objectId]['code'],
141-
$this->parsed[$objectId]['reason'],
142-
$this->parsed[$objectId]['headers'],
143-
(string) \curl_multi_getcontent($message['handle']),
144-
$info
145-
);
146-
unset($this->parsed[$objectId], $handles[$id]);
147-
break;
148-
}
119+
if ($message === false) {
120+
continue;
121+
}
122+
foreach ($handles as $id => $handle) {
123+
if ($message['handle'] !== $handle) {
124+
continue;
125+
}
126+
$info = [];
127+
if ($requests[$id]->isGettingInfo()) {
128+
$info = (array) \curl_getinfo($handle);
129+
}
130+
$objectId = \spl_object_id($handle);
131+
if (!isset($this->parsed[$objectId])) {
132+
yield $id => new ResponseError(
133+
$requests[$id],
134+
\curl_error($handle),
135+
\curl_errno($handle),
136+
$info
137+
);
138+
unset($handles[$id]);
139+
break;
149140
}
150-
\curl_multi_remove_handle($multiHandle, $message['handle']);
141+
yield $id => new Response(
142+
$requests[$id],
143+
$this->parsed[$objectId]['protocol'],
144+
$this->parsed[$objectId]['code'],
145+
$this->parsed[$objectId]['reason'],
146+
$this->parsed[$objectId]['headers'],
147+
(string) \curl_multi_getcontent($message['handle']),
148+
$info
149+
);
150+
unset($this->parsed[$objectId], $handles[$id]);
151+
break;
151152
}
153+
\curl_multi_remove_handle($multiHandle, $message['handle']);
152154
} while ($stillRunning && $status === \CURLM_OK);
153155
\curl_multi_close($multiHandle);
154156
}

0 commit comments

Comments
 (0)