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 2222d57 commit 7d78475
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

### [1.1.0](https://github.com/webeweb/pexels-library/tree/v1.1.0) (2019-08-07)

- Add Logger interface into API provider

### [1.0.2](https://github.com/webeweb/pexels-library/tree/v1.0.2) (2019-08-02)

- Add buildConfiguration()
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 @@
"php": "^5.6|^7.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.0",
"psr/log": "^1.0",
"webeweb/core-library": "^5.0"
},
"require-dev": {
Expand Down
18 changes: 6 additions & 12 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Search photos

```php
// Create the API provider.
$provider = new APIProvider();
$provider->setAuthorization("YOUR API KEY");
$provider = new APIProvider("YOUR API KEY");

// Create a Search photos model.
$request = new SearchPhotosRequest();
Expand Down Expand Up @@ -59,8 +58,7 @@ Curated photos

```php
// Create the API provider.
$provider = new APIProvider();
$provider->setAuthorization("YOUR API KEY");
$provider = new APIProvider("YOUR API KEY");

// Create a Curated photo model.
$request = new CuratedPhotosRequest();
Expand All @@ -77,8 +75,7 @@ Get a photo

```php
// Create the API provider.
$provider = new APIProvider();
$provider->setAuthorization("YOUR API KEY");
$provider = new APIProvider("YOUR API KEY");

// Create a Get photo model.
$request = new GetPhotoRequest();
Expand All @@ -101,8 +98,7 @@ Search videos

```php
// Create the API provider.
$provider = new APIProvider();
$provider->setAuthorization("YOUR API KEY");
$provider = new APIProvider("YOUR API KEY");

// Create a Search videos model.
$request = new SearchVideosRequest();
Expand Down Expand Up @@ -165,8 +161,7 @@ Popular videos

```php
// Create the API provider.
$provider = new APIProvider();
$provider->setAuthorization("YOUR API KEY");
$provider = new APIProvider("YOUR API KEY");

// Create a Popular videos model.
$request = new PopularVideosRequest();
Expand All @@ -183,8 +178,7 @@ Get a video

```php
// Create the API provider.
$provider = new APIProvider();
$provider->setAuthorization("YOUR API KEY");
$provider = new APIProvider("YOUR API KEY");

// Create a Get video model.
$request = new GetVideoRequest();
Expand Down
58 changes: 55 additions & 3 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Exception;
use GuzzleHttp\Client;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use WBW\Library\Pexels\API\PaginateResponseInterface;
use WBW\Library\Pexels\API\SubstituteRequestInterface;
use WBW\Library\Pexels\Exception\APIException;
Expand Down Expand Up @@ -53,11 +54,23 @@ abstract class AbstractProvider {
*/
private $debug;

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

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

/**
Expand Down Expand Up @@ -114,11 +127,16 @@ private function callAPI($uri, array $queryData) {

try {

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

$client = new Client($config);

$method = "GET";
$options = 0 < count($queryData) ? ["query" => $queryData] : [];

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

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

$this->setLimit(intval($response->getHeaderLine("X-Ratelimit-Limit")));
$this->setRemaining(intval($response->getHeaderLine("X-Ratelimit-Remaining")));
Expand Down Expand Up @@ -196,6 +214,29 @@ public function getDebug() {
return $this->debug;
}

/**
* 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 authorization.
*
Expand All @@ -217,4 +258,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;
}
}
37 changes: 15 additions & 22 deletions 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\Pexels\Exception\APIException;
use WBW\Library\Pexels\Model\Request\CuratedPhotosRequest;
use WBW\Library\Pexels\Model\Request\GetPhotoRequest;
Expand Down Expand Up @@ -78,11 +79,13 @@ public function testBeforeReturnResponse() {
*/
public function testCuratedPhotos() {

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

// Set a Curated photos request mock.
$curatedPhotosRequest = new CuratedPhotosRequest();

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization, $logger);

try {

Expand Down Expand Up @@ -129,8 +132,7 @@ public function testGetPhoto() {
$getPhotoRequest = new GetPhotoRequest();
$getPhotoRequest->setId(1181292);

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand All @@ -155,8 +157,7 @@ public function testGetPhotoWithInvalidArgumentException() {
$getPhotoRequest = new GetPhotoRequest();
// $getPhotoRequest->setId(1181292);

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand All @@ -179,8 +180,7 @@ public function testGetVideo() {
$getVideoRequest = new GetVideoRequest();
$getVideoRequest->setId(1972034);

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand All @@ -205,8 +205,7 @@ public function testNextPageWithPhotosResponse() {
$searchPhotosRequest = new SearchPhotosRequest();
$searchPhotosRequest->setQuery("landscape");

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand All @@ -231,8 +230,7 @@ public function testNextPageWithVideosResponse() {
$searchVideosRequest = new SearchVideosRequest();
$searchVideosRequest->setQuery("landscape");

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand All @@ -256,8 +254,7 @@ public function testPopularVideos() {
// Set a Popular videos request mock.
$popularVideosRequest = new PopularVideosRequest();

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand All @@ -283,8 +280,7 @@ public function testPrevPageWithPhotosResponse() {
$searchPhotosRequest->setQuery("landscape");
$searchPhotosRequest->setPage(2);

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand All @@ -310,8 +306,7 @@ public function testPrevPageWithVideosResponse() {
$searchVideosRequest->setQuery("landscape");
$searchVideosRequest->setPage(2);

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand All @@ -337,8 +332,7 @@ public function testSearchPhotos() {
$searchPhotoRequest = new SearchPhotosRequest();
$searchPhotoRequest->setQuery("github");

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand All @@ -363,8 +357,7 @@ public function testSearchVideos() {
$searchVideosRequest = new SearchVideosRequest();
$searchVideosRequest->setQuery("github");

$obj = new APIProvider();
$obj->setAuthorization($this->authorization);
$obj = new APIProvider($this->authorization);

try {

Expand Down

0 comments on commit 7d78475

Please sign in to comment.