Skip to content

Commit 618c088

Browse files
committed
ShopifyClient and ShopifyAPI class re-organized
1 parent 286231c commit 618c088

File tree

3 files changed

+71
-49
lines changed

3 files changed

+71
-49
lines changed

lib/CustomerAddress.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function pluralizeKey()
5353
//TODO Fix Issue (Api Error) : Internal server error
5454
public function set($params)
5555
{
56-
$url = $this->apiUrl . '/' . $this->id . '/set.json?' . http_build_query($params);
56+
$url = $this->resourceUrl . '/set.json?' . http_build_query($params);
5757
return $this->put(array(), $url);
5858
}
5959
}

lib/ShopifyAPI.php

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ abstract class ShopifyAPI
4949
*
5050
* @var string
5151
*/
52-
//TODO Change $apiUrl to $resourceUrl
53-
protected $apiUrl;
52+
protected $resourceUrl;
5453

5554
/**
5655
* Key of the API Resource which is used to fetch data from request responses
@@ -117,22 +116,10 @@ public function __construct($config, $id = null)
117116
$this->id = $id;
118117

119118
$this->config = $config;
120-
//TODO Move the url manipulation part to ShopifyClient class
121-
//Remove https:// and trailing slash (if provided)
122-
$shopUrl = preg_replace('#^https?://|/$#', '',$config['ShopUrl']);
123-
124-
$this->shopUrl = $shopUrl;
125119

126120
$parentResource = isset($config['ParentResource']) ? $config['ParentResource'] : '';
127121

128-
if(isset($config['ApiKey'])) {
129-
$apiKey = $config['ApiKey'];
130-
$password = $config['Password'];
131-
132-
$this->apiUrl = "https://$apiKey:$password@" . $shopUrl . '/admin/' . $parentResource . $this->getResourcePath();
133-
} else {
134-
$this->apiUrl = 'https://' . $shopUrl . '/admin/' . $parentResource . $this->getResourcePath();
135-
}
122+
$this->resourceUrl = $config['ApiUrl'] . $parentResource . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');
136123
}
137124

138125
/**
@@ -166,30 +153,33 @@ public function __get($childName)
166153
*/
167154
public function __call($name, $arguments)
168155
{
169-
//If the $name starts with an uppercase letter, it's considered as a child class and a custom action otherwise
156+
//If the $name starts with an uppercase letter, it's considered as a child class
157+
//Otherwise it's a custom action
170158
if(ctype_upper($name[0])) {
171-
$childClass = array_search($name, $this->childResource);
159+
//Get the array key of the childResource in the childResource array
160+
$childKey = array_search($name, $this->childResource);
172161

173-
if ($childClass === false) {
162+
if ($childKey === false) {
174163
throw new \SdkException("Child Resource $name is not available for " . $this->getResourceName());
175-
} elseif (is_numeric($childClass)) {
176-
//If any associative key is given to the childname, then it will be considered as the class name, otherwise the childname will be the class name
177-
$childClass = $name;
178164
}
179165

180-
$apiClassName = __NAMESPACE__ . "\\" . $childClass;
166+
//If any associative key is given to the childname, then it will be considered as the class name,
167+
//otherwise the childname will be the class name
168+
$childClassName = !is_numeric($childKey) ? $childKey : $name;
169+
170+
$childClass = __NAMESPACE__ . "\\" . $childClassName;
171+
181172
$config = $this->config;
182173

183174
//Set the parent resource path for the child class
184175
$config['ParentResource'] = (isset($config['ParentResource']) ? $config['ParentResource'] : '') . $this->getResourcePath() . '/' . $this->id . '/';
185176

186-
$api = new $apiClassName($config);
187177

188-
if (!empty($arguments)) {
189-
//if the first argument is provided with the call, consider it as a resource ID
190-
$resourceID = $arguments[0];
191-
$api->id = $resourceID;
192-
}
178+
//If first argument is provided, it will be considered as the ID of the resource.
179+
$resourceID = !empty($arguments) ? $arguments[0] : null;
180+
181+
182+
$api = new $childClass($config, $resourceID);
193183

194184
return $api;
195185
} else {
@@ -200,26 +190,25 @@ public function __call($name, $arguments)
200190
'delete'=> 'customDeleteActions',
201191
);
202192

193+
//Get the array key for the action in the actions array
203194
foreach ($actionMaps as $httpMethod => $actionArrayKey) {
204195
$actionKey = array_search($name, $this->$actionArrayKey);
205196
if($actionKey !== false) break;
206197
}
207198

208199
if($actionKey === false) {
209200
throw new SdkException("No action named $name is defined for " . $this->getResourceName());
210-
} elseif (is_numeric($actionKey)) {
211-
//If any associative key is given to the action name, then it will be considered as the method name, otherwise the action name will be the method name
212-
$actionKey = $name;
213201
}
214202

215-
$params = array();
203+
//If any associative key is given to the action, then it will be considered as the method name,
204+
//otherwise the action name will be the method name
205+
$customAction = !is_numeric($actionKey) ? $actionKey : $name;
206+
216207

217208
//Get the first argument if provided with the method call
218-
if (!empty($arguments)) {
219-
$params = $arguments[0];
220-
}
209+
$params = !empty($arguments) ? $arguments[0] : array();
221210

222-
$url = $this->apiUrl . ($this->id ? '/' . $this->id : '') . "/$actionKey.json";
211+
$url = $this->generateUrl($params, $customAction);
223212

224213
return $this->$httpMethod($params, $url);
225214
}
@@ -256,14 +245,28 @@ protected function pluralizeKey() {
256245

257246
/**
258247
* Get the resource path to be used to generate the api url
259-
* Normally its the same as the pluralized version of the resource key, when it's different, the specific resource class will override this function
248+
* Normally its the same as the pluralized version of the resource key,
249+
* when it's different, the specific resource class will override this function
260250
*
261251
* @return string
262252
*/
263253
protected function getResourcePath() {
264254
return $this->pluralizeKey();
265255
}
266256

257+
/**
258+
* Generate the custom url for api request based on the params and custom action (if any)
259+
*
260+
* @param array $params
261+
* @param string $customAction
262+
*
263+
* @return string
264+
*/
265+
public function generateUrl($params = array(), $customAction = null)
266+
{
267+
return $this->resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($params) ? '?' . http_build_query($params) : '');
268+
}
269+
267270
/**
268271
* Generate a HTTP GET request and return results as an array
269272
*
@@ -277,7 +280,7 @@ protected function getResourcePath() {
277280
public function get($params = array(), $url = null)
278281
{
279282

280-
if (! $url) $url = $this->apiUrl . ($this->id ? "/{$this->id}" : '') . '.json' . (!empty($params) ? '?' . http_build_query($params) : '');
283+
if (! $url) $url = $this->generateUrl($params);
281284

282285
$this->prepareRequest();
283286

@@ -302,7 +305,7 @@ public function get($params = array(), $url = null)
302305
public function getCount($params = array(), $url = null)
303306
{
304307

305-
if (! $url) $url = $this->apiUrl . '/count.json' . (!empty($params) ? '?' . http_build_query($params) : '');
308+
if (! $url) $url = $this->generateUrl($params, 'count');
306309

307310
$this->prepareRequest();
308311

@@ -329,7 +332,7 @@ public function search($query)
329332

330333
if (! is_array($query)) $query = array('query' => $query);
331334

332-
$url = $this->apiUrl . '/search.json?' . http_build_query($query);
335+
$url = $this->generateUrl($query, 'search');
333336

334337
return $this->get(array(), $url);
335338
}
@@ -346,7 +349,7 @@ public function search($query)
346349
*/
347350
public function post($data, $url = null)
348351
{
349-
if (! $url) $url = $this->apiUrl . '.json';
352+
if (! $url) $url = $this->generateUrl();
350353

351354
$data = array($this->getResourcePostKey() => $data);
352355

@@ -370,7 +373,7 @@ public function post($data, $url = null)
370373
public function put($data, $url = null)
371374
{
372375

373-
if (! $url) $url = $this->apiUrl . ($this->id ? "/{$this->id}" : '') .'.json';
376+
if (! $url) $url = $this->generateUrl();
374377

375378
$data = array($this->getResourcePostKey() => $data);
376379

@@ -393,7 +396,7 @@ public function put($data, $url = null)
393396
*/
394397
public function delete($params = array(), $url = null)
395398
{
396-
if (! $url) $url = $this->apiUrl . ($this->id ? "/{$this->id}" : '') .'.json' . (!empty($params) ? '?' . http_build_query($params) : '');
399+
if (! $url) $url = $this->generateUrl($params);
397400

398401
$this->prepareRequest();
399402

lib/ShopifyClient.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
|$products = $shopify->Product($productID)->Variant->get();
6262
|
6363
*/
64+
use PHPShopify\Exception\SdkException;
65+
6466
class ShopifyClient
6567
{
6668
/**
@@ -75,10 +77,26 @@ class ShopifyClient
7577
*
7678
* @param array $config
7779
*
80+
* @throws SdkException if both AccessToken and ApiKey+Password Combination are missing
81+
*
7882
* @return void
7983
*/
8084
public function __construct($config)
8185
{
86+
//Remove https:// and trailing slash (if provided)
87+
$config['ShopUrl'] = preg_replace('#^https?://|/$#', '',$config['ShopUrl']);
88+
89+
if(isset($config['ApiKey']) && isset($config['Password'])) {
90+
$apiKey = $config['ApiKey'];
91+
$password = $config['Password'];
92+
93+
$config['ApiUrl'] = "https://$apiKey:$password@" . $config['ShopUrl'] . '/admin/';
94+
} elseif(!isset($config['AccessToken'])) {
95+
throw new SdkException("Either AccessToken or ApiKey+Password Combination must be provided!");
96+
} else {
97+
$config['ApiUrl'] = 'https://' . $config['ShopUrl'] . '/admin/';
98+
}
99+
82100
$this->config = $config;
83101
}
84102

@@ -111,12 +129,13 @@ public function __get($resourceName)
111129
public function __call($resourceName, $arguments)
112130
{
113131
$resourceClassName = __NAMESPACE__ . "\\$resourceName";
114-
$resource = new $resourceClassName($this->config);
115132

116-
if(!empty($arguments)) {
117-
$resourceID = $arguments[0];
118-
$resource->id = $resourceID;
119-
}
133+
//If first argument is provided, it will be considered as the ID of the resource.
134+
$resourceID = !empty($arguments) ? $arguments[0] : null;
135+
136+
//Initiate the resource object
137+
$resource = new $resourceClassName($this->config, $resourceID);
138+
120139
return $resource;
121140
}
122141
}

0 commit comments

Comments
 (0)