| Conditions | 10 |
| Paths | 120 |
| Total Lines | 76 |
| Code Lines | 47 |
| 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 |
||
| 35 | protected function doRequest() |
||
| 36 | { |
||
| 37 | $response = array(); |
||
| 38 | $errno = ''; |
||
| 39 | $errstr = ''; |
||
| 40 | $urlArray = $this->generateUrlArray(); |
||
| 41 | |||
| 42 | $urlHost = $urlArray['host']; |
||
| 43 | $urlPath = isset($urlArray['path']) ? $urlArray['path'] : ''; |
||
| 44 | $urlScheme = $urlArray['scheme']; |
||
| 45 | $urlQuery = $urlArray['query']; |
||
| 46 | |||
| 47 | $socketScheme = ''; |
||
| 48 | $socketPort = 80; |
||
| 49 | |||
| 50 | if ($urlScheme == 'https') { |
||
| 51 | $socketScheme = 'ssl://'; |
||
| 52 | $socketPort = 443; |
||
| 53 | } |
||
| 54 | |||
| 55 | // Request - Method |
||
| 56 | $method = 'Post'; |
||
| 57 | |||
| 58 | // Request - Header |
||
| 59 | $headers = array(); |
||
| 60 | $headers[] = "POST " . $urlPath . " HTTP/1.1\r\n"; |
||
| 61 | $headers[] = "Host: " . $urlHost . "\r\n"; |
||
| 62 | $headers[] = "Content-Type: application/x-www-form-urlencoded\r\n"; |
||
| 63 | $headers[] = "Content-Length: " . strlen($urlQuery) . "\r\n"; |
||
| 64 | $headers[] = "Connection: close\r\n\r\n"; |
||
| 65 | |||
| 66 | // Request - Body |
||
| 67 | $body = $urlArray['query']; |
||
| 68 | |||
| 69 | // Request - Build |
||
| 70 | $request = "{$method} {$urlPath} HTTP/1.1\r\n"; |
||
| 71 | foreach ($headers as $k => $v) { |
||
| 72 | $request .= "$v\r\n"; |
||
| 73 | } |
||
| 74 | |||
| 75 | $request .= "\r\n" . $body; |
||
| 76 | |||
| 77 | // Socket - Connect |
||
| 78 | $flags = STREAM_CLIENT_CONNECT; |
||
| 79 | $context = stream_context_create(); |
||
| 80 | $socket = @stream_socket_client( |
||
| 81 | $socketScheme . $urlHost . ':' . $socketPort, |
||
| 82 | $errno, |
||
| 83 | $errstr, |
||
| 84 | self::DEFAULT_TIMEOUT, |
||
| 85 | $flags, |
||
| 86 | $context |
||
| 87 | ); |
||
| 88 | |||
| 89 | // Socket - Write |
||
| 90 | if (!@fwrite($socket, $request)) { |
||
| 91 | throw new Payone_Api_Exception_WritingRequestToServer(); |
||
| 92 | } |
||
| 93 | |||
| 94 | $gotStatus = false; |
||
| 95 | while (($line = @fgets($socket)) !== false) { |
||
| 96 | $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false); |
||
| 97 | if ($gotStatus) { |
||
| 98 | $response[] = $line; |
||
| 99 | if (rtrim($line) === '') { |
||
| 100 | break; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | if (count($response) == 0) { |
||
| 106 | throw new Payone_Api_Exception_InvalidResponse(); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $response; |
||
| 110 | } |
||
| 111 | |||
| 113 |