Conditions | 12 |
Paths | 84 |
Total Lines | 77 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
58 | private function doCall(string $url, ?string $body = null, string $method = 'GET'): SimpleXMLElement |
||
59 | { |
||
60 | $headers = [ |
||
61 | 'Authorization: Basic ' . $this->getAuthorizationHeader(), |
||
62 | ]; |
||
63 | |||
64 | $options = [ |
||
65 | CURLOPT_URL => self::API_URL . $url, |
||
66 | CURLOPT_USERAGENT => $this->getUserAgent(), |
||
67 | CURLOPT_RETURNTRANSFER => true, |
||
68 | CURLOPT_TIMEOUT => $this->getTimeOut(), |
||
69 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||
70 | CURLOPT_HTTPHEADER => $headers, |
||
71 | ]; |
||
72 | |||
73 | if ($this->port !== null) { |
||
74 | $options[CURLOPT_PORT] = $this->port; |
||
75 | } |
||
76 | |||
77 | if ($method === 'POST') { |
||
78 | $options[CURLOPT_POST] = true; |
||
79 | $options[CURLOPT_POSTFIELDS] = $body ?? ''; |
||
80 | } |
||
81 | |||
82 | $curl = curl_init(); |
||
83 | curl_setopt_array($curl, $options); |
||
84 | |||
85 | try { |
||
86 | $response = curl_exec($curl); |
||
87 | $info = curl_getinfo($curl); |
||
88 | |||
89 | $errorNumber = curl_errno($curl); |
||
90 | $errorMessage = curl_error($curl); |
||
91 | |||
92 | if ($errorNumber !== 0) { |
||
93 | throw new BpostCurlException($errorMessage, $errorNumber); |
||
94 | } |
||
95 | |||
96 | $httpCode = (int)($info['http_code'] ?? 0); |
||
97 | |||
98 | // Non 200 => tenter de parser l'erreur métier pour renvoyer l’exception dédiée |
||
99 | if (!in_array($httpCode, [0, 200], true)) { |
||
100 | $xml = @simplexml_load_string((string)$response); |
||
101 | |||
102 | if ( |
||
103 | $xml !== false |
||
104 | && ($xml->getName() === 'businessException' || $xml->getName() === 'systemException') |
||
105 | ) { |
||
106 | $message = (string) ($xml->message ?? ''); |
||
107 | $code = isset($xml->code) ? (int) $xml->code : 0; |
||
108 | |||
109 | if ($xml->getName() === 'businessException') { |
||
110 | throw new BpostApiBusinessException($message, $code); |
||
111 | } |
||
112 | throw new BpostApiSystemException($message, $code); |
||
113 | } |
||
114 | |||
115 | throw new BpostInvalidResponseException('', $httpCode); |
||
116 | } |
||
117 | |||
118 | // 200: parser XML |
||
119 | $xml = simplexml_load_string((string)$response); |
||
120 | if ($xml === false) { |
||
121 | // pas de XML valide alors que 200 => considérer comme réponse invalide |
||
122 | throw new BpostInvalidResponseException('Empty or invalid XML body', 200); |
||
123 | } |
||
124 | |||
125 | if ($xml->getName() === 'businessException') { |
||
126 | $message = (string) ($xml->message ?? ''); |
||
127 | $code = (int) ($xml->code ?? 0); |
||
128 | throw new BpostApiBusinessException($message, $code); |
||
129 | } |
||
130 | |||
131 | return $xml; |
||
132 | } finally { |
||
133 | curl_close($curl); |
||
134 | $curl = null; |
||
|
|||
135 | } |
||
214 | } |