Skip to content

Commit

Permalink
Expose Guzzle Client, code cleanup (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
robwittman authored Aug 10, 2019
1 parent 4fbdef2 commit a7e7ba9
Show file tree
Hide file tree
Showing 37 changed files with 167 additions and 115 deletions.
3 changes: 3 additions & 0 deletions src/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function __construct(array $options = array())
* Set our Client instance
*
* @param Client $httpHandler
* @return $this
*/
public function setHttpHandler(Client $httpHandler)
{
Expand All @@ -82,6 +83,7 @@ public function getHttpHandler()
* Set our LoggerInterface
*
* @param LoggerInterface $logger
* @return $this
*/
public function setLogger(LoggerInterface $logger)
{
Expand All @@ -104,6 +106,7 @@ public function getMyshopifyDomain()
* a new client, with new details
*
* @param string $domain
* @return $this
*/
public function setMyshopifyDomain($domain)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function getApiSecret()
* new client instance
*
* @param string $accessToken
* @return $this
*/
public function setAccessToken($accessToken)
{
Expand All @@ -94,6 +95,7 @@ public function getAccessToken()
* Set our persistent storage interface
*
* @param PersistentStorageInterface $storage
* @return $this
*/
public function setStorageInterface(PersistentStorageInterface $storage)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Service/AbandonedCheckoutsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class AbandonedCheckoutsService extends AbstractService
{
/**
* List all abandonded checkouts
* List all abandoned checkouts
*
* @link https://help.shopify.com/api/reference/abandoned_checkouts#index
* @param array $params
Expand Down
62 changes: 48 additions & 14 deletions src/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,93 @@

namespace Shopify\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Shopify\ApiInterface;
use Shopify\Object\AbstractObject;
use Shopify\Inflector;

abstract class AbstractService
{
private $api;
private $mapper;
/**
* Instantiated Guzzle Client for requests
* @var Client
*/
private $client;

/**
* The last API response from Shopify
* @var Response|null
*/
private $lastResponse;

const REQUEST_METHOD_GET = 'GET';
const REQUEST_METHOD_POST = 'POST';
const REQUEST_METHOD_PUT = 'PUT';
const REQUEST_METHOD_DELETE = 'DELETE';

public static function factory(ApiInterface $api)
public static function factory(ApiInterface $api): AbstractService
{
return new static($api);
}

public function __construct(ApiInterface $api)
{
$this->api = $api;
$this->client = $api->getHttpHandler();
}

public function getApi()
/**
* Get the client instance
*
* @return Client
*/
public function getClient()
{
return $this->api;
return $this->client;
}

public function request($endpoint, $method = self::REQUEST_METHOD_GET, array $params = array())
/**
* @param $endpoint
* @param string $method
* @param array $params
* @return mixed
*/
public function request($endpoint, $method = self::REQUEST_METHOD_GET, array $params = [])
{
$request = $this->createRequest($endpoint, $method);
return $this->send($request, $params);
return $this->send(new Request($method, $endpoint), $params);
}

/**
* @param $endpoint
* @param string $method
* @return Request
*/
public function createRequest($endpoint, $method = self::REQUEST_METHOD_GET)
{
return new Request($method, $endpoint);
}

/**
* Get the last response from Shopify
* @return Response
*/
public function getLastResponse()
{
return $this->lastResponse;
}

public function send(Request $request, array $params = array())
{
$handler = $this->getApi()->getHttpHandler();
$args = array();
if ($request->getMethod() === 'GET') {
$args['query'] = $params;
} else {
$args['json'] = $params;
}
$this->lastResponse = $handler->send($request, $args);
return json_decode($this->lastResponse->getBody()->getContents(), true);
$this->lastResponse = $this->client->send($request, $args);
return json_decode(
$this->lastResponse->getBody()->getContents(),
true
);
}

public function createObject($className, $data)
Expand Down
2 changes: 1 addition & 1 deletion src/Service/ApplicationChargeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function create(ApplicationCharge &$applicationCharge)
$data = $applicationCharge->exportData();
$endpoint = '/admin/application_charges.json';
$response = $this->request(
'/admin/application_charges.json', 'POST', array(
$endpoint, 'POST', array(
'application_charge' => $data
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/Service/ArticleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function all($blogId, array $params = array())
}

/**
* Receive acount of all Articles
* Receive a count of all Articles
*
* @link https://help.shopify.com/api/reference/article#count
* @param integer $blogId
Expand Down Expand Up @@ -115,7 +115,7 @@ public function delete($blogId, Article &$article)
* Get a list of all the authors
*
* @link https://help.shopify.com/api/reference/article#authors
* @return arrays
* @return array
*/
public function authors()
{
Expand Down
8 changes: 4 additions & 4 deletions src/Service/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AssetService extends AbstractService
* @link https://help.shopify.com/api/reference/asset#index
* @param integer $themeId
* @param array $params
* @return Asset[]
* @throws ShopifySdkException
*/
public function all($themeId, array $params = array())
{
Expand All @@ -26,7 +26,7 @@ public function all($themeId, array $params = array())
* @link https://help.shopify.com/api/reference/asset#show
* @param integer $themeId
* @param array $params
* @return Article
* @throws ShopifySdkException
*/
public function get($themeId, array $params = array())
{
Expand All @@ -39,7 +39,7 @@ public function get($themeId, array $params = array())
* @link https://help.shopify.com/api/reference/asset#update
* @param integer $themeId
* @param Asset $asset
* @return
* @throws ShopifySdkException
*/
public function put($themeId, Asset $asset)
{
Expand All @@ -52,7 +52,7 @@ public function put($themeId, Asset $asset)
* @link https://help.shopify.com/api/reference/asset#destroy
* @param integer $themeId
* @param Asset $asset
* @return void
* @throws ShopifySdkException
*/
public function delete($themeId, Asset $asset)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Service/BlogService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function all(array $params = array())
}

/**
* Receivea count of all blogs
* Receive a count of all blogs
*
* @link https://help.shopify.com/api/reference/blog#count
* @return integer
Expand Down Expand Up @@ -77,19 +77,19 @@ public function create(Blog &$blog)
*
* @link https://help.shopify.com/api/reference/blog#update
* @param Blog $blog
* @return void
* @throws ShopifySdkException
*/
public function update(Blog &$blog)
{
throw new ShopifySdkException('BlogService::update() not implemented');
}

/**
* Removea blog from Database
* Remove a blog from Database
*
* @link https://help.shopify.com/api/reference/blog#destroy
* @param Blog $blog
* @return void
* @throws ShopifySdkException
*/
public function delete(Blog $blog)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Service/CollectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function all(array $params = array())
public function count(array $params = array())
{
$endpoint = '/collects/count.json';
$data = $this->request($endpoint);
$data = $this->request($endpoint, $params);
return $data['count'];
}

Expand Down
12 changes: 6 additions & 6 deletions src/Service/CommentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CommentService extends AbstractService
* @param array $params
* @return Comment[]
*/
public function all(array $params = array())
public function all(array $params = [])
{
$endpoint = '/comments.json';
$response = $this->request($endpoint, 'GET', $params);
Expand All @@ -27,10 +27,10 @@ public function all(array $params = array())
* @param array $params
* @return integer
*/
public function count(array $params = array())
public function count(array $params = [])
{
$endpoint = '/comments/count.json';
$response = $this->request($endpoint);
$response = $this->request($endpoint, 'GET', $params);
return $response['count'];
}

Expand All @@ -42,10 +42,10 @@ public function count(array $params = array())
* @param array $params
* @return Comment
*/
public function get($commentId, array $params = array())
public function get($commentId, array $params = [])
{
$endpoint = '/comments/'.$commentId.'.json';
$response = $this->request($endpoint);
$response = $this->request($endpoint, 'GET', $params);
return $this->createObject(Comment::class, $response['comment']);
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public function remove(Comment &$comment)
}

/**
* Retore a comment
* Restore a comment
*
* @link https://help.shopify.com/api/reference/comment#restore
* @param Comment $comment
Expand Down
7 changes: 4 additions & 3 deletions src/Service/CountryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CountryService extends AbstractService
* @param array $params
* @return Country[]
*/
public function all(array $params = array())
public function all(array $params = [])
{
$endpoint = '/countries.json';
$response = $this->request($endpoint, 'GET', $params);
Expand Down Expand Up @@ -41,7 +41,7 @@ public function count()
* @param array $fields
* @return Country
*/
public function get($countryId, array $fields = array())
public function get($countryId, array $fields = [])
{
$params = array();
if (!empty($fields)) {
Expand Down Expand Up @@ -99,6 +99,7 @@ public function update(Country &$country)
*/
public function delete(Country $country)
{
return $this->request('/countries/'.$country->id.'.json', 'DELETE');
$this->request('/countries/'.$country->id.'.json', 'DELETE');
return;
}
}
3 changes: 1 addition & 2 deletions src/Service/CustomCollectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ public function update(CustomCollection &$customCollection)
public function delete(CustomCollection &$customCollection)
{
$endpoint = '/custom_collections/'.$customCollection->getId().'.json';
$request = $this->createRequest($endpoint, static::REQUEST_METHOD_DELETE);
$response = $this->send($request);
$this->request($endpoint, static::REQUEST_METHOD_DELETE);
return;
}
}
3 changes: 2 additions & 1 deletion src/Service/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function update(Customer &$customer)
public function delete(Customer $customer)
{
$endpoint = '/customers/'.$customer->id.'.json';
$response = $this->request($endpoint, 'DELETE');
$this->request($endpoint, 'DELETE');
return;
}

Expand All @@ -134,6 +134,7 @@ public function accountActivationUrl($customerId)
* @todo Return CustomInvite, instead of raw object
* @link https://help.shopify.com/api/reference/customer#send_invite
* @param integer $customerId
* @param CustomerInvite $invite
* @return CustomerInvite
*/
public function sendInvite($customerId, CustomerInvite $invite = null)
Expand Down
2 changes: 1 addition & 1 deletion src/Service/DiscountCodeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DiscountCodeService extends AbstractService
* @param array $params
* @return DiscountCode[]
*/
public function all($priceRuleId, array $params = array())
public function all($priceRuleId, array $params = [])
{
$endpoint = '/price_rules/'.$priceRuleId.'/discount_codes.json';
$response = $this->request($endpoint, 'GET', $params);
Expand Down
4 changes: 2 additions & 2 deletions src/Service/EventService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class EventService extends AbstractService
* @param array $params
* @return Event[]
*/
public function all(array $params = array())
public function all(array $params = [])
{
$endpoint = '/events.json';
$response = $this->request($endpoint, 'GET', $params);
Expand All @@ -27,7 +27,7 @@ public function all(array $params = array())
* @param array $params
* @return integer
*/
public function count(array $params = array())
public function count(array $params = [])
{
$endpoint = '/events/count.json';
$response = $this->request($endpoint, 'GET', $params);
Expand Down
Loading

0 comments on commit a7e7ba9

Please sign in to comment.