| Total Complexity | 42 |
| Total Lines | 289 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like RequestBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RequestBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare( strict_types=1 ); |
||
| 19 | abstract class RequestBase { |
||
| 20 | protected const USER_AGENT = 'Daimona - BotRiconferme ' . BOT_VERSION . |
||
| 21 | ' (https://github.com/Daimona/BotRiconferme)'; |
||
| 22 | protected const HEADERS = [ |
||
| 23 | 'Content-Type: application/x-www-form-urlencoded', |
||
| 24 | 'User-Agent: ' . self::USER_AGENT |
||
| 25 | ]; |
||
| 26 | // In seconds |
||
| 27 | protected const MAXLAG = 5; |
||
| 28 | |||
| 29 | protected const METHOD_GET = 'GET'; |
||
| 30 | protected const METHOD_POST = 'POST'; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | protected $url; |
||
| 34 | /** @var string[] */ |
||
| 35 | protected $cookiesToSet; |
||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | * @phan-var array<int|string|bool> |
||
| 39 | */ |
||
| 40 | protected $params; |
||
| 41 | /** @var string */ |
||
| 42 | protected $method = self::METHOD_GET; |
||
| 43 | /** @var string[] */ |
||
| 44 | protected $newCookies = []; |
||
| 45 | /** @var callable|null */ |
||
| 46 | private $cookiesHandlerCallback; |
||
| 47 | |||
| 48 | /** @var LoggerInterface */ |
||
| 49 | protected $logger; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @private Use RequestFactory |
||
| 53 | * |
||
| 54 | * @param LoggerInterface $logger |
||
| 55 | * @param array $params |
||
| 56 | * @phan-param array<int|string|bool> $params |
||
| 57 | * @param string $domain |
||
| 58 | * @param callable $cookiesHandlerCallback |
||
| 59 | */ |
||
| 60 | public function __construct( |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set the method to POST |
||
| 74 | * |
||
| 75 | * @return self For chaining |
||
| 76 | */ |
||
| 77 | public function setPost(): self { |
||
| 78 | $this->method = self::METHOD_POST; |
||
| 79 | return $this; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string[] $cookies |
||
| 84 | * @return self For chaining |
||
| 85 | */ |
||
| 86 | public function setCookies( array $cookies ): self { |
||
| 87 | $this->cookiesToSet = $cookies; |
||
| 88 | return $this; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Execute a query request |
||
| 93 | * @return Generator |
||
| 94 | */ |
||
| 95 | public function executeAsQuery(): Generator { |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Execute a request that doesn't need any continuation. |
||
| 127 | * @return stdClass |
||
| 128 | */ |
||
| 129 | public function executeSingle(): stdClass { |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return int |
||
| 138 | */ |
||
| 139 | private function parseLimit(): int { |
||
| 140 | foreach ( $this->params as $name => $val ) { |
||
| 141 | if ( substr( $name, -strlen( 'limit' ) ) === 'limit' ) { |
||
| 142 | return $val === 'max' ? -1 : (int)$val; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | // Assume no limit |
||
| 146 | return -1; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Try to count the amount of entries in a result. |
||
| 151 | * |
||
| 152 | * @param stdClass $res |
||
| 153 | * @param string $resKey |
||
| 154 | * @return int|null |
||
| 155 | */ |
||
| 156 | private function countQueryResults( stdClass $res, string $resKey ): ?int { |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Process parameters and call the actual request method |
||
| 179 | * |
||
| 180 | * @param array $params |
||
| 181 | * @phan-param array<int|string|bool> $params |
||
| 182 | * @return stdClass |
||
| 183 | */ |
||
| 184 | private function makeRequestInternal( array $params ): stdClass { |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Parses an HTTP response header. |
||
| 203 | * |
||
| 204 | * @param string $rawHeader |
||
| 205 | */ |
||
| 206 | protected function handleResponseHeader( string $rawHeader ): void { |
||
| 207 | $headerParts = explode( ':', $rawHeader, 2 ); |
||
| 208 | $headerName = $headerParts[0]; |
||
| 209 | $headerValue = $headerParts[1] ?? null; |
||
| 210 | if ( strtolower( trim( $headerName ) ) === 'set-cookie' && $headerValue ) { |
||
| 211 | // TODO Maybe use a cookie file? |
||
| 212 | $cookieKeyVal = explode( ';', $headerValue )[0]; |
||
| 213 | [ $name, $value ] = explode( '=', $cookieKeyVal ); |
||
| 214 | $this->newCookies[$name] = $value; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Actual method which will make the request |
||
| 220 | * |
||
| 221 | * @param string $params |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | abstract protected function reallyMakeRequest( string $params ): string; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get a specific exception class depending on the error code |
||
| 228 | * |
||
| 229 | * @param stdClass $res |
||
| 230 | * @return APIRequestException |
||
| 231 | */ |
||
| 232 | private function getException( stdClass $res ): APIRequestException { |
||
| 233 | switch ( $res->error->code ) { |
||
| 234 | case 'missingtitle': |
||
| 235 | $ex = new MissingPageException; |
||
| 236 | break; |
||
| 237 | case 'protectedpage': |
||
| 238 | $ex = new ProtectedPageException; |
||
| 239 | break; |
||
| 240 | case 'permissiondenied': |
||
| 241 | $ex = new PermissionDeniedException( $res->error->info ); |
||
| 242 | break; |
||
| 243 | case 'blocked': |
||
| 244 | $ex = new BlockedException( $res->error->info ); |
||
| 245 | break; |
||
| 246 | default: |
||
| 247 | $ex = new APIRequestException( $res->error->code . ' - ' . $res->error->info ); |
||
| 248 | } |
||
| 249 | return $ex; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Handle known warning and errors from an API request |
||
| 254 | * |
||
| 255 | * @param stdClass $res |
||
| 256 | * @throws APIRequestException |
||
| 257 | */ |
||
| 258 | protected function handleErrorAndWarnings( stdClass $res ): void { |
||
| 259 | if ( isset( $res->error ) ) { |
||
| 260 | throw $this->getException( $res ); |
||
| 261 | } |
||
| 262 | if ( isset( $res->warnings ) ) { |
||
| 263 | $act = $this->params[ 'action' ]; |
||
| 264 | $warning = $res->warnings->$act ?? $res->warnings->main; |
||
| 265 | throw new APIRequestException( reset( $warning ) ); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the headers to use for a new request |
||
| 271 | * |
||
| 272 | * @return string[] |
||
| 273 | */ |
||
| 274 | protected function getHeaders(): array { |
||
| 275 | $ret = self::HEADERS; |
||
| 276 | if ( $this->cookiesToSet ) { |
||
| 277 | $cookies = []; |
||
| 278 | foreach ( $this->cookiesToSet as $cname => $cval ) { |
||
| 279 | $cookies[] = trim( "$cname=$cval" ); |
||
| 280 | } |
||
| 281 | $ret[] = 'Cookie: ' . implode( '; ', $cookies ); |
||
| 282 | } |
||
| 283 | return $ret; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Utility function to implode headers |
||
| 288 | * |
||
| 289 | * @param string[] $headers |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | protected function buildHeadersString( array $headers ): string { |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $actualParams |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | protected function getDebugURL( string $actualParams ): string { |
||
| 308 | } |
||
| 309 | } |
||
| 310 |