| Conditions | 8 |
| Paths | 28 |
| Total Lines | 58 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 60 | public function doRequest(): BeesWaxResponse |
||
| 61 | { |
||
| 62 | $curlUrl = sprintf(static::BEESWAX_ENDPOINT, $this->session->getBuzzKey(), $this->path); |
||
| 63 | if (!empty($this->queryParams)) { |
||
| 64 | $query = \http_build_query($this->queryParams); |
||
| 65 | $curlUrl .= '?'.$query; |
||
| 66 | } |
||
| 67 | $curlHandler = curl_init($curlUrl); |
||
| 68 | $cookieFileHandler = tmpfile(); |
||
| 69 | |||
| 70 | if ($cookieFileHandler === false) { |
||
| 71 | throw new BeesWaxGenericException( |
||
| 72 | 'Impossible to create a temporary cookie file', |
||
| 73 | BeesWaxGenericException::CODE_CANT_CREATE_COOKIE_FILE |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | $cookieFilePath = stream_get_meta_data($cookieFileHandler)['uri']; |
||
| 78 | |||
| 79 | if (!$cookieFilePath) { |
||
| 80 | throw new BeesWaxGenericException( |
||
| 81 | 'An error occurred creating a temporary cookie file', |
||
| 82 | BeesWaxGenericException::CODE_CANT_CREATE_COOKIE_FILE |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | |||
| 86 | $cookieFilePath = realpath($cookieFilePath); |
||
| 87 | fwrite($cookieFileHandler, $this->session->getSessionCookies()); |
||
| 88 | |||
| 89 | curl_setopt_array($curlHandler, [ |
||
| 90 | CURLOPT_RETURNTRANSFER => 1, |
||
| 91 | CURLOPT_USERAGENT => static::BEESWAX_UA, |
||
| 92 | CURLOPT_COOKIEFILE => $cookieFilePath, |
||
| 93 | CURLOPT_COOKIEJAR => $cookieFilePath, |
||
| 94 | ]); |
||
| 95 | |||
| 96 | if ($this->method === static::METHOD_POST) { |
||
| 97 | curl_setopt($curlHandler, CURLOPT_POST, 1); |
||
| 98 | } elseif ($this->method === static::METHOD_PUT) { |
||
| 99 | curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, static::METHOD_PUT); |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($this->payload) { |
||
| 103 | curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $this->payload); |
||
| 104 | } |
||
| 105 | |||
| 106 | $responsePayload = curl_exec($curlHandler); |
||
| 107 | if ($responsePayload === false) { |
||
| 108 | throw new BeesWaxResponseException($curlHandler, 'An error occurred attempting to log into BeesWax'); |
||
| 109 | } |
||
| 110 | $statusCode = (int) curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); |
||
| 111 | curl_close($curlHandler); |
||
| 112 | $cookiesContent = file_get_contents($cookieFilePath); |
||
| 113 | unlink($cookieFilePath); |
||
| 114 | |||
| 115 | $response = new BeesWaxResponse($curlHandler, $responsePayload, $statusCode, $cookiesContent); |
||
| 116 | |||
| 117 | return $response; |
||
| 118 | } |
||
| 120 |