Skip to content

Commit 379a1d0

Browse files
committed
tidy: refactor tick function to reduce cyclomatic complexity
1 parent 95e52d3 commit 379a1d0

File tree

1 file changed

+56
-40
lines changed

1 file changed

+56
-40
lines changed

src/RequestResolver.php

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public function add(
5656
UriInterface $uri,
5757
array $curlOptArray,
5858
Deferred $deferred,
59-
string $integrity = null,
60-
Controller $signal = null,
59+
?string $integrity = null,
60+
?Controller $signal = null,
6161
):void {
6262
/** @var CurlInterface $curl */
6363
$curl = new $this->curlClass($uri);
@@ -101,55 +101,71 @@ public function tick():void {
101101
$totalActive = 0;
102102

103103
foreach($this->curlMultiList as $i => $curlMulti) {
104-
$active = 0;
104+
$this->processCurlMulti($curlMulti, $i, $totalActive);
105+
}
105106

106-
// 1) This first do-while loop initiates all underlying curl handles, but on
107-
// slow networks, this might not be enough to download all responses...
108-
do {
109-
$status = $curlMulti->exec($active);
110-
}
111-
while($status === CURLM_CALL_MULTI_PERFORM);
107+
if($totalActive === 0) {
108+
$this->loop->halt();
109+
}
110+
}
112111

113-
if($status !== CURLM_OK) {
114-
$errNo = curl_multi_errno($curlMulti->getHandle());
115-
$errString = curl_multi_strerror($errNo);
116-
throw new CurlException($errString);
117-
}
112+
private function processCurlMulti(
113+
CurlMultiInterface $curlMulti,
114+
int $index,
115+
int &$totalActive,
116+
):void {
117+
$active = 0;
118118

119+
$this->executeCurlMultiLoop($curlMulti, $active);
120+
121+
if($active === 0) {
122+
$this->handleCompletedCurlMulti($index);
123+
}
124+
else {
119125
$totalActive += $active;
126+
}
127+
}
120128

121-
if($active === 0) {
122-
$response = $this->responseList[$i] ?? null;
123-
$response->endDeferredResponse(
124-
$this->integrityList[$i]
125-
);
126-
if($this->deferredList[$i]) {
127-
$this->deferredList[$i]->resolve($response);
128-
}
129+
private function executeCurlMultiLoop(
130+
CurlMultiInterface $curlMulti,
131+
int &$active,
132+
):void {
133+
do {
134+
$status = $curlMulti->exec($active);
135+
}
136+
while($status === CURLM_CALL_MULTI_PERFORM);
129137

130-
$response = null;
131-
$this->deferredList[$i] = null;
132-
}
133-
else {
134-
while($active && $status === CURLM_OK) {
135-
// 2) We must wait for network activity, because there may be no activity
136-
// between us starting the request and checking the response, especially with
137-
// slow servers.
138-
if($curlMulti->select() !== -1) {
139-
do {
140-
$status = $curlMulti->exec($active);
141-
}
142-
while($status === CURLM_CALL_MULTI_PERFORM);
143-
}
144-
}
145-
}
138+
if($status !== CURLM_OK) {
139+
$this->handleCurlMultiError($curlMulti);
146140
}
141+
}
147142

148-
if($totalActive === 0) {
149-
$this->loop->halt();
143+
private function handleCurlMultiError(CurlMultiInterface $curlMulti):void {
144+
$errNo = curl_multi_errno($curlMulti->getHandle());
145+
$errString = curl_multi_strerror($errNo);
146+
throw new CurlException($errString);
147+
}
148+
149+
private function handleCompletedCurlMulti(int $index):void {
150+
$response = $this->responseList[$index] ?? null;
151+
152+
if($response) {
153+
$response->endDeferredResponse($this->integrityList[$index]);
154+
155+
if($this->deferredList[$index]) {
156+
$this->deferredList[$index]->resolve($response);
157+
}
158+
159+
$this->cleanupCompletedRequestData($index);
150160
}
151161
}
152162

163+
private function cleanupCompletedRequestData(int $index):void {
164+
$this->responseList[$index] = null;
165+
$this->deferredList[$index] = null;
166+
}
167+
168+
153169
private function writeHeader(
154170
CurlHandle|CurlInterface $ch,
155171
string $rawHeader,

0 commit comments

Comments
 (0)