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 8bec5b8 commit 4f52201
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 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.1.0](https://github.com/webeweb/haveibeenpwned-library/tree/v2.1.0) (2019-08-07)

- Add Logger interface into API provider

### [2.0.1](https://github.com/webeweb/haveibeenpwned-library/tree/v2.0.1) (2019-08-01)

- Optimize API v3 calls
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"php": "^5.6|^7.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.0",
"psr/log": "^1.0",
"webeweb/core-library": "^5.0"
},
"require-dev": {
Expand Down
6 changes: 4 additions & 2 deletions src/Provider/APIv3Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace WBW\Library\HaveIBeenPwned\Provider;

use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use WBW\Library\HaveIBeenPwned\Exception\APIException;
use WBW\Library\HaveIBeenPwned\Model\Request\BreachedAccountRequest;
use WBW\Library\HaveIBeenPwned\Model\Request\BreachesRequest;
Expand Down Expand Up @@ -45,9 +46,10 @@ class APIv3Provider extends AbstractProvider {
* Constructor.
*
* @param string|null $apiKey The API key.
* @param LoggerInterface|null $logger The logger.
*/
public function __construct($apiKey = null) {
parent::__construct();
public function __construct($apiKey = null, LoggerInterface $logger = null) {
parent::__construct($logger);
$this->setApiKey($apiKey);
}

Expand Down
56 changes: 53 additions & 3 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use WBW\Library\HaveIBeenPwned\API\SubstituteRequestInterface;
use WBW\Library\HaveIBeenPwned\Exception\APIException;
use WBW\Library\HaveIBeenPwned\Model\AbstractRequest;
Expand Down Expand Up @@ -42,11 +43,21 @@ abstract class AbstractProvider {
*/
private $debug;

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

/**
* Constructor.
*
* @param LoggerInterface|null $logger The logger.
*/
public function __construct() {
public function __construct(LoggerInterface $logger = null) {
$this->setDebug(false);
$this->setLogger($logger);
}

/**
Expand Down Expand Up @@ -112,14 +123,19 @@ protected function callAPI(AbstractRequest $request, array $queryData, $endpoint

$host = null === $endpointPath ? self::ENDPOINT_PATH . $this->getEndpointVersion() : $endpointPath;

$client = new Client($this->buildConfiguration($host, $apiKey));
$config = $this->buildConfiguration($host, $apiKey);

$client = new Client($config);

$method = "GET";
$uri = substr($this->buildResourcePath($request), 1);
$options = [
"query" => $queryData,
];

$response = $client->request("GET", $uri, $options);
$this->log(sprintf("Call HaveIBeenPwned API %s %s", $method, $uri), ["config" => $config, "options" => $options]);

$response = $client->request($method, $uri, $options);

return $response->getBody()->getContents();
} catch (InvalidArgumentException $ex) {
Expand Down Expand Up @@ -151,6 +167,29 @@ public function getDebug() {
*/
abstract public function getEndpointVersion();

/**
* Get the logger.
*
* @return LoggerInterface Returns the logger.
*/
public function getLogger() {
return $this->logger;
}

/**
* 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 debug.
*
Expand All @@ -161,4 +200,15 @@ public function setDebug($debug) {
$this->debug = $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;
}
}
6 changes: 5 additions & 1 deletion tests/Provider/APIv1ProviderTest.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\HaveIBeenPwned\Exception\APIException;
use WBW\Library\HaveIBeenPwned\Model\Request\BreachedAccountRequest;
use WBW\Library\HaveIBeenPwned\Model\Response\BreachesResponse;
Expand Down Expand Up @@ -43,11 +44,14 @@ protected function setUp() {
*/
public function testBreachedAccount() {

// Set a Logger mock.
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();

// Set a Breached account request mock.
$breachedAccountRequest = new BreachedAccountRequest();
$breachedAccountRequest->setAccount("john.doe@gmail.com");

$obj = new APIv1Provider();
$obj = new APIv1Provider($logger);

try {

Expand Down

0 comments on commit 4f52201

Please sign in to comment.