Skip to content

Commit

Permalink
Add Logger interface into API provider
Browse files Browse the repository at this point in the history
  • Loading branch information
webeweb committed Aug 7, 2019
1 parent f6070c4 commit b059c3a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"ext-iconv": "*",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.0",
"psr/log": "^1.0",
"webeweb/core-library": "^5.0"
},
"require-dev": {
Expand Down
52 changes: 50 additions & 2 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +50,13 @@ abstract class AbstractProvider {
*/
private $debug;

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

/**
* Request normalizer.
*
Expand All @@ -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());
}

Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand Down
6 changes: 5 additions & 1 deletion tests/Provider/APIProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b059c3a

Please sign in to comment.