From b059c3aa9f45e852aa856aca70f93a3778d47655 Mon Sep 17 00:00:00 2001 From: webeweb Date: Wed, 7 Aug 2019 18:29:30 +0200 Subject: [PATCH] Add Logger interface into API provider --- CHANGELOG.md | 4 +++ composer.json | 1 + src/Provider/AbstractProvider.php | 52 ++++++++++++++++++++++++++++-- tests/Provider/APIProviderTest.php | 6 +++- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b758d0a..b4076b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ CHANGELOG ========= +### [2.5.0](https://github.com/webeweb/smsmode-library/tree/v2.5.0) (2019-08-07) + +- Add Logger interface into API provider + ### [2.4.3](https://github.com/webeweb/smsmode-library/tree/v2.4.3) (2019-08-02) - Add buildConfiguration() method diff --git a/composer.json b/composer.json index 6558c33..242bac3 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "ext-iconv": "*", "ext-json": "*", "guzzlehttp/guzzle": "^6.0", + "psr/log": "^1.0", "webeweb/core-library": "^5.0" }, "require-dev": { diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 3d8c22d..07978db 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -14,6 +14,7 @@ use Exception; use GuzzleHttp\Client; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use WBW\Library\SMSMode\Exception\APIException; use WBW\Library\SMSMode\Model\AbstractRequest; use WBW\Library\SMSMode\Model\Authentication; @@ -49,6 +50,13 @@ abstract class AbstractProvider { */ private $debug; + /** + * Logger. + * + * @var LoggerInterface + */ + private $logger; + /** * Request normalizer. * @@ -60,10 +68,12 @@ abstract class AbstractProvider { * Constructor. * * @param Authentication $authentication The authentication. + * @param LoggerInterface|null $logger The logger. */ - public function __construct(Authentication $authentication) { + public function __construct(Authentication $authentication, LoggerInterface $logger = null) { $this->setAuthentication($authentication); $this->setDebug(false); + $this->setLogger($logger); $this->setRequestNormalizer(new RequestNormalizer()); } @@ -98,7 +108,9 @@ protected function callAPI(AbstractRequest $request, array $queryData, array $po try { - $client = new Client($this->buildConfiguration()); + $config = $this->buildConfiguration(); + + $client = new Client($config); $method = 0 === count($postData) ? "GET" : "POST"; $uri = substr($request->getResourcePath(), 1); @@ -107,6 +119,8 @@ protected function callAPI(AbstractRequest $request, array $queryData, array $po "form_params" => $postData, ]; + $this->log(sprintf("Call sMsmode API %s %s", $method, $uri), ["config" => $config, "options" => $options]); + $response = $client->request($method, $uri, $options); return utf8_encode($response->getBody()->getContents()); @@ -137,6 +151,15 @@ public function getDebug() { return $this->debug; } + /** + * Get the logger. + * + * @return LoggerInterface Returns the logger. + */ + public function getLogger() { + return $this->logger; + } + /** * Get the request normalizer. * @@ -146,6 +169,20 @@ public function getRequestNormalizer() { return $this->requestNormalizer; } + /** + * Log. + * + * @param string $message The message. + * @param array $context The context. + * @return AbstractProvider Returns this provider. + */ + protected function log($message, array $context) { + if (null !== $this->getLogger()) { + $this->getLogger()->info($message, $context); + } + return $this; + } + /** * Set the authentication. * @@ -168,6 +205,17 @@ public function setDebug($debug) { return $this; } + /** + * Set the logger. + * + * @param LoggerInterface|null $logger The logger + * @return AbstractProvider Returns this provider + */ + protected function setLogger(LoggerInterface $logger = null) { + $this->logger = $logger; + return $this; + } + /** * Set the request normalizer. * diff --git a/tests/Provider/APIProviderTest.php b/tests/Provider/APIProviderTest.php index df98ac6..6df83dd 100644 --- a/tests/Provider/APIProviderTest.php +++ b/tests/Provider/APIProviderTest.php @@ -13,6 +13,7 @@ use Exception; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use WBW\Library\SMSMode\Model\Authentication; use WBW\Library\SMSMode\Model\Request\AccountBalanceRequest; use WBW\Library\SMSMode\Model\Request\AddingContactRequest; @@ -81,10 +82,13 @@ protected function setUp() { */ public function testAccountBalance() { + // Set a Logger mock. + $logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); + // Set an Account balance request mock. $arg = new AccountBalanceRequest(); - $obj = new APIProvider($this->authentication); + $obj = new APIProvider($this->authentication, $logger); $res = $obj->accountBalance($arg); $this->assertInstanceOf(AccountBalanceResponse::class, $res);