Conditions | 11 |
Paths | 16 |
Total Lines | 72 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
87 | private function doCall($url, $body = null, $method = 'GET') |
||
88 | { |
||
89 | // build Authorization header |
||
90 | $headers = array(); |
||
91 | $headers[] = 'Authorization: Basic ' . $this->getAuthorizationHeader(); |
||
92 | |||
93 | // set options |
||
94 | $options = array(); |
||
95 | $options[CURLOPT_URL] = self::API_URL . $url; |
||
96 | $options[CURLOPT_USERAGENT] = $this->getUserAgent(); |
||
97 | $options[CURLOPT_RETURNTRANSFER] = true; |
||
98 | $options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut(); |
||
99 | $options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; |
||
100 | $options[CURLOPT_HTTPHEADER] = $headers; |
||
101 | |||
102 | if ($method == 'POST') { |
||
103 | $options[CURLOPT_POST] = true; |
||
104 | $options[CURLOPT_POSTFIELDS] = $body; |
||
105 | } |
||
106 | |||
107 | // init |
||
108 | $this->curl = curl_init(); |
||
109 | |||
110 | // set options |
||
111 | curl_setopt_array($this->curl, $options); |
||
112 | |||
113 | // execute |
||
114 | $response = curl_exec($this->curl); |
||
115 | $headers = curl_getinfo($this->curl); |
||
116 | |||
117 | // fetch errors |
||
118 | $errorNumber = curl_errno($this->curl); |
||
119 | $errorMessage = curl_error($this->curl); |
||
120 | |||
121 | // error? |
||
122 | if ($errorNumber != '') { |
||
123 | throw new BpostCurlException($errorMessage, $errorNumber); |
||
124 | } |
||
125 | |||
126 | // valid HTTP-code |
||
127 | if (!in_array($headers['http_code'], array(0, 200))) { |
||
128 | $xml = @simplexml_load_string($response); |
||
129 | |||
130 | if ( |
||
131 | $xml !== false |
||
132 | && ($xml->getName() == 'businessException' || $xml->getName() == 'systemException') |
||
133 | ) { |
||
134 | $message = (string) $xml->message; |
||
135 | $code = isset($xml->code) ? (int) $xml->code : null; |
||
136 | switch ($xml->getName()) { |
||
137 | case 'businessException': |
||
138 | throw new BpostApiBusinessException($message, $code); |
||
139 | case 'systemException': |
||
140 | throw new BpostApiSystemException($message, $code); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | throw new BpostInvalidResponseException('', $headers['http_code']); |
||
145 | } |
||
146 | |||
147 | // convert into XML |
||
148 | $xml = simplexml_load_string($response); |
||
149 | |||
150 | // validate |
||
151 | if ($xml->getName() == 'businessException') { |
||
152 | $message = (string) $xml->message; |
||
153 | $code = (string) $xml->code; |
||
154 | throw new BpostApiBusinessException($message, $code); |
||
155 | } |
||
156 | |||
157 | // return the response |
||
158 | return $xml; |
||
159 | } |
||
281 |