Skip to content

Commit

Permalink
Merge pull request #10 from inextensodigital/dev
Browse files Browse the repository at this point in the history
dev => master
  • Loading branch information
aubryfr authored Apr 14, 2017
2 parents b25494d + e9e982b commit e3e657f
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 42 deletions.
112 changes: 82 additions & 30 deletions src/DataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace ActiveCollab\Quickbooks;

use ActiveCollab\Quickbooks\Quickbooks;
use ActiveCollab\Quickbooks\Data\Entity;
use Guzzle\Service\Client as GuzzleClient;
use ActiveCollab\Quickbooks\Data\QueryResponse;
use ActiveCollab\Quickbooks\Exception\FaultException;
use DateTime;
use Guzzle\Http\Exception\BadResponseException;
use League\OAuth1\Client\Credentials\TokenCredentials;
use Guzzle\Service\Client as GuzzleClient;
use League\OAuth1\Client\Credentials\ClientCredentials;
use DateTime;
use League\OAuth1\Client\Credentials\TokenCredentials;

class DataService
{
Expand All @@ -32,7 +32,7 @@ class DataService

/**
* Construct data service
*
*
* @param string $consumer_key
* @param string $consumer_key_secret
* @param string $access_token
Expand All @@ -46,11 +46,11 @@ public function __construct($consumer_key, $consumer_key_secret, $access_token,
$this->access_token = $access_token;
$this->access_token_secret = $access_token_secret;
$this->realmId = $realmId;
}
}

/**
* Return api url
*
*
* @return string
*/
public function getApiUrl()
Expand All @@ -60,7 +60,7 @@ public function getApiUrl()

/**
* Return http client
*
*
* @return GuzzleClient
*/
public function createHttpClient()
Expand All @@ -70,7 +70,7 @@ public function createHttpClient()

/**
* Return oauth server
*
*
* @return Quickbooks
*/
public function createServer()
Expand All @@ -84,7 +84,7 @@ public function createServer()

/**
* Return token credentials
*
*
* @return TokenCredentials
*/
public function getTokenCredentials()
Expand All @@ -98,8 +98,10 @@ public function getTokenCredentials()

/**
* Set user agent
*
*
* @param string|null $user_agent
*
* @return static
*/
public function setUserAgent($user_agent = null)
{
Expand All @@ -110,7 +112,7 @@ public function setUserAgent($user_agent = null)

/**
* Return user agent
*
*
* @return string
*/
public function getUserAgent()
Expand All @@ -120,8 +122,10 @@ public function getUserAgent()

/**
* Set entity
*
*
* @param string $entity
*
* @return static
*/
public function setEntity($entity)
{
Expand All @@ -132,7 +136,9 @@ public function setEntity($entity)

/**
* Return entity url
*
*
* @param string $slug
*
* @return string
*/
public function getRequestUrl($slug)
Expand All @@ -142,7 +148,7 @@ public function getRequestUrl($slug)

/**
* Send create request
*
*
* @param array $payload
* @return Entity
*/
Expand All @@ -155,7 +161,7 @@ public function create(array $payload)

/**
* Send read request
*
*
* @param int $id
* @return Entity
*/
Expand All @@ -170,7 +176,7 @@ public function read($id)

/**
* Send update request
*
*
* @param array $payload
* @return Entity
*/
Expand All @@ -185,7 +191,7 @@ public function update(array $payload)

/**
* Send delete request
*
*
* @param array $payload
* @return null
*/
Expand All @@ -200,29 +206,39 @@ public function delete(array $payload)

/**
* Send query request
*
* @param string|null $query
*
* @param string|null $query
* @param int|null $minorVersion
*
* @return QueryResponse
*/
public function query($query = null)
public function query($query = null, $minorVersion = null)
{
if ($query === null) {
$query = "select * from {$this->entity}";
}

$uri = $this->getRequestUrl('query') . '?query=' . urlencode($query);

if (null !== $minorVersion) {
$this->validateMinorVersion($minorVersion);
$uri .= '&minorversion=' . $minorVersion;
}

$response = $this->request('GET', $uri);

return new QueryResponse($response['QueryResponse']);
}

/**
* Send CDC request
*
* @param array $entities
* @param DateTime $changed_since
*
* @param array $entities
* @param DateTime $changed_since
*
* @return array
*
* @throws \Exception
*/
public function cdc(array $entities, DateTime $changed_since)
{
Expand Down Expand Up @@ -254,12 +270,12 @@ public function cdc(array $entities, DateTime $changed_since)

/**
* Return headers for request
*
*
* @param string $method
* @param string $uri
* @return array
*/
public function getHeaders($method, $uri)
public function getHeaders($method, $uri)
{
$server = $this->createServer();

Expand All @@ -277,15 +293,15 @@ public function getHeaders($method, $uri)

/**
* Request
*
*
* @param string $method
* @param string $uri
* @param string|array $body
* @return array
* @throws \Exception
*/
public function request($method, $uri, array $body = null)
{
{
$client = $this->createHttpClient();

$headers = $this->getHeaders($method, $uri);
Expand All @@ -298,13 +314,49 @@ public function request($method, $uri, array $body = null)
return $client->createRequest($method, $uri, $headers, $body)->send()->json();
} catch (BadResponseException $e) {
$response = $e->getResponse();
$body = $response->getBody();
$statusCode = $response->getStatusCode();

$body = $response->json();
if (isset($body['Fault'])) {
throw $this->createFaultException($body, $e);
}

$body = $response;
throw new \Exception(
"Received error [$body] with status code [$statusCode] when sending request."
);
}
}

}
/**
* Validates the type for $minorVersion
*
* @param int $minorVersion
*
* @return self
*
* @throws \InvalidArgumentException
*/
private function validateMinorVersion($minorVersion)
{
if (!is_int($minorVersion)) {
throw new \InvalidArgumentException(sprintf('Invalid type for "$minorVersion" : expected "int", got "%s".', gettype($minorVersion)));
}
return $this;
}

/**
* @param array $body
* @param \Exception $previous
*
* @return FaultException
*/
private function createFaultException(array $body, \Exception $previous = null)
{
$errors = [];
if (isset($body['Fault']['Error'])) {
$errors = $body['Fault']['Error'];
}
return new FaultException("Fault response : " . json_encode($errors), 0, $previous, $errors);
}
}
56 changes: 56 additions & 0 deletions src/Exception/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace ActiveCollab\Quickbooks\Exception;

class Error
{
/**
* @var string
*/
private $message;

/**
* @var string
*/
private $detail;

/**
* @var int
*/
private $code;

/**
* @var mixed
*/
private $element;

/**
* @param string $message
* @param string $detail
* @param string $code
* @param string $element
*/
public function __construct($message, $detail, $code, $element)
{
$this->message = $message;
$this->detail = $detail;
$this->code = (int) $code;
$this->element = $element;
}

/**
* @return string
*/
public function getDetail()
{
return $this->detail;
}

/**
* @return int
*/
public function getCode()
{
return $this->code;
}
}
42 changes: 42 additions & 0 deletions src/Exception/FaultException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace ActiveCollab\Quickbooks\Exception;

class FaultException extends \RuntimeException
{
/**
* @var array
*/
private $errors;

/**
* {@inheritDoc}
*
* @param array $errors
*/
public function __construct ($message = null, $code = null, $previous = null, $errors = []) {
parent::__construct ($message, $code, $previous);
$this->errors = [];
$this->setErrors($errors);
}

private function setErrors($errors)
{
foreach ($errors as $error) {
$this->errors[] = new Error(
$error['Message'],
$error['Detail'],
$error['code'],
isset($error['element']) ? $error['element'] : null
);
}
}

/**
* @return array
*/
public function getErrors()
{
return $this->errors;
}
}
Loading

0 comments on commit e3e657f

Please sign in to comment.