| Conditions | 12 |
| Paths | 512 |
| Total Lines | 64 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 88 | protected function makeHttpRequest($url, $verb, $params, $body, $headers = array(), $timeout = null) |
||
| 89 | { |
||
| 90 | // create the full URL |
||
| 91 | if (count($params) > 0) { |
||
| 92 | $url = $url . '?' . http_build_query($params); |
||
| 93 | } |
||
| 94 | |||
| 95 | // what are we doing? |
||
| 96 | $logMsg = [ "HTTP " . strtoupper($verb) . " '${url}'" ]; |
||
| 97 | if ($body != null) { |
||
| 98 | $logMsg[] = $body; |
||
| 99 | } |
||
| 100 | if (count($headers) > 0) { |
||
| 101 | $logMsg[] = $headers; |
||
| 102 | } |
||
| 103 | $log = Log::usingLog()->startAction($logMsg); |
||
| 104 | |||
| 105 | // build the HTTP request |
||
| 106 | $request = new HttpClientRequest($url); |
||
| 107 | $request->withUserAgent("Storyplayer") |
||
| 108 | ->withHttpVerb($verb); |
||
| 109 | |||
| 110 | if (is_array($headers)) { |
||
| 111 | foreach ($headers as $key => $value) { |
||
| 112 | $request->withExtraHeader($key, $value); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | if (is_array($body)) { |
||
| 117 | foreach ($body as $key => $value) { |
||
| 118 | $request->addData($key, $value); |
||
| 119 | } |
||
| 120 | }else{ |
||
| 121 | $request->setPayload($body); |
||
| 122 | } |
||
| 123 | |||
| 124 | // special case - do we validate SSL certificates in this |
||
| 125 | // test environment? |
||
| 126 | $validateSsl = fromConfig()->getModuleSetting("http.validateSsl"); |
||
| 127 | if (null === $validateSsl) { |
||
| 128 | // default to TRUE if no setting present |
||
| 129 | $validateSsl = true; |
||
| 130 | } |
||
| 131 | if (!$validateSsl) { |
||
| 132 | $request->disableSslCertificateValidation(); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($timeout !== null) { |
||
| 136 | $request->setReadTimeout($timeout); |
||
| 137 | } |
||
| 138 | |||
| 139 | // make the call |
||
| 140 | $client = new HttpClient(); |
||
| 141 | $response = $client->newRequest($request); |
||
| 142 | |||
| 143 | // is this a valid response? |
||
| 144 | if (!$response instanceof HttpClientResponse) { |
||
| 145 | throw Exceptions::newActionFailedException(__METHOD__); |
||
| 146 | } |
||
| 147 | |||
| 148 | // all done |
||
| 149 | $log->endAction($response); |
||
| 150 | return $response; |
||
| 151 | } |
||
| 152 | } |
||
| 153 |