| Total Complexity | 50 |
| Total Lines | 496 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Request |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var Application the Facebook app entity |
||
| 42 | */ |
||
| 43 | protected $app; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var null|string the access token to use for this request |
||
| 47 | */ |
||
| 48 | protected $accessToken; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string the HTTP method for this request |
||
| 52 | */ |
||
| 53 | protected $method; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string the Graph endpoint for this request |
||
| 57 | */ |
||
| 58 | protected $endpoint; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array the headers to send with this request |
||
| 62 | */ |
||
| 63 | protected $headers = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array the parameters to send with this request |
||
| 67 | */ |
||
| 68 | protected $params = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array the files to send with this request |
||
| 72 | */ |
||
| 73 | protected $files = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string ETag to send with this request |
||
| 77 | */ |
||
| 78 | protected $eTag; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string graph version to use for this request |
||
| 82 | */ |
||
| 83 | protected $graphVersion; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Creates a new Request entity. |
||
| 87 | * |
||
| 88 | * @param null|Application $app |
||
| 89 | * @param null|AccessToken|string $accessToken |
||
| 90 | * @param null|string $method |
||
| 91 | * @param null|string $endpoint |
||
| 92 | * @param null|array $params |
||
| 93 | * @param null|string $eTag |
||
| 94 | * @param null|string $graphVersion |
||
| 95 | */ |
||
| 96 | public function __construct(Application $app = null, $accessToken = null, $method = null, $endpoint = null, array $params = [], $eTag = null, $graphVersion = null) |
||
| 97 | { |
||
| 98 | $this->setApp($app); |
||
| 99 | $this->setAccessToken($accessToken); |
||
| 100 | $this->setMethod($method); |
||
| 101 | $this->setEndpoint($endpoint); |
||
| 102 | $this->setParams($params); |
||
| 103 | $this->setETag($eTag); |
||
| 104 | $this->graphVersion = $graphVersion; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Set the access token for this request. |
||
| 109 | * |
||
| 110 | * @param null|AccessToken|string |
||
| 111 | * @param mixed $accessToken |
||
| 112 | * |
||
| 113 | * @return Request |
||
| 114 | */ |
||
| 115 | public function setAccessToken($accessToken) |
||
| 116 | { |
||
| 117 | $this->accessToken = $accessToken; |
||
| 118 | if ($accessToken instanceof AccessToken) { |
||
| 119 | $this->accessToken = $accessToken->getValue(); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Sets the access token with one harvested from a URL or POST params. |
||
| 127 | * |
||
| 128 | * @param string $accessToken the access token |
||
| 129 | * |
||
| 130 | * @throws SDKException |
||
| 131 | * |
||
| 132 | * @return Request |
||
| 133 | */ |
||
| 134 | public function setAccessTokenFromParams($accessToken) |
||
| 135 | { |
||
| 136 | $existingAccessToken = $this->getAccessToken(); |
||
| 137 | if (!$existingAccessToken) { |
||
|
|
|||
| 138 | $this->setAccessToken($accessToken); |
||
| 139 | } elseif ($accessToken !== $existingAccessToken) { |
||
| 140 | throw new SDKException('Access token mismatch. The access token provided in the Request and the one provided in the URL or POST params do not match.'); |
||
| 141 | } |
||
| 142 | |||
| 143 | return $this; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return the access token for this request. |
||
| 148 | * |
||
| 149 | * @return null|string |
||
| 150 | */ |
||
| 151 | public function getAccessToken() |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return the access token for this request as an AccessToken entity. |
||
| 158 | * |
||
| 159 | * @return null|AccessToken |
||
| 160 | */ |
||
| 161 | public function getAccessTokenEntity() |
||
| 162 | { |
||
| 163 | return $this->accessToken ? new AccessToken($this->accessToken) : null; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Set the Application entity used for this request. |
||
| 168 | * |
||
| 169 | * @param null|Application $app |
||
| 170 | */ |
||
| 171 | public function setApp(Application $app = null) |
||
| 172 | { |
||
| 173 | $this->app = $app; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Return the Application entity used for this request. |
||
| 178 | * |
||
| 179 | * @return Application |
||
| 180 | */ |
||
| 181 | public function getApplication() |
||
| 182 | { |
||
| 183 | return $this->app; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Generate an app secret proof to sign this request. |
||
| 188 | * |
||
| 189 | * @return null|string |
||
| 190 | */ |
||
| 191 | public function getAppSecretProof() |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Validate that an access token exists for this request. |
||
| 202 | * |
||
| 203 | * @throws SDKException |
||
| 204 | */ |
||
| 205 | public function validateAccessToken() |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Set the HTTP method for this request. |
||
| 215 | * |
||
| 216 | * @param string |
||
| 217 | * @param mixed $method |
||
| 218 | */ |
||
| 219 | public function setMethod($method) |
||
| 220 | { |
||
| 221 | $this->method = strtoupper($method); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Return the HTTP method for this request. |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getMethod() |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Validate that the HTTP method is set. |
||
| 236 | * |
||
| 237 | * @throws SDKException |
||
| 238 | */ |
||
| 239 | public function validateMethod() |
||
| 240 | { |
||
| 241 | if (!$this->method) { |
||
| 242 | throw new SDKException('HTTP method not specified.'); |
||
| 243 | } |
||
| 244 | |||
| 245 | if (!in_array($this->method, ['GET', 'POST', 'DELETE'])) { |
||
| 246 | throw new SDKException('Invalid HTTP method specified.'); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set the endpoint for this request. |
||
| 252 | * |
||
| 253 | * @param string |
||
| 254 | * @param mixed $endpoint |
||
| 255 | * |
||
| 256 | * @throws SDKException |
||
| 257 | * |
||
| 258 | * @return Request |
||
| 259 | */ |
||
| 260 | public function setEndpoint($endpoint) |
||
| 261 | { |
||
| 262 | // Harvest the access token from the endpoint to keep things in sync |
||
| 263 | $params = UrlManipulator::getParamsAsArray($endpoint); |
||
| 264 | if (isset($params['access_token'])) { |
||
| 265 | $this->setAccessTokenFromParams($params['access_token']); |
||
| 266 | } |
||
| 267 | |||
| 268 | // Clean the token & app secret proof from the endpoint. |
||
| 269 | $filterParams = ['access_token', 'appsecret_proof']; |
||
| 270 | $this->endpoint = UrlManipulator::removeParamsFromUrl($endpoint, $filterParams); |
||
| 271 | |||
| 272 | return $this; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Return the endpoint for this request. |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getEndpoint() |
||
| 281 | { |
||
| 282 | // For batch requests, this will be empty |
||
| 283 | return $this->endpoint; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Generate and return the headers for this request. |
||
| 288 | * |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | public function getHeaders() |
||
| 292 | { |
||
| 293 | $headers = static::getDefaultHeaders(); |
||
| 294 | |||
| 295 | if ($this->eTag) { |
||
| 296 | $headers['If-None-Match'] = $this->eTag; |
||
| 297 | } |
||
| 298 | |||
| 299 | return array_merge($this->headers, $headers); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Set the headers for this request. |
||
| 304 | * |
||
| 305 | * @param array $headers |
||
| 306 | */ |
||
| 307 | public function setHeaders(array $headers) |
||
| 308 | { |
||
| 309 | $this->headers = array_merge($this->headers, $headers); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Sets the eTag value. |
||
| 314 | * |
||
| 315 | * @param string $eTag |
||
| 316 | */ |
||
| 317 | public function setETag($eTag) |
||
| 318 | { |
||
| 319 | $this->eTag = $eTag; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set the params for this request. |
||
| 324 | * |
||
| 325 | * @param array $params |
||
| 326 | * |
||
| 327 | * @throws SDKException |
||
| 328 | * |
||
| 329 | * @return Request |
||
| 330 | */ |
||
| 331 | public function setParams(array $params = []) |
||
| 332 | { |
||
| 333 | if (isset($params['access_token'])) { |
||
| 334 | $this->setAccessTokenFromParams($params['access_token']); |
||
| 335 | } |
||
| 336 | |||
| 337 | // Don't let these buggers slip in. |
||
| 338 | unset($params['access_token'], $params['appsecret_proof']); |
||
| 339 | |||
| 340 | // @TODO Refactor code above with this |
||
| 341 | //$params = $this->sanitizeAuthenticationParams($params); |
||
| 342 | $params = $this->sanitizeFileParams($params); |
||
| 343 | $this->dangerouslySetParams($params); |
||
| 344 | |||
| 345 | return $this; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Set the params for this request without filtering them first. |
||
| 350 | * |
||
| 351 | * @param array $params |
||
| 352 | * |
||
| 353 | * @return Request |
||
| 354 | */ |
||
| 355 | public function dangerouslySetParams(array $params = []) |
||
| 356 | { |
||
| 357 | $this->params = array_merge($this->params, $params); |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Iterate over the params and pull out the file uploads. |
||
| 364 | * |
||
| 365 | * @param array $params |
||
| 366 | * |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | public function sanitizeFileParams(array $params) |
||
| 370 | { |
||
| 371 | foreach ($params as $key => $value) { |
||
| 372 | if ($value instanceof File) { |
||
| 373 | $this->addFile($key, $value); |
||
| 374 | unset($params[$key]); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | return $params; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Add a file to be uploaded. |
||
| 383 | * |
||
| 384 | * @param string $key |
||
| 385 | * @param File $file |
||
| 386 | */ |
||
| 387 | public function addFile($key, File $file) |
||
| 388 | { |
||
| 389 | $this->files[$key] = $file; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Removes all the files from the upload queue. |
||
| 394 | */ |
||
| 395 | public function resetFiles() |
||
| 396 | { |
||
| 397 | $this->files = []; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get the list of files to be uploaded. |
||
| 402 | * |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | public function getFiles() |
||
| 406 | { |
||
| 407 | return $this->files; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Let's us know if there is a file upload with this request. |
||
| 412 | * |
||
| 413 | * @return bool |
||
| 414 | */ |
||
| 415 | public function containsFileUploads() |
||
| 416 | { |
||
| 417 | return !empty($this->files); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Let's us know if there is a video upload with this request. |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | public function containsVideoUploads() |
||
| 426 | { |
||
| 427 | foreach ($this->files as $file) { |
||
| 428 | if ($file instanceof Video) { |
||
| 429 | return true; |
||
| 430 | } |
||
| 431 | } |
||
| 432 | |||
| 433 | return false; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns the body of the request as multipart/form-data. |
||
| 438 | * |
||
| 439 | * @return RequestBodyMultipart |
||
| 440 | */ |
||
| 441 | public function getMultipartBody() |
||
| 442 | { |
||
| 443 | $params = $this->getPostParams(); |
||
| 444 | |||
| 445 | return new RequestBodyMultipart($params, $this->files); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Returns the body of the request as URL-encoded. |
||
| 450 | * |
||
| 451 | * @return RequestBodyUrlEncoded |
||
| 452 | */ |
||
| 453 | public function getUrlEncodedBody() |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Generate and return the params for this request. |
||
| 462 | * |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | public function getParams() |
||
| 466 | { |
||
| 467 | $params = $this->params; |
||
| 468 | |||
| 469 | $accessToken = $this->getAccessToken(); |
||
| 470 | if ($accessToken) { |
||
| 471 | $params['access_token'] = $accessToken; |
||
| 472 | $params['appsecret_proof'] = $this->getAppSecretProof(); |
||
| 473 | } |
||
| 474 | |||
| 475 | return $params; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Only return params on POST requests. |
||
| 480 | * |
||
| 481 | * @return array |
||
| 482 | */ |
||
| 483 | public function getPostParams() |
||
| 484 | { |
||
| 485 | if ($this->getMethod() === 'POST') { |
||
| 486 | return $this->getParams(); |
||
| 487 | } |
||
| 488 | |||
| 489 | return []; |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * The graph version used for this request. |
||
| 494 | * |
||
| 495 | * @return null|string |
||
| 496 | */ |
||
| 497 | public function getGraphVersion() |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Generate and return the URL for this request. |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | public function getUrl() |
||
| 508 | { |
||
| 509 | $this->validateMethod(); |
||
| 510 | |||
| 511 | $graphVersion = UrlManipulator::forceSlashPrefix($this->graphVersion); |
||
| 512 | $endpoint = UrlManipulator::forceSlashPrefix($this->getEndpoint()); |
||
| 513 | |||
| 514 | $url = $graphVersion . $endpoint; |
||
| 515 | |||
| 516 | if ($this->getMethod() !== 'POST') { |
||
| 517 | $params = $this->getParams(); |
||
| 518 | $url = UrlManipulator::appendParamsToUrl($url, $params); |
||
| 519 | } |
||
| 520 | |||
| 521 | return $url; |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Return the default headers that every request should use. |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | public static function getDefaultHeaders() |
||
| 534 | ]; |
||
| 535 | } |
||
| 536 | } |
||
| 537 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: