Skip to content

Commit

Permalink
Merge pull request #3 from swarming/master
Browse files Browse the repository at this point in the history
Added unit tests.  Code improvements.
  • Loading branch information
Garth Brantley authored Jul 16, 2016
2 parents 80025a4 + 717873b commit 2e529ae
Show file tree
Hide file tree
Showing 52 changed files with 6,256 additions and 343 deletions.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"authors": [
{
"name": "Swarming Tech",
"name": "Swarming Technology",
"homepage": "http://swarmingtech.com"
},
{
Expand All @@ -20,11 +20,10 @@
},
"require": {
"php": ">=5.5",
"guzzlehttp/guzzle": "~6.0.1|~6.1|~6.2",
"monolog/monolog": "1.16.0"
"guzzlehttp/guzzle": "~6.0.1|~6.1|~6.2"
},
"require-dev": {
"phpunit/phpunit": "~4.0|~5.0"
"monolog/monolog": "^1.16.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions src/SubscribePro/Exception/EntityInvalidDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace SubscribePro\Exception;

class EntityInvalidDataException extends \InvalidArgumentException
{
}
45 changes: 45 additions & 0 deletions src/SubscribePro/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,51 @@

namespace SubscribePro\Exception;

use Exception;
use Psr\Http\Message\ResponseInterface;

class HttpException extends \RuntimeException
{
/**
* @var \Psr\Http\Message\ResponseInterface
*/
protected $response;

/**
* @param \Psr\Http\Message\ResponseInterface $response
* @param int $code
* @param Exception|null $previous
*/
public function __construct(ResponseInterface $response, $code = 0, Exception $previous = null)
{
parent::__construct($this->getErrorMessage($response), $code, $previous);

$this->response = $response;
}

/**
* @return int
*/
public function getStatusCode()
{
return $this->response->getStatusCode();
}

/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function getResponse()
{
return $this->response;
}

/**
* @param \Psr\Http\Message\ResponseInterface $response
* @return string
*/
protected function getErrorMessage(ResponseInterface $response)
{
$errorBody = json_decode((string)$response->getBody(), true);
return !empty($errorBody['message']) ? $errorBody['message'] : $response->getReasonPhrase();
}
}
58 changes: 37 additions & 21 deletions src/SubscribePro/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,30 @@ class Http
public function __construct($app)
{
$this->app = $app;
$this->handlerStack = HandlerStack::create();
$this->handlerStack = $this->createHandlerStack();
}

protected function initClient()
/**
* @return \GuzzleHttp\Client
*/
protected function createClient()
{
$this->client = new Client([
return new Client([
'base_uri' => $this->baseUrl,
'handler' => $this->handlerStack,
RequestOptions::HTTP_ERRORS => false,
RequestOptions::AUTH => [$this->app->getClientId(), $this->app->getClientSecret()]
]);
}

/**
* @return \GuzzleHttp\HandlerStack
*/
protected function createHandlerStack()
{
return HandlerStack::create();
}

/**
* @return \GuzzleHttp\HandlerStack
*/
Expand All @@ -81,14 +92,15 @@ public function getHandlerStack()
* @param string|null $fileName
* @param string|null $lineFormat
* @param string|null $messageFormat
* @param string $logLevel
* @param string|null $logLevel
* @return Http
*/
public function initDefaultLogger($fileName = null, $lineFormat = null, $messageFormat = null, $logLevel = LogLevel::INFO)
public function initDefaultLogger($fileName = null, $lineFormat = null, $messageFormat = null, $logLevel = null)
{
$fileName = $fileName ?: static::DEFAULT_LOG_FILE_NAME;
$lineFormat = $lineFormat ?: static::DEFAULT_LOG_LINE_FORMAT;
$messageFormat = $messageFormat ?: static::DEFAULT_LOG_MESSAGE_FORMAT;
$logLevel = $logLevel ?: LogLevel::INFO;

$logHandler = new RotatingFileHandler($fileName);
$logHandler->setFormatter(new LineFormatter($lineFormat, null, true));
Expand All @@ -104,18 +116,32 @@ public function initDefaultLogger($fileName = null, $lineFormat = null, $message
*/
public function addLogger($logger, $messageFormatter, $logLevel = LogLevel::INFO)
{
$this->handlerStack->push(Middleware::log($logger, $messageFormatter, $logLevel), 'logger');
$this->getHandlerStack()
->push(
$this->createMiddlewareLogCallback($logger, $messageFormatter, $logLevel),
'logger'
);
return $this;
}

/**
* @param bool $force
* @param \Psr\Log\LoggerInterface $logger
* @param \GuzzleHttp\MessageFormatter $messageFormatter
* @param string $logLevel
* @return callable
*/
protected function createMiddlewareLogCallback($logger, $messageFormatter, $logLevel = LogLevel::INFO)
{
return Middleware::log($logger, $messageFormatter, $logLevel);
}

/**
* @return \GuzzleHttp\Client
*/
public function getClient($force = false)
protected function getClient()
{
if (null === $this->client || $force) {
$this->initClient();
if (null === $this->client) {
$this->client = $this->createClient();
}
return $this->client;
}
Expand Down Expand Up @@ -187,16 +213,6 @@ protected function processResponse($response)
return !empty($body) ? json_decode($body, true) : $response->getStatusCode();
}

throw new HttpException($this->getErrorMessage($response), $response->getStatusCode());
}

/**
* @param \Psr\Http\Message\ResponseInterface $response
* @return string
*/
protected function getErrorMessage($response)
{
$errorBody = json_decode((string)$response->getBody(), true);
return !empty($errorBody['message']) ? $errorBody['message'] : $response->getReasonPhrase();
throw new HttpException($response);
}
}
Loading

0 comments on commit 2e529ae

Please sign in to comment.