Skip to content

Commit

Permalink
Merge pull request #69 from iammichiel/adding-psr-implementation
Browse files Browse the repository at this point in the history
Adding PSR Logger implementation
  • Loading branch information
hobailey committed Feb 9, 2016
2 parents 4a9de74 + 399849f commit 4fa9126
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 17 deletions.
8 changes: 8 additions & 0 deletions MangoPay/Libraries/ApiBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ abstract class ApiBase {
* @var \MangoPay\MangoPayApi
*/
protected $_root;

/**
* @return mixed
*/
protected function getLogger()
{
return $this->_root->getLogger();
}

/**
* Array with REST url and request type
Expand Down
68 changes: 52 additions & 16 deletions MangoPay/Libraries/RestTool.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace MangoPay\Libraries;
use Psr\Log\LoggerInterface;

/**
* Class to prepare HTTP request, call the request and decode the response
Expand Down Expand Up @@ -47,7 +48,12 @@ class RestTool {
* @var int
*/
private $_responseCode;


/**
* @var LoggerInterface
*/
private $logger;

/**
* Pagination object
* @var MangoPay\Pagination
Expand All @@ -66,6 +72,7 @@ class RestTool {
function __construct($authRequired = true, $root) {
$this->_authRequired = $authRequired;
$this->_root = $root;
$this->logger = $root->getLogger();
}

public function AddRequestHttpHeader($httpHeader) {
Expand All @@ -91,13 +98,15 @@ public function Request($urlMethod, $requestType, $requestData = null, $idempote
$this->_requestData = $requestData;

$logClass = $this->_root->Config->LogClass;
if ($this->_root->Config->DebugMode)
$this->logger->debug("New request");
if ($this->_root->Config->DebugMode) {
$logClass::Debug('++++++++++++++++++++++ New request ++++++++++++++++++++++', '');
}

$this->BuildRequest($urlMethod, $pagination, $additionalUrlParams, $idempotencyKey);
$responseResult = $this->RunRequest();

if(!is_null($pagination)){
if(!is_null($pagination)) {
$pagination = $this->_pagination;
}

Expand All @@ -112,22 +121,31 @@ public function Request($urlMethod, $requestType, $requestData = null, $idempote
private function RunRequest() {

$result = curl_exec($this->_curlHandle);
if ($result === false && curl_errno($this->_curlHandle) != 0)
if ($result === false && curl_errno($this->_curlHandle) != 0) {
$this->logger->error("cURL error: " . curl_error($this->_curlHandle));

throw new Exception('cURL error: ' . curl_error($this->_curlHandle));
}

$this->_responseCode = (int)curl_getinfo($this->_curlHandle, CURLINFO_HTTP_CODE);
$this->_responseCode = (int) curl_getinfo($this->_curlHandle, CURLINFO_HTTP_CODE);

curl_close($this->_curlHandle);

$logClass = $this->_root->Config->LogClass;
if ($this->_root->Config->DebugMode)

$this->logger->debug('Response JSON : ' . print_r($result, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('Response JSON', $result);

$response = json_decode($result);

if ($this->_root->Config->DebugMode)
}

// FIXME This can fail hard.
$response = json_decode($result);

$this->logger->debug('Decoded object : ' . print_r($response, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('Response object', $response);

}

$this->CheckResponseCode($response);

return $response;
Expand All @@ -145,12 +163,16 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu

$this->_requestUrl = $urlTool->GetFullUrl($restUrl);
$logClass = $this->_root->Config->LogClass;

$this->logger->debug('FullUrl : ' . $this->_requestUrl);
if ($this->_root->Config->DebugMode) {
$logClass::Debug('FullUrl', $this->_requestUrl);
}

$this->_curlHandle = curl_init($this->_requestUrl);
if ($this->_curlHandle === false){
$this->logger->error('Cannot initialize cURL session');

throw new Exception('Cannot initialize cURL session');
}

Expand Down Expand Up @@ -179,26 +201,38 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu
curl_setopt($this->_curlHandle, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
}

if ($this->_root->Config->DebugMode)

$this->logger->debug('RequestType : ' . $this->_requestType);
if ($this->_root->Config->DebugMode) {
$logClass::Debug('RequestType', $this->_requestType);
}

$httpHeaders = $this->GetHttpHeaders();
if ($idempotencyKey != null) array_push($httpHeaders, 'Idempotency-Key: ' . $idempotencyKey);
curl_setopt($this->_curlHandle, CURLOPT_HTTPHEADER, $httpHeaders);
if ($this->_root->Config->DebugMode)

$this->logger->debug('HTTP Headers : ' . print_r($httpHeaders, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('HTTP Headers', $httpHeaders);
}

if (!is_null($this->_requestData)) {

if ($this->_root->Config->DebugMode)
$this->logger->debug('RequestData object :' . print_r($this->_requestData, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('RequestData object', $this->_requestData);
}

// encode to json if needed
if (in_array(self::$_JSON_HEADER, $httpHeaders)) {

// FIXME This can also fail hard and is not checked.
$this->_requestData = json_encode($this->_requestData);
if ($this->_root->Config->DebugMode)

$this->logger->debug('RequestData JSON :' . print_r($this->_requestData, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('RequestData JSON', $this->_requestData);
}
}

curl_setopt($this->_curlHandle, CURLOPT_POSTFIELDS, $this->_requestData);
Expand All @@ -214,6 +248,8 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu
private function ReadResponseHeader($handle, $header) {

$logClass = $this->_root->Config->LogClass;

$this->logger->debug('Response headers :' . $header);
if ($this->_root->Config->DebugMode)
$logClass::Debug('Response headers', $header);

Expand Down
28 changes: 28 additions & 0 deletions MangoPay/MangoPayApi.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace MangoPay;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
* MangoPay API main entry point.
* Provides managers to connect, send and read data from MangoPay API
Expand Down Expand Up @@ -130,6 +133,11 @@ class MangoPayApi {
*/
public $DisputeDocuments;

/**
* @var LoggerInterface
*/
public $logger;

/**
* Constructor
*/
Expand Down Expand Up @@ -157,5 +165,25 @@ function __construct() {
$this->KycDocuments = new ApiKycDocuments($this);
$this->Disputes = new ApiDisputes($this);
$this->DisputeDocuments = new ApiDisputeDocuments($this);

// Setting default NullLogger
$this->logger = new NullLogger();

}

/**
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* @return LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}
}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Step 2 - Update your dependencies with Composer

The Library has been added into your dependencies and ready to be used.


License
-------------------------------------------------
MangopaySDK is distributed under MIT license, see LICENSE file.
Expand Down Expand Up @@ -175,3 +176,22 @@ class MangoPayService
}
}
```


Logging
-------
MangoPay uses the PSR3 LoggerInterface. You can provide your own logger to the API.
Here is a sample showing Monolog integration :

```php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

...

$logger = new Logger('sample-logger');
$logger->pushHandler(new StreamHandler($logConfig['path'], Logger::DEBUG));

$this->mangoPayApi = new MangoPay\MangoPayApi();
$this->mangoPayApi->setLogger($logger);
```
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"require": {
"php": ">=5.5.0",
"ext-curl": "*",
"ext-openssl": "*"
"ext-openssl": "*",
"psr/log": "^1.0"
},
"autoload": {
"psr-4": { "MangoPay\\": "" },
Expand Down

0 comments on commit 4fa9126

Please sign in to comment.