Skip to content

Commit 550c3e8

Browse files
committed
chore: use logger context
1 parent 21b90ee commit 550c3e8

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed

MangoPay/Libraries/HttpCurl.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ private function RunRequest()
9999
{
100100
$result = curl_exec($this->_curlHandle);
101101
if ($result === false && curl_errno($this->_curlHandle) != 0) {
102-
$this->logger->error("cURL error: " . curl_error($this->_curlHandle));
102+
$this->logger->error('cURL error', [
103+
'errno' => curl_errno($this->_curlHandle),
104+
'error' => curl_error($this->_curlHandle)
105+
]);
103106
throw new Exception('cURL error: ' . curl_error($this->_curlHandle));
104107
}
105108

MangoPay/Libraries/RestTool.php

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ public function GetRequestHeaders()
4545
}
4646

4747
/**
48-
* Request type for current request
49-
* @var RequestType
48+
* Request type for current request (HTTP method).
49+
* One of \MangoPay\Libraries\RequestType constants
50+
* @var string
5051
*/
5152
private $_requestType;
5253

5354
/**
5455
* Return HTTP request method
55-
* @return RequestType
56+
* @return string
5657
*/
5758
public function GetRequestType()
5859
{
@@ -124,7 +125,7 @@ public function AddRequestHttpHeader($httpHeader)
124125
/**
125126
* Call request to MangoPay API
126127
* @param string $urlMethod Type of method in REST API
127-
* @param \MangoPay\Libraries\RequestType $requestType Type of request
128+
* @param string $requestType Type of request (constant of \MangoPay\Libraries\RequestType)
128129
* @param array $requestData Data to send in request
129130
* @param string $idempotencyKey
130131
* @param \MangoPay\Pagination $pagination Pagination object
@@ -139,25 +140,23 @@ public function Request($urlMethod, $requestType, $requestData = null, $idempote
139140
&& (strpos($urlMethod, 'KYC/documents') !== false || strpos($urlMethod, 'dispute-documents') !== false)) {
140141
$this->_requestData = "";
141142
}
142-
$logClass = $this->_root->Config->LogClass;
143-
$this->logger->debug("New request");
144-
if ($this->_root->Config->DebugMode) {
145-
$logClass::Debug('++++++++++++++++++++++ New request ++++++++++++++++++++++', '');
146-
}
143+
144+
$this->debug('++++++++++++++++++++++ New request ++++++++++++++++++++++', '');
145+
147146
$this->BuildRequest($urlMethod, $pagination, $additionalUrlParams, $idempotencyKey);
148147
$responseResult = $this->_root->getHttpClient()->Request($this);
149-
$logClass = $this->_root->Config->LogClass;
150-
$this->logger->debug('Response JSON : ' . print_r($responseResult->Body, true));
151-
if ($this->_root->Config->DebugMode) {
152-
$logClass::Debug('Response JSON', $responseResult->Body);
153-
}
148+
$this->debug('Response JSON', $responseResult->Body);
149+
154150
// FIXME This can fail hard.
155151
$response = json_decode($responseResult->Body);
152+
$this->debug('Response object', $response);
153+
154+
$this->logger->info('Response received', [
155+
'responseCode' => $responseResult->ResponseCode,
156+
'headers' => $responseResult->Headers,
157+
'body' => $response ?: $responseResult->Body,
158+
]);
156159

157-
$this->logger->debug('Decoded object : ' . print_r($response, true));
158-
if ($this->_root->Config->DebugMode) {
159-
$logClass::Debug('Response object', $response);
160-
}
161160
$this->CheckResponseCode($responseResult->ResponseCode, $response);
162161
$this->ReadResponseHeader($responseResult->Headers);
163162
if (!is_null($pagination)) {
@@ -179,36 +178,34 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu
179178
$urlTool = new UrlTool($this->_root);
180179
$restUrl = $urlTool->GetRestUrl($urlMethod, $this->_clientIdRequired, $pagination, $additionalUrlParams);
181180
$this->_requestUrl = $urlTool->GetFullUrl($restUrl);
182-
$logClass = $this->_root->Config->LogClass;
183-
$this->logger->debug('FullUrl : ' . $this->_requestUrl);
184-
if ($this->_root->Config->DebugMode) {
185-
$logClass::Debug('FullUrl', $this->_requestUrl);
186-
}
181+
182+
$this->debug('FullUrl', $this->_requestUrl);
183+
$this->debug('RequestType', $this->_requestType);
184+
187185
if (!is_null($pagination)) {
188186
$this->_pagination = $pagination;
189187
}
190-
$this->logger->debug('RequestType : ' . $this->_requestType);
191-
if ($this->_root->Config->DebugMode) {
192-
$logClass::Debug('RequestType', $this->_requestType);
193-
}
188+
194189
$httpHeaders = $this->GetHttpHeaders($idempotencyKey);
195-
$this->logger->debug('HTTP Headers : ' . print_r($httpHeaders, true));
196-
if ($this->_root->Config->DebugMode) {
197-
$logClass::Debug('HTTP Headers', $httpHeaders);
198-
}
190+
$this->debug('HTTP Headers', $httpHeaders);
191+
192+
$this->logger->info('Building request', [
193+
'urlMethod' => $urlMethod,
194+
'fullUrl' => $this->_requestUrl,
195+
'requestType' => $this->_requestType,
196+
'headers' => $httpHeaders,
197+
'requestData' => $this->_requestData,
198+
'json' => in_array(self::$_JSON_HEADER, $httpHeaders, true),
199+
]);
200+
199201
if (!is_null($this->_requestData)) {
200-
$this->logger->debug('RequestData object :' . print_r($this->_requestData, true));
201-
if ($this->_root->Config->DebugMode) {
202-
$logClass::Debug('RequestData object', $this->_requestData);
203-
}
202+
$this->debug('RequestData object', $this->_requestData);
203+
204204
// encode to json if needed
205205
if (in_array(self::$_JSON_HEADER, $httpHeaders)) {
206206
if (!is_null($this->_requestData)) {
207207
$this->_requestData = json_encode($this->_requestData);
208-
$this->logger->debug('RequestData JSON :' . print_r($this->_requestData, true));
209-
if ($this->_root->Config->DebugMode) {
210-
$logClass::Debug('RequestData JSON', $this->_requestData);
211-
}
208+
$this->debug('RequestData JSON', $this->_requestData);
212209
}
213210
}
214211
}
@@ -220,12 +217,7 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu
220217
*/
221218
private function ReadResponseHeader($headers)
222219
{
223-
$logClass = $this->_root->Config->LogClass;
224-
225-
$this->logger->debug('Response headers :' . print_r($headers, true));
226-
if ($this->_root->Config->DebugMode) {
227-
$logClass::Debug('Response headers', print_r($headers, true));
228-
}
220+
$this->debug('Response headers', print_r($headers, true));
229221

230222
$updatedRateLimits = null;
231223

@@ -397,4 +389,21 @@ private function CheckResponseCode($responseCode, $response)
397389

398390
throw new ResponseException($this->_requestUrl, $responseCode, $error);
399391
}
392+
393+
/**
394+
* Prints debug message & context if debug mode is enabled.
395+
*
396+
* @param string $message
397+
* @param mixed $data
398+
* @return void
399+
*/
400+
private function debug($message, $data = null)
401+
{
402+
if (!$this->_root->Config->DebugMode) {
403+
return;
404+
}
405+
406+
$logClass = $this->_root->Config->LogClass;
407+
$logClass::Debug($message, $data);
408+
}
400409
}

0 commit comments

Comments
 (0)