Skip to content

Commit 097424c

Browse files
committed
Check available resource list before accessing.
1 parent cb5853a commit 097424c

File tree

1 file changed

+103
-36
lines changed

1 file changed

+103
-36
lines changed

lib/ShopifyClient.php

Lines changed: 103 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,50 @@
1515
| Shopify API SDK Client Class
1616
|--------------------------------------------------------------------------
1717
|
18-
|This class initializes the resource objects
18+
| This class initializes the resource objects
1919
|
20-
|Usage:
21-
|//For private app
22-
|$config = array(
23-
| 'ShopUrl' => 'yourshop.myshopify.com',
24-
| 'ApiKey' => '***YOUR-PRIVATE-API-KEY***',
25-
| 'Password' => '***YOUR-PRIVATE-API-PASSWORD***',
26-
|);
27-
|//For third party app
28-
|$config = array(
29-
| 'ShopUrl' => 'yourshop.myshopify.com',
30-
| 'AccessToken' => '***ACCESS-TOKEN-FOR-THIRD-PARTY-APP***',
31-
|);
32-
|//Create the shopify client object
33-
|$shopify = new ShopifyClient($config);
20+
| Usage:
21+
| //For private app
22+
| $config = array(
23+
| 'ShopUrl' => 'yourshop.myshopify.com',
24+
| 'ApiKey' => '***YOUR-PRIVATE-API-KEY***',
25+
| 'Password' => '***YOUR-PRIVATE-API-PASSWORD***',
26+
| );
27+
| //For third party app
28+
| $config = array(
29+
| 'ShopUrl' => 'yourshop.myshopify.com',
30+
| 'AccessToken' => '***ACCESS-TOKEN-FOR-THIRD-PARTY-APP***',
31+
| );
32+
| //Create the shopify client object
33+
| $shopify = new ShopifyClient($config);
3434
|
35-
|//Get shop details
36-
|$products = $shopify->Shop->get();
35+
| //Get shop details
36+
| $products = $shopify->Shop->get();
3737
|
38-
|//Get list of all products
39-
|$products = $shopify->Product->get();
38+
| //Get list of all products
39+
| $products = $shopify->Product->get();
4040
|
41-
|//Get a specific product by product ID
42-
|$products = $shopify->Product($productID)->get();
41+
| //Get a specific product by product ID
42+
| $products = $shopify->Product($productID)->get();
4343
|
44-
|//Update a product
45-
|$updateInfo = array('title' => 'New Product Title');
46-
|$products = $shopify->Product($productID)->put($updateInfo);
44+
| //Update a product
45+
| $updateInfo = array('title' => 'New Product Title');
46+
| $products = $shopify->Product($productID)->put($updateInfo);
4747
|
48-
|//Delete a product
49-
|$products = $shopify->Product($productID)->delete();
48+
| //Delete a product
49+
| $products = $shopify->Product($productID)->delete();
5050
|
51-
|//Create a new product
52-
|$productInfo = array(
53-
| "title" => "Burton Custom Freestlye 151",
54-
| "body_html" => "<strong>Good snowboard!<\/strong>",
55-
| "vendor" => "Burton",
56-
| "product_type" => "Snowboard",
57-
|);
58-
|$products = $shopify->Product->post($productInfo);
51+
| //Create a new product
52+
| $productInfo = array(
53+
| "title" => "Burton Custom Freestlye 151",
54+
| "body_html" => "<strong>Good snowboard!<\/strong>",
55+
| "vendor" => "Burton",
56+
| "product_type" => "Snowboard",
57+
| );
58+
| $products = $shopify->Product->post($productInfo);
5959
|
60-
|//Get variants of a product (using Child resource)
61-
|$products = $shopify->Product($productID)->Variant->get();
60+
| //Get variants of a product (using Child resource)
61+
| $products = $shopify->Product($productID)->Variant->get();
6262
|
6363
*/
6464
use PHPShopify\Exception\SdkException;
@@ -72,6 +72,64 @@ class ShopifyClient
7272
*/
7373
protected $config;
7474

75+
/**
76+
* List of available resources which can be called from this client
77+
*
78+
* @var string[]
79+
*/
80+
protected $resources = array(
81+
'AbandonedCheckout',
82+
'ApplicationCharge',
83+
'Blog',
84+
'CarrierService',
85+
'Collect',
86+
'Comment',
87+
'Country',
88+
'CustomCollection',
89+
'Customer',
90+
'CustomerSavedSearch',
91+
'Discount',
92+
'Event',
93+
'FulfillmentService',
94+
'GiftCard',
95+
'Location',
96+
'Metafield',
97+
'Multipass',
98+
'Order',
99+
'Page',
100+
'Policy',
101+
'Product',
102+
'RecurringApplicationCharge',
103+
'Redirect',
104+
'ScriptTag',
105+
'ShippingZone',
106+
'Shop',
107+
'SmartCollection',
108+
'Theme',
109+
'User',
110+
'Webhook',
111+
);
112+
113+
/**
114+
* List of resources which are only available through a parent resource
115+
*
116+
* @var array Array key is the child resource name and array value is the parent resource name
117+
*/
118+
protected $childResources = array(
119+
'Article' => 'Blog',
120+
'Asset' => 'Theme',
121+
'CustomerAddress' => 'Customer',
122+
'Fulfillment' => 'Order',
123+
'FulfillmentEvent' => 'Fulfillment',
124+
'OrderRisk' => 'Order',
125+
'ProductImage' => 'Product',
126+
'ProductVariant' => 'Product',
127+
'Province' => 'Country',
128+
'Refund' => 'Order',
129+
'Transaction' => 'Order',
130+
'UsageCharge' => 'RecurringApplicationCharge',
131+
);
132+
75133
/*
76134
* ShopifyClient constructor
77135
*
@@ -128,6 +186,15 @@ public function __get($resourceName)
128186
*/
129187
public function __call($resourceName, $arguments)
130188
{
189+
if (!in_array($resourceName, $this->resources)) {
190+
if (isset($this->childResources[$resourceName])) {
191+
$message = "$resourceName is a child resource of " . $this->childResources[$resourceName] . ". Cannot be accessed directly.";
192+
} else {
193+
$message = "Invalid resource name $resourceName. Pls check the API Reference to get the appropriate resource name.";
194+
}
195+
throw new SdkException($message);
196+
}
197+
131198
$resourceClassName = __NAMESPACE__ . "\\$resourceName";
132199

133200
//If first argument is provided, it will be considered as the ID of the resource.

0 commit comments

Comments
 (0)