@@ -45,14 +45,15 @@ public function GetRequestHeaders()
45
45
}
46
46
47
47
/**
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
50
51
*/
51
52
private $ _requestType ;
52
53
53
54
/**
54
55
* Return HTTP request method
55
- * @return RequestType
56
+ * @return string
56
57
*/
57
58
public function GetRequestType ()
58
59
{
@@ -124,7 +125,7 @@ public function AddRequestHttpHeader($httpHeader)
124
125
/**
125
126
* Call request to MangoPay API
126
127
* @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)
128
129
* @param array $requestData Data to send in request
129
130
* @param string $idempotencyKey
130
131
* @param \MangoPay\Pagination $pagination Pagination object
@@ -139,25 +140,23 @@ public function Request($urlMethod, $requestType, $requestData = null, $idempote
139
140
&& (strpos ($ urlMethod , 'KYC/documents ' ) !== false || strpos ($ urlMethod , 'dispute-documents ' ) !== false )) {
140
141
$ this ->_requestData = "" ;
141
142
}
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
+
147
146
$ this ->BuildRequest ($ urlMethod , $ pagination , $ additionalUrlParams , $ idempotencyKey );
148
147
$ 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
+
154
150
// FIXME This can fail hard.
155
151
$ 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
+ ]);
156
159
157
- $ this ->logger ->debug ('Decoded object : ' . print_r ($ response , true ));
158
- if ($ this ->_root ->Config ->DebugMode ) {
159
- $ logClass ::Debug ('Response object ' , $ response );
160
- }
161
160
$ this ->CheckResponseCode ($ responseResult ->ResponseCode , $ response );
162
161
$ this ->ReadResponseHeader ($ responseResult ->Headers );
163
162
if (!is_null ($ pagination )) {
@@ -179,36 +178,34 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu
179
178
$ urlTool = new UrlTool ($ this ->_root );
180
179
$ restUrl = $ urlTool ->GetRestUrl ($ urlMethod , $ this ->_clientIdRequired , $ pagination , $ additionalUrlParams );
181
180
$ 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
+
187
185
if (!is_null ($ pagination )) {
188
186
$ this ->_pagination = $ pagination ;
189
187
}
190
- $ this ->logger ->debug ('RequestType : ' . $ this ->_requestType );
191
- if ($ this ->_root ->Config ->DebugMode ) {
192
- $ logClass ::Debug ('RequestType ' , $ this ->_requestType );
193
- }
188
+
194
189
$ 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
+
199
201
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
+
204
204
// encode to json if needed
205
205
if (in_array (self ::$ _JSON_HEADER , $ httpHeaders )) {
206
206
if (!is_null ($ this ->_requestData )) {
207
207
$ 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 );
212
209
}
213
210
}
214
211
}
@@ -220,12 +217,7 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu
220
217
*/
221
218
private function ReadResponseHeader ($ headers )
222
219
{
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 ));
229
221
230
222
$ updatedRateLimits = null ;
231
223
@@ -397,4 +389,21 @@ private function CheckResponseCode($responseCode, $response)
397
389
398
390
throw new ResponseException ($ this ->_requestUrl , $ responseCode , $ error );
399
391
}
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
+ }
400
409
}
0 commit comments