Total Complexity | 41 |
Total Lines | 287 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 | [ $headerName, $headerValue ] = explode( ':', $rawHeader, 2 ); |
||
208 | if ( strtolower( trim( $headerName ) ) === 'set-cookie' ) { |
||
209 | // TODO Maybe use a cookie file? |
||
210 | $cookieKeyVal = explode( ';', $headerValue )[0]; |
||
211 | [ $name, $value ] = explode( '=', $cookieKeyVal ); |
||
212 | $this->newCookies[$name] = $value; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Actual method which will make the request |
||
218 | * |
||
219 | * @param string $params |
||
220 | * @return string |
||
221 | */ |
||
222 | abstract protected function reallyMakeRequest( string $params ): string; |
||
223 | |||
224 | /** |
||
225 | * Get a specific exception class depending on the error code |
||
226 | * |
||
227 | * @param stdClass $res |
||
228 | * @return APIRequestException |
||
229 | */ |
||
230 | private function getException( stdClass $res ): APIRequestException { |
||
231 | switch ( $res->error->code ) { |
||
232 | case 'missingtitle': |
||
233 | $ex = new MissingPageException; |
||
234 | break; |
||
235 | case 'protectedpage': |
||
236 | $ex = new ProtectedPageException; |
||
237 | break; |
||
238 | case 'permissiondenied': |
||
239 | $ex = new PermissionDeniedException( $res->error->info ); |
||
240 | break; |
||
241 | case 'blocked': |
||
242 | $ex = new BlockedException( $res->error->info ); |
||
243 | break; |
||
244 | default: |
||
245 | $ex = new APIRequestException( $res->error->code . ' - ' . $res->error->info ); |
||
246 | } |
||
247 | return $ex; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Handle known warning and errors from an API request |
||
252 | * |
||
253 | * @param stdClass $res |
||
254 | * @throws APIRequestException |
||
255 | */ |
||
256 | protected function handleErrorAndWarnings( stdClass $res ): void { |
||
257 | if ( isset( $res->error ) ) { |
||
258 | throw $this->getException( $res ); |
||
259 | } |
||
260 | if ( isset( $res->warnings ) ) { |
||
261 | $act = $this->params[ 'action' ]; |
||
262 | $warning = $res->warnings->$act ?? $res->warnings->main; |
||
263 | throw new APIRequestException( reset( $warning ) ); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Get the headers to use for a new request |
||
269 | * |
||
270 | * @return string[] |
||
271 | */ |
||
272 | protected function getHeaders(): array { |
||
273 | $ret = self::HEADERS; |
||
274 | if ( $this->cookiesToSet ) { |
||
275 | $cookies = []; |
||
276 | foreach ( $this->cookiesToSet as $cname => $cval ) { |
||
277 | $cookies[] = trim( "$cname=$cval" ); |
||
278 | } |
||
279 | $ret[] = 'Cookie: ' . implode( '; ', $cookies ); |
||
280 | } |
||
281 | return $ret; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Utility function to implode headers |
||
286 | * |
||
287 | * @param string[] $headers |
||
288 | * @return string |
||
289 | */ |
||
290 | protected function buildHeadersString( array $headers ): string { |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param string $actualParams |
||
300 | * @return string |
||
301 | */ |
||
302 | protected function getDebugURL( string $actualParams ): string { |
||
306 | } |
||
307 | } |
||
308 |