Skip to content

Commit 1404d12

Browse files
committed
Better naming for variables
1 parent a2b5429 commit 1404d12

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

lib/ShopifyAPI.php

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHPShopify\Exception\ApiException;
1313
use PHPShopify\Exception\SdkException;
14+
use PHPShopify\Exception\CurlException;
1415

1516
/*
1617
|--------------------------------------------------------------------------
@@ -206,11 +207,24 @@ public function __call($name, $arguments)
206207

207208

208209
//Get the first argument if provided with the method call
209-
$params = !empty($arguments) ? $arguments[0] : array();
210+
$methodArgument = !empty($arguments) ? $arguments[0] : array();
211+
212+
//Url parameters
213+
$urlParams = array();
214+
//Data body
215+
$dataArray = array();
216+
217+
//Consider the argument as url parameters for get and delete request
218+
//and data array for post and put request
219+
if ($httpMethod == 'post' || $httpMethod == 'put') {
220+
$dataArray = $methodArgument;
221+
} else {
222+
$urlParams = $methodArgument;
223+
}
210224

211-
$url = $this->generateUrl($params, $customAction);
225+
$url = $this->generateUrl($urlParams, $customAction);
212226

213-
return $this->$httpMethod($params, $url);
227+
return $this->$httpMethod($dataArray, $url);
214228
}
215229
}
216230

@@ -257,30 +271,30 @@ protected function getResourcePath() {
257271
/**
258272
* Generate the custom url for api request based on the params and custom action (if any)
259273
*
260-
* @param array $params
274+
* @param array $urlParams
261275
* @param string $customAction
262276
*
263277
* @return string
264278
*/
265-
public function generateUrl($params = array(), $customAction = null)
279+
public function generateUrl($urlParams = array(), $customAction = null)
266280
{
267-
return $this->resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($params) ? '?' . http_build_query($params) : '');
281+
return $this->resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . http_build_query($urlParams) : '');
268282
}
269283

270284
/**
271285
* Generate a HTTP GET request and return results as an array
272286
*
273-
* @param array $params Check Shopify API reference of the specific resource for the list of URL parameters
287+
* @param array $urlParams Check Shopify API reference of the specific resource for the list of URL parameters
274288
* @param string $url
275289
*
276290
* @uses CurlRequest::get() to send the HTTP request
277291
*
278292
* @return array
279293
*/
280-
public function get($params = array(), $url = null)
294+
public function get($urlParams = array(), $url = null)
281295
{
282296

283-
if (! $url) $url = $this->generateUrl($params);
297+
if (! $url) $url = $this->generateUrl($urlParams);
284298

285299
$this->prepareRequest();
286300

@@ -295,17 +309,17 @@ public function get($params = array(), $url = null)
295309
/**
296310
* Get count for the number of resources available
297311
*
298-
* @param array $params Check Shopify API reference of the specific resource for the list of URL parameters
312+
* @param array $urlParams Check Shopify API reference of the specific resource for the list of URL parameters
299313
* @param string $url
300314
*
301315
* @uses CurlRequest::get() to send the HTTP request
302316
*
303317
* @return integer
304318
*/
305-
public function getCount($params = array(), $url = null)
319+
public function getCount($urlParams = array(), $url = null)
306320
{
307321

308-
if (! $url) $url = $this->generateUrl($params, 'count');
322+
if (! $url) $url = $this->generateUrl($urlParams, 'count');
309323

310324
$this->prepareRequest();
311325

@@ -317,8 +331,7 @@ public function getCount($params = array(), $url = null)
317331
/**
318332
* Search within the resouce
319333
*
320-
* @param array $params
321-
* @param string $url
334+
* @param mixed $query
322335
*
323336
* @throws SdkException if search is not enabled for the resouce
324337
*
@@ -340,18 +353,18 @@ public function search($query)
340353
/**
341354
* Call POST method to create a new resource
342355
*
343-
* @param array $data Check Shopify API reference of the specific resource for the list of required and optional data elements to be provided
356+
* @param array $dataArray Check Shopify API reference of the specific resource for the list of required and optional data elements to be provided
344357
* @param string $url
345358
*
346359
* @uses CurlRequest::post() to send the HTTP request
347360
*
348361
* @return array
349362
*/
350-
public function post($data, $url = null)
363+
public function post($dataArray, $url = null)
351364
{
352365
if (! $url) $url = $this->generateUrl();
353366

354-
$this->prepareRequest($data);
367+
$this->prepareRequest($dataArray);
355368

356369
$response = CurlRequest::post($url, $this->postDataJSON, $this->httpHeaders);
357370

@@ -361,19 +374,19 @@ public function post($data, $url = null)
361374
/**
362375
* Call PUT method to update an existing resource
363376
*
364-
* @param array $data Check Shopify API reference of the specific resource for the list of required and optional data elements to be provided
377+
* @param array $dataArray Check Shopify API reference of the specific resource for the list of required and optional data elements to be provided
365378
* @param string $url
366379
*
367380
* @uses CurlRequest::put() to send the HTTP request
368381
*
369382
* @return array
370383
*/
371-
public function put($data, $url = null)
384+
public function put($dataArray, $url = null)
372385
{
373386

374387
if (! $url) $url = $this->generateUrl();
375388

376-
$this->prepareRequest($data);
389+
$this->prepareRequest($dataArray);
377390

378391
$response = CurlRequest::put($url, $this->postDataJSON, $this->httpHeaders);
379392

@@ -383,16 +396,16 @@ public function put($data, $url = null)
383396
/**
384397
* Call DELETE method to delete an existing resource
385398
*
386-
* @param array $params Check Shopify API reference of the specific resource for the list of URL parameters
399+
* @param array $urlParams Check Shopify API reference of the specific resource for the list of URL parameters
387400
* @param string $url
388401
*
389402
* @uses CurlRequest::delete() to send the HTTP request
390403
*
391404
* @return array an empty array will be returned if the request is successfully completed
392405
*/
393-
public function delete($params = array(), $url = null)
406+
public function delete($urlParams = array(), $url = null)
394407
{
395-
if (! $url) $url = $this->generateUrl($params);
408+
if (! $url) $url = $this->generateUrl($urlParams);
396409

397410
$this->prepareRequest();
398411

@@ -404,23 +417,23 @@ public function delete($params = array(), $url = null)
404417
/**
405418
* Prepare the data and request headers before making the call
406419
*
407-
* @param array $data
420+
* @param array $dataArray
408421
*
409422
* @return void
410423
*/
411-
public function prepareRequest($data = array())
424+
public function prepareRequest($dataArray = array())
412425
{
413-
if (!empty($data)) $data = $this->wrapData($data);
426+
if (!empty($dataArray)) $dataArray = $this->wrapData($dataArray);
414427

415-
$this->postDataJSON = json_encode($data);
428+
$this->postDataJSON = json_encode($dataArray);
416429

417430
$this->httpHeaders = array();
418431

419432
if (isset($this->config['AccessToken'])) {
420433
$this->httpHeaders['X-Shopify-Access-Token'] = $this->config['AccessToken'];
421434
}
422435

423-
if (!empty($data)) {
436+
if (!empty($dataArray)) {
424437
$this->httpHeaders['Content-type'] = 'application/json';
425438
$this->httpHeaders['Content-Length'] = strlen($this->postDataJSON);
426439
}
@@ -430,15 +443,15 @@ public function prepareRequest($data = array())
430443
/**
431444
* Wrap data array with resource key
432445
*
433-
* @param array $data
446+
* @param array $dataArray
434447
* @param string $dataKey
435448
*
436449
* @return array
437450
*/
438-
protected function wrapData($data, $dataKey = null) {
439-
if (!$dataKey) $dataKey = $this->getResourcePostKey();
451+
protected function wrapData($dataArray, $dataKey = null) {
452+
if (! $dataKey) $dataKey = $this->getResourcePostKey();
440453

441-
return array($dataKey => $data);
454+
return array($dataKey => $dataArray);
442455
}
443456

444457
/**
@@ -475,14 +488,15 @@ protected function castString($array)
475488
* @param string $dataKey key to be looked for in the data
476489
*
477490
* @throws ApiException if the response has an error specified
491+
* @throws CurlException if response received with unexpected HTTP code.
478492
*
479493
* @return array
480494
*/
481495
public function processResponse($response, $dataKey = false)
482496
{
483497
$responseArray = json_decode($response, true);
484498

485-
if($responseArray === null) {
499+
if ($responseArray === null) {
486500
//Something went wrong, Checking HTTP Codes
487501
$httpOK = 200; //Request Successful, OK.
488502
$httpCreated = 201; //Create Successful.
@@ -499,7 +513,7 @@ public function processResponse($response, $dataKey = false)
499513
throw new ApiException($message);
500514
}
501515

502-
if($dataKey && isset($responseArray[$dataKey])) {
516+
if ($dataKey && isset($responseArray[$dataKey])) {
503517
return $responseArray[$dataKey];
504518
} else {
505519
return $responseArray;

0 commit comments

Comments
 (0)