Skip to content

Commit a7e7ba9

Browse files
authored
Expose Guzzle Client, code cleanup (#30)
1 parent 4fbdef2 commit a7e7ba9

37 files changed

+167
-115
lines changed

src/AbstractApi.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function __construct(array $options = array())
5858
* Set our Client instance
5959
*
6060
* @param Client $httpHandler
61+
* @return $this
6162
*/
6263
public function setHttpHandler(Client $httpHandler)
6364
{
@@ -82,6 +83,7 @@ public function getHttpHandler()
8283
* Set our LoggerInterface
8384
*
8485
* @param LoggerInterface $logger
86+
* @return $this
8587
*/
8688
public function setLogger(LoggerInterface $logger)
8789
{
@@ -104,6 +106,7 @@ public function getMyshopifyDomain()
104106
* a new client, with new details
105107
*
106108
* @param string $domain
109+
* @return $this
107110
*/
108111
public function setMyshopifyDomain($domain)
109112
{

src/Api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function getApiSecret()
7272
* new client instance
7373
*
7474
* @param string $accessToken
75+
* @return $this
7576
*/
7677
public function setAccessToken($accessToken)
7778
{
@@ -94,6 +95,7 @@ public function getAccessToken()
9495
* Set our persistent storage interface
9596
*
9697
* @param PersistentStorageInterface $storage
98+
* @return $this
9799
*/
98100
public function setStorageInterface(PersistentStorageInterface $storage)
99101
{

src/Service/AbandonedCheckoutsService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class AbandonedCheckoutsService extends AbstractService
88
{
99
/**
10-
* List all abandonded checkouts
10+
* List all abandoned checkouts
1111
*
1212
* @link https://help.shopify.com/api/reference/abandoned_checkouts#index
1313
* @param array $params

src/Service/AbstractService.php

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,93 @@
22

33
namespace Shopify\Service;
44

5+
use GuzzleHttp\Client;
56
use GuzzleHttp\Psr7\Request;
67
use GuzzleHttp\Psr7\Response;
78
use Shopify\ApiInterface;
8-
use Shopify\Object\AbstractObject;
9-
use Shopify\Inflector;
109

1110
abstract class AbstractService
1211
{
13-
private $api;
14-
private $mapper;
12+
/**
13+
* Instantiated Guzzle Client for requests
14+
* @var Client
15+
*/
16+
private $client;
17+
18+
/**
19+
* The last API response from Shopify
20+
* @var Response|null
21+
*/
22+
private $lastResponse;
1523

1624
const REQUEST_METHOD_GET = 'GET';
1725
const REQUEST_METHOD_POST = 'POST';
1826
const REQUEST_METHOD_PUT = 'PUT';
1927
const REQUEST_METHOD_DELETE = 'DELETE';
2028

21-
public static function factory(ApiInterface $api)
29+
public static function factory(ApiInterface $api): AbstractService
2230
{
2331
return new static($api);
2432
}
2533

2634
public function __construct(ApiInterface $api)
2735
{
28-
$this->api = $api;
36+
$this->client = $api->getHttpHandler();
2937
}
3038

31-
public function getApi()
39+
/**
40+
* Get the client instance
41+
*
42+
* @return Client
43+
*/
44+
public function getClient()
3245
{
33-
return $this->api;
46+
return $this->client;
3447
}
3548

36-
public function request($endpoint, $method = self::REQUEST_METHOD_GET, array $params = array())
49+
/**
50+
* @param $endpoint
51+
* @param string $method
52+
* @param array $params
53+
* @return mixed
54+
*/
55+
public function request($endpoint, $method = self::REQUEST_METHOD_GET, array $params = [])
3756
{
38-
$request = $this->createRequest($endpoint, $method);
39-
return $this->send($request, $params);
57+
return $this->send(new Request($method, $endpoint), $params);
4058
}
4159

60+
/**
61+
* @param $endpoint
62+
* @param string $method
63+
* @return Request
64+
*/
4265
public function createRequest($endpoint, $method = self::REQUEST_METHOD_GET)
4366
{
4467
return new Request($method, $endpoint);
4568
}
4669

70+
/**
71+
* Get the last response from Shopify
72+
* @return Response
73+
*/
74+
public function getLastResponse()
75+
{
76+
return $this->lastResponse;
77+
}
78+
4779
public function send(Request $request, array $params = array())
4880
{
49-
$handler = $this->getApi()->getHttpHandler();
5081
$args = array();
5182
if ($request->getMethod() === 'GET') {
5283
$args['query'] = $params;
5384
} else {
5485
$args['json'] = $params;
5586
}
56-
$this->lastResponse = $handler->send($request, $args);
57-
return json_decode($this->lastResponse->getBody()->getContents(), true);
87+
$this->lastResponse = $this->client->send($request, $args);
88+
return json_decode(
89+
$this->lastResponse->getBody()->getContents(),
90+
true
91+
);
5892
}
5993

6094
public function createObject($className, $data)

src/Service/ApplicationChargeService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function create(ApplicationCharge &$applicationCharge)
4949
$data = $applicationCharge->exportData();
5050
$endpoint = '/admin/application_charges.json';
5151
$response = $this->request(
52-
'/admin/application_charges.json', 'POST', array(
52+
$endpoint, 'POST', array(
5353
'application_charge' => $data
5454
)
5555
);

src/Service/ArticleService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function all($blogId, array $params = array())
2222
}
2323

2424
/**
25-
* Receive acount of all Articles
25+
* Receive a count of all Articles
2626
*
2727
* @link https://help.shopify.com/api/reference/article#count
2828
* @param integer $blogId
@@ -115,7 +115,7 @@ public function delete($blogId, Article &$article)
115115
* Get a list of all the authors
116116
*
117117
* @link https://help.shopify.com/api/reference/article#authors
118-
* @return arrays
118+
* @return array
119119
*/
120120
public function authors()
121121
{

src/Service/AssetService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AssetService extends AbstractService
1313
* @link https://help.shopify.com/api/reference/asset#index
1414
* @param integer $themeId
1515
* @param array $params
16-
* @return Asset[]
16+
* @throws ShopifySdkException
1717
*/
1818
public function all($themeId, array $params = array())
1919
{
@@ -26,7 +26,7 @@ public function all($themeId, array $params = array())
2626
* @link https://help.shopify.com/api/reference/asset#show
2727
* @param integer $themeId
2828
* @param array $params
29-
* @return Article
29+
* @throws ShopifySdkException
3030
*/
3131
public function get($themeId, array $params = array())
3232
{
@@ -39,7 +39,7 @@ public function get($themeId, array $params = array())
3939
* @link https://help.shopify.com/api/reference/asset#update
4040
* @param integer $themeId
4141
* @param Asset $asset
42-
* @return
42+
* @throws ShopifySdkException
4343
*/
4444
public function put($themeId, Asset $asset)
4545
{
@@ -52,7 +52,7 @@ public function put($themeId, Asset $asset)
5252
* @link https://help.shopify.com/api/reference/asset#destroy
5353
* @param integer $themeId
5454
* @param Asset $asset
55-
* @return void
55+
* @throws ShopifySdkException
5656
*/
5757
public function delete($themeId, Asset $asset)
5858
{

src/Service/BlogService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function all(array $params = array())
2222
}
2323

2424
/**
25-
* Receivea count of all blogs
25+
* Receive a count of all blogs
2626
*
2727
* @link https://help.shopify.com/api/reference/blog#count
2828
* @return integer
@@ -77,19 +77,19 @@ public function create(Blog &$blog)
7777
*
7878
* @link https://help.shopify.com/api/reference/blog#update
7979
* @param Blog $blog
80-
* @return void
80+
* @throws ShopifySdkException
8181
*/
8282
public function update(Blog &$blog)
8383
{
8484
throw new ShopifySdkException('BlogService::update() not implemented');
8585
}
8686

8787
/**
88-
* Removea blog from Database
88+
* Remove a blog from Database
8989
*
9090
* @link https://help.shopify.com/api/reference/blog#destroy
9191
* @param Blog $blog
92-
* @return void
92+
* @throws ShopifySdkException
9393
*/
9494
public function delete(Blog $blog)
9595
{

src/Service/CollectService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function all(array $params = array())
3030
public function count(array $params = array())
3131
{
3232
$endpoint = '/collects/count.json';
33-
$data = $this->request($endpoint);
33+
$data = $this->request($endpoint, $params);
3434
return $data['count'];
3535
}
3636

src/Service/CommentService.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CommentService extends AbstractService
1313
* @param array $params
1414
* @return Comment[]
1515
*/
16-
public function all(array $params = array())
16+
public function all(array $params = [])
1717
{
1818
$endpoint = '/comments.json';
1919
$response = $this->request($endpoint, 'GET', $params);
@@ -27,10 +27,10 @@ public function all(array $params = array())
2727
* @param array $params
2828
* @return integer
2929
*/
30-
public function count(array $params = array())
30+
public function count(array $params = [])
3131
{
3232
$endpoint = '/comments/count.json';
33-
$response = $this->request($endpoint);
33+
$response = $this->request($endpoint, 'GET', $params);
3434
return $response['count'];
3535
}
3636

@@ -42,10 +42,10 @@ public function count(array $params = array())
4242
* @param array $params
4343
* @return Comment
4444
*/
45-
public function get($commentId, array $params = array())
45+
public function get($commentId, array $params = [])
4646
{
4747
$endpoint = '/comments/'.$commentId.'.json';
48-
$response = $this->request($endpoint);
48+
$response = $this->request($endpoint, 'GET', $params);
4949
return $this->createObject(Comment::class, $response['comment']);
5050
}
5151

@@ -144,7 +144,7 @@ public function remove(Comment &$comment)
144144
}
145145

146146
/**
147-
* Retore a comment
147+
* Restore a comment
148148
*
149149
* @link https://help.shopify.com/api/reference/comment#restore
150150
* @param Comment $comment

0 commit comments

Comments
 (0)