Browse Source

Log requests to console

master
Rodrigo Gonzalez 4 years ago
parent
commit
8a4858f5dc
  1. 4
      composer.json
  2. 13
      src/Appsflyer.php
  3. 28
      src/Config.php
  4. 10
      src/ConfigInterface.php
  5. 12
      src/Core/Api.php

4
composer.json

@ -15,7 +15,9 @@
], ],
"require": { "require": {
"php": ">=5.6", "php": ">=5.6",
"guzzlehttp/guzzle": "~6.0" "guzzlehttp/guzzle": "~6.0",
"monolog/monolog": "^2.1",
"gmponos/guzzle_logger": "^2.0"
}, },
"autoload": { "autoload": {
"psr-4": { "Jlorente\\Appsflyer\\": "src/" } "psr-4": { "Jlorente\\Appsflyer\\": "src/" }

13
src/Appsflyer.php

@ -52,9 +52,9 @@ class Appsflyer
* @param string $apiToken * @param string $apiToken
* @return void * @return void
*/ */
public function __construct($devKey = null, $apiToken = null) public function __construct($devKey = null, $apiToken = null, $logRequests = false)
{ {
$this->config = new Config(self::VERSION, $devKey, $apiToken); $this->config = new Config(self::VERSION, $devKey, $apiToken, $logRequests);
} }
/** /**
@ -62,11 +62,12 @@ class Appsflyer
* *
* @param string $devKey * @param string $devKey
* @param string $apiToken * @param string $apiToken
* @param bool $logRequests
* @return \Jlorente\Appsflyer\Appsflyer * @return \Jlorente\Appsflyer\Appsflyer
*/ */
public static function make($devKey = null, $apiToken = null) public static function make($devKey = null, $apiToken = null, $logRequests = false)
{ {
return new static($devKey, $apiToken); return new static($devKey, $apiToken, $logRequests);
} }
/** /**
@ -118,9 +119,9 @@ class Appsflyer
* @param string $devKey * @param string $devKey
* @return $this * @return $this
*/ */
public function setApiKey($devKey) public function setDevKey($devKey)
{ {
$this->config->setApiKey($devKey); $this->config->setDevKey($devKey);
return $this; return $this;
} }

28
src/Config.php

@ -50,6 +50,13 @@ class Config implements ConfigInterface
*/ */
protected $accountId; protected $accountId;
/**
* Define if we log requests or not
*
* @var bool
*/
protected $logRequests;
/** /**
* Constructor. * Constructor.
* *
@ -59,7 +66,7 @@ class Config implements ConfigInterface
* @return void * @return void
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function __construct($version, $devKey, $apiToken) public function __construct($version, $devKey, $apiToken, $logRequests = false)
{ {
$this->setVersion($version); $this->setVersion($version);
@ -74,6 +81,8 @@ class Config implements ConfigInterface
if (!$this->devKey) { if (!$this->devKey) {
throw new \RuntimeException('The Appsflyer dev_key is not defined!'); throw new \RuntimeException('The Appsflyer dev_key is not defined!');
} }
$this->setLogRequests($logRequests);
} }
/** /**
@ -130,4 +139,21 @@ class Config implements ConfigInterface
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
public function getLogRequests()
{
return $this->logRequests;
}
/**
* {@inheritdoc}
*/
public function setLogRequests($logRequests)
{
$this->logRequests = $logRequests;
return $this;
}
} }

10
src/ConfigInterface.php

@ -66,4 +66,14 @@ interface ConfigInterface
* @return $this * @return $this
*/ */
public function setApiToken($apiVersion); public function setApiToken($apiVersion);
/**
* Sets the property to log or not the requests
*/
public function setLogRequests($logRequests);
/**
* Gets the property to log or not the requests
*/
public function getLogRequests();
} }

12
src/Core/Api.php

@ -33,6 +33,10 @@ use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
use GuzzleLogMiddleware\LogMiddleware;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
abstract class Api implements ApiInterface abstract class Api implements ApiInterface
{ {
@ -197,7 +201,13 @@ abstract class Api implements ApiInterface
return (int) pow(2, $retries) * 1000; return (int) pow(2, $retries) * 1000;
})); }));
return $stack; if ($this->config->getLogRequests()) {
$logger = new Logger('stderr');
$logger->pushHandler(new StreamHandler('php://stderr'));
$stack->push(new LogMiddleware($logger));
} }
return $stack;
}
} }

Loading…
Cancel
Save