@@ -63,18 +63,18 @@ discard block |
||
| 63 | 63 | */ |
| 64 | 64 | public function run(): self |
| 65 | 65 | { |
| 66 | - for($i=0; $i<$this->maxConnections; ++$i) { |
|
| 66 | + for ($i = 0; $i < $this->maxConnections; ++$i) { |
|
| 67 | 67 | /** @var resource|false $unemployedWorker */ |
| 68 | 68 | $unemployedWorker = curl_init(); |
| 69 | - if(!$unemployedWorker) { |
|
| 69 | + if (!$unemployedWorker) { |
|
| 70 | 70 | throw new RuntimeException("failed creating unemployed worker #{$i}"); |
| 71 | 71 | } |
| 72 | 72 | $this->unemployedWorkers[] = $unemployedWorker; |
| 73 | 73 | } |
| 74 | 74 | unset($i, $this->unemployedWorker); |
| 75 | 75 | |
| 76 | - foreach($this->requestsConfigMap as $id => $options) { |
|
| 77 | - while(!count($this->unemployedWorkers)) { |
|
| 76 | + foreach ($this->requestsConfigMap as $id => $options) { |
|
| 77 | + while (!count($this->unemployedWorkers)) { |
|
| 78 | 78 | $this->doWork(); |
| 79 | 79 | } |
| 80 | 80 | |
@@ -83,23 +83,23 @@ discard block |
||
| 83 | 83 | $newWorker = array_pop($this->unemployedWorkers); |
| 84 | 84 | |
| 85 | 85 | // @phpstan-ignore-next-line |
| 86 | - if(!curl_setopt_array($newWorker, $options)) { |
|
| 86 | + if (!curl_setopt_array($newWorker, $options)) { |
|
| 87 | 87 | $errNo = curl_errno($newWorker); // @phpstan-ignore-line |
| 88 | 88 | $errMess = curl_error($newWorker); // @phpstan-ignore-line |
| 89 | 89 | $errData = var_export($options, true); |
| 90 | 90 | throw new RuntimeException("curl_setopt_array failed: {$errNo} {$errMess} {$errData}"); |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - $this->workersMap[(int)$newWorker] = $id; |
|
| 93 | + $this->workersMap[(int) $newWorker] = $id; |
|
| 94 | 94 | curl_multi_add_handle($this->mh, $newWorker); // @phpstan-ignore-line |
| 95 | 95 | } |
| 96 | 96 | unset($options); |
| 97 | 97 | |
| 98 | - while(count($this->workersMap) > 0) { |
|
| 98 | + while (count($this->workersMap) > 0) { |
|
| 99 | 99 | $this->doWork(); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - foreach($this->unemployedWorkers as $unemployedWorker) { |
|
| 102 | + foreach ($this->unemployedWorkers as $unemployedWorker) { |
|
| 103 | 103 | curl_close($unemployedWorker); // @phpstan-ignore-line |
| 104 | 104 | } |
| 105 | 105 | |
@@ -118,8 +118,8 @@ discard block |
||
| 118 | 118 | { |
| 119 | 119 | $result = []; |
| 120 | 120 | |
| 121 | - foreach($this->result as $key => $value) { |
|
| 122 | - if(!$okOnly || $value['code'] === 200) { |
|
| 121 | + foreach ($this->result as $key => $value) { |
|
| 122 | + if (!$okOnly || $value['code'] === 200) { |
|
| 123 | 123 | $result[$key] = $value; |
| 124 | 124 | } |
| 125 | 125 | } |
@@ -137,8 +137,8 @@ discard block |
||
| 137 | 137 | { |
| 138 | 138 | $result = []; |
| 139 | 139 | |
| 140 | - foreach($this->result as $key => $value) { |
|
| 141 | - if(!$okOnly || $value['code'] >= 200 && $value['code'] < 300) { |
|
| 140 | + foreach ($this->result as $key => $value) { |
|
| 141 | + if (!$okOnly || $value['code'] >= 200 && $value['code'] < 300) { |
|
| 142 | 142 | $result[$key] = $value['body']; |
| 143 | 143 | } |
| 144 | 144 | } |
@@ -155,12 +155,12 @@ discard block |
||
| 155 | 155 | assert(count($this->workersMap) > 0, "work() called with 0 workers!!"); |
| 156 | 156 | $stillRunning = null; |
| 157 | 157 | |
| 158 | - while(true) { |
|
| 158 | + while (true) { |
|
| 159 | 159 | do { |
| 160 | 160 | $err = curl_multi_exec($this->mh, $stillRunning); // @phpstan-ignore-line |
| 161 | - } while($err === CURLM_CALL_MULTI_PERFORM); |
|
| 161 | + } while ($err === CURLM_CALL_MULTI_PERFORM); |
|
| 162 | 162 | |
| 163 | - if($err !== CURLM_OK) { |
|
| 163 | + if ($err !== CURLM_OK) { |
|
| 164 | 164 | $errInfo = [ |
| 165 | 165 | "multi_exec_return" => $err, |
| 166 | 166 | "curl_multi_errno" => curl_multi_errno($this->mh), // @phpstan-ignore-line |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | $errData = str_replace(["\r", "\n"], "", var_export($errInfo, true)); |
| 171 | 171 | throw new RuntimeException("curl_multi_exec error: {$errData}"); |
| 172 | 172 | } |
| 173 | - if($stillRunning < count($this->workersMap)) { |
|
| 173 | + if ($stillRunning < count($this->workersMap)) { |
|
| 174 | 174 | // some workers has finished downloading, process them |
| 175 | 175 | // echo "processing!"; |
| 176 | 176 | break; |
@@ -181,13 +181,13 @@ discard block |
||
| 181 | 181 | } |
| 182 | 182 | } |
| 183 | 183 | // @phpstan-ignore-next-line |
| 184 | - while(($info = curl_multi_info_read($this->mh)) !== false) { |
|
| 185 | - if($info['msg'] !== CURLMSG_DONE) { |
|
| 184 | + while (($info = curl_multi_info_read($this->mh)) !== false) { |
|
| 185 | + if ($info['msg'] !== CURLMSG_DONE) { |
|
| 186 | 186 | // no idea what this is, it's not the message we're looking for though, ignore it. |
| 187 | 187 | continue; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - if($info['result'] !== CURLM_OK) { |
|
| 190 | + if ($info['result'] !== CURLM_OK) { |
|
| 191 | 191 | $errInfo = [ |
| 192 | 192 | "effective_url" => curl_getinfo($info['handle'], CURLINFO_EFFECTIVE_URL), |
| 193 | 193 | "curl_errno" => curl_errno($info['handle']), |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | $ch = $info['handle']; |
| 204 | - $chIndex = (int)$ch; |
|
| 204 | + $chIndex = (int) $ch; |
|
| 205 | 205 | |
| 206 | 206 | // @phpstan-ignore-next-line |
| 207 | 207 | $this->result[$this->workersMap[$chIndex]] = $this->parseResponse(curl_multi_getcontent($ch)); |
@@ -225,29 +225,29 @@ discard block |
||
| 225 | 225 | $statusCode = null; |
| 226 | 226 | $body = null; |
| 227 | 227 | |
| 228 | - while(count($arResponse)) { |
|
| 228 | + while (count($arResponse)) { |
|
| 229 | 229 | $respItem = array_shift($arResponse); |
| 230 | 230 | |
| 231 | - $line = (string)strtok($respItem, "\r\n"); |
|
| 231 | + $line = (string) strtok($respItem, "\r\n"); |
|
| 232 | 232 | $statusCodeLine = trim($line); |
| 233 | - if(preg_match('|HTTP/[\d.]+\s+(\d+)|', $statusCodeLine, $matches)) { |
|
| 233 | + if (preg_match('|HTTP/[\d.]+\s+(\d+)|', $statusCodeLine, $matches)) { |
|
| 234 | 234 | $arHeaders = []; |
| 235 | 235 | |
| 236 | - if(isset($matches[1])) { |
|
| 237 | - $statusCode = (int)$matches[1]; |
|
| 236 | + if (isset($matches[1])) { |
|
| 237 | + $statusCode = (int) $matches[1]; |
|
| 238 | 238 | } else { |
| 239 | 239 | $statusCode = null; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | // Parse the string, saving it into an array instead |
| 243 | - while(($line = strtok("\r\n")) !== false) { |
|
| 244 | - if(($matches = explode(':', $line, 2)) !== false) { |
|
| 243 | + while (($line = strtok("\r\n")) !== false) { |
|
| 244 | + if (($matches = explode(':', $line, 2)) !== false) { |
|
| 245 | 245 | $arHeaders[trim(mb_strtolower($matches[0]))] = trim(mb_strtolower($matches[1])); |
| 246 | 246 | } |
| 247 | 247 | } |
| 248 | 248 | } else { |
| 249 | 249 | $contentType = $arHeaders['content-type'] ?? null; |
| 250 | - if($contentType === 'application/json') { |
|
| 250 | + if ($contentType === 'application/json') { |
|
| 251 | 251 | $body = json_decode($respItem, true); |
| 252 | 252 | } else { |
| 253 | 253 | $body = $respItem; |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | CURLOPT_ENCODING => '' |
| 46 | 46 | ]; |
| 47 | 47 | |
| 48 | - foreach($commonOptions as $key => $value) { |
|
| 48 | + foreach ($commonOptions as $key => $value) { |
|
| 49 | 49 | $this->commonOptions[$key] = $value; |
| 50 | 50 | } |
| 51 | 51 | |
@@ -84,23 +84,23 @@ discard block |
||
| 84 | 84 | array $options = [], |
| 85 | 85 | array $headers = [] |
| 86 | 86 | ): self { |
| 87 | - foreach($this->commonOptions as $key => $value) { |
|
| 88 | - if(!array_key_exists($key, $options)) { |
|
| 87 | + foreach ($this->commonOptions as $key => $value) { |
|
| 88 | + if (!array_key_exists($key, $options)) { |
|
| 89 | 89 | $options[$key] = $value; |
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - foreach($this->commonHeaders as $key => $value) { |
|
| 94 | - if(!array_key_exists($key, $headers)) { |
|
| 93 | + foreach ($this->commonHeaders as $key => $value) { |
|
| 94 | + if (!array_key_exists($key, $headers)) { |
|
| 95 | 95 | $headers[$key] = $value; |
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - if(is_array($body)) { |
|
| 99 | + if (is_array($body)) { |
|
| 100 | 100 | $body = json_encode($body); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - if($body !== null) { |
|
| 103 | + if ($body !== null) { |
|
| 104 | 104 | /** @var string|numeric $body */ |
| 105 | 105 | $options[CURLOPT_POSTFIELDS] = strval($body); |
| 106 | 106 | } |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | { |
| 123 | 123 | $result = []; |
| 124 | 124 | |
| 125 | - foreach($headers as $key => $value) { |
|
| 125 | + foreach ($headers as $key => $value) { |
|
| 126 | 126 | $result[] = "{$key}: {$value}"; |
| 127 | 127 | } |
| 128 | 128 | |