Skip to content

Commit 3ec576d

Browse files
committed
Static configuration variable to ShopifyClient class and remove $config from ShopifyAPI class
1 parent a26a6e0 commit 3ec576d

File tree

3 files changed

+19
-160
lines changed

3 files changed

+19
-160
lines changed

lib/Multipass.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ class Multipass extends ShopifyAPI
1818
/**
1919
* Multipass constructor.
2020
*
21-
* @param array $config
2221
* @param integer $id
2322
*
2423
* @throws ApiException
25-
*
26-
* @return void
2724
*/
28-
public function __construct($config, $id = null)
25+
public function __construct($id = null)
2926
{
3027
throw new ApiException("Multipass API is not available yet!");
3128
}

lib/ShopifyAPI.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@
2323
*/
2424
abstract class ShopifyAPI
2525
{
26-
/**
27-
* Shop / API configurations
28-
*
29-
* @var array
30-
*/
31-
protected $config;
32-
3326
/**
3427
* HTTP request headers
3528
*
@@ -108,23 +101,23 @@ abstract class ShopifyAPI
108101
/**
109102
* Create a new Shopify API resource instance.
110103
*
111-
* @param array $config
112104
* @param integer $id
105+
* @param string $parentResourceUrl
113106
*
114-
* @return void
107+
* @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
115108
*/
116-
public function __construct($config, $id = null)
109+
public function __construct($id = null, $parentResourceUrl = '')
117110
{
118111
$this->id = $id;
119112

120-
$this->config = $config;
113+
$config = ShopifyClient::$config;
121114

122-
$parentResource = isset($config['ParentResource']) ? $config['ParentResource'] : '';
123-
124-
$this->resourceUrl = $config['ApiUrl'] . $parentResource . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');
115+
$this->resourceUrl = ($parentResourceUrl ? $parentResourceUrl . '/' : $config['ApiUrl']) . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');
125116

126117
if (isset($config['AccessToken'])) {
127118
$this->httpHeaders['X-Shopify-Access-Token'] = $config['AccessToken'];
119+
} elseif (!isset($config['ApiKey']) || !isset($config['Password'])) {
120+
throw new SdkException("Either AccessToken or ApiKey+Password Combination (in case of private API) is required to access the resources. Please check SDK configuration!");
128121
}
129122
}
130123

@@ -177,17 +170,11 @@ public function __call($name, $arguments)
177170

178171
$childClass = __NAMESPACE__ . "\\" . $childClassName;
179172

180-
$config = $this->config;
181-
182-
//Set the parent resource path for the child class
183-
$config['ParentResource'] = (isset($config['ParentResource']) ? $config['ParentResource'] : '') . $this->getResourcePath() . '/' . $this->id . '/';
184-
185-
186173
//If first argument is provided, it will be considered as the ID of the resource.
187174
$resourceID = !empty($arguments) ? $arguments[0] : null;
188175

189176

190-
$api = new $childClass($config, $resourceID);
177+
$api = new $childClass($resourceID, $this->resourceUrl);
191178

192179
return $api;
193180
} else {

lib/ShopifyClient.php

Lines changed: 10 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ShopifyClient
7070
*
7171
* @var array
7272
*/
73-
protected $config;
73+
public static $config;
7474

7575
/**
7676
* List of available resources which can be called from this client
@@ -135,23 +135,17 @@ class ShopifyClient
135135
*
136136
* @param array $config
137137
*
138-
* @throws SdkException if both AccessToken and ApiKey+Password Combination are missing
139-
*
140138
* @return void
141139
*/
142140
public function __construct($config)
143141
{
144-
145-
146142
if (isset($config['ApiKey']) && isset($config['Password'])) {
147-
$config['ApiUrl'] = ShopifyClient::getAdminUrl($config['ShopUrl'], $config['ApiKey'], $config['Password']);
148-
} elseif (!isset($config['AccessToken'])) {
149-
throw new SdkException("Either AccessToken or ApiKey+Password Combination must be provided!");
143+
$config['ApiUrl'] = AuthHelper::getAdminUrl($config['ShopUrl'], $config['ApiKey'], $config['Password']);
150144
} else {
151-
$config['ApiUrl'] = ShopifyClient::getAdminUrl($config['ShopUrl']);
145+
$config['ApiUrl'] = AuthHelper::getAdminUrl($config['ShopUrl']);
152146
}
153147

154-
$this->config = $config;
148+
ShopifyClient::$config = $config;
155149
}
156150

157151
/**
@@ -197,138 +191,19 @@ public function __call($resourceName, $arguments)
197191
$resourceID = !empty($arguments) ? $arguments[0] : null;
198192

199193
//Initiate the resource object
200-
$resource = new $resourceClassName($this->config, $resourceID);
194+
$resource = new $resourceClassName($resourceID);
201195

202196
return $resource;
203197
}
204198

205199
/**
206-
* Return the admin url, based on a given shop url
200+
* Configure the SDK client
207201
*
208-
* @param string $shopUrl
209-
* @param string $apiKey
210-
* @param string $apiPassword
211-
* @return string
202+
* @param $config
203+
* @return ShopifyClient
212204
*/
213-
public static function getAdminUrl($shopUrl, $apiKey = null, $apiPassword = null)
205+
public static function config($config)
214206
{
215-
//Remove https:// and trailing slash (if provided)
216-
$shopUrl = preg_replace('#^https?://|/$#', '', $shopUrl);
217-
218-
if($apiKey && $apiPassword) {
219-
$adminUrl = "https://$apiKey:$apiPassword@$shopUrl/admin/";
220-
} else {
221-
$adminUrl = "https://$shopUrl/admin/";
222-
}
223-
return $adminUrl;
224-
}
225-
226-
/**
227-
* Verify if the request is made from shopify using hmac hash value
228-
*
229-
* @throws SdkException if hmac is not found in the url parameters
230-
*
231-
* @param string $sharedSecret Shared Secret of the Shopify App
232-
*
233-
* @return bool
234-
*/
235-
public static function verifyShopifyRequest($sharedSecret)
236-
{
237-
$data = $_GET;
238-
//Get the hmac and remove it from array
239-
if (isset($data['hmac'])) {
240-
$hmac = $data['hmac'];
241-
unset($data['hmac']);
242-
} else {
243-
throw new SdkException("HMAC value not found in url parameters.");
244-
}
245-
//signature validation is deprecated
246-
if (isset($data['signature'])) {
247-
unset($data['signature']);
248-
}
249-
//Create data string for the remaining url parameters
250-
$dataString = http_build_query($data);
251-
252-
$realHmac = hash_hmac('sha256', $dataString, $sharedSecret);
253-
254-
//hash the values before comparing (to prevent time attack)
255-
if(md5($realHmac) === md5($hmac)) {
256-
return true;
257-
} else {
258-
return false;
259-
}
260-
}
261-
262-
/**
263-
* Redirect the user to the authorization page to allow the app access to the shop
264-
*
265-
* @see https://help.shopify.com/api/guides/authentication/oauth#scopes For allowed scopes
266-
*
267-
* @param array $config
268-
* @param string|string[] $scopes Scopes required by app
269-
* @param string $redirectUrl
270-
*
271-
* @return void
272-
*/
273-
public static function createAuthRequest($config, $scopes, $redirectUrl = null)
274-
{
275-
if (!$redirectUrl) {
276-
//If redirect url is the same as this url, then need to check for access token when redirected back from shopify
277-
if(isset($_GET['code'])) {
278-
return self::getAccessToken($config);
279-
} else {
280-
$redirectUrl = self::getCurrentUrl();
281-
}
282-
}
283-
284-
if (is_array($scopes)) {
285-
$scopes = join(',', $scopes);
286-
}
287-
$authUrl = self::getAdminUrl($config['ShopUrl']) . 'oauth/authorize?client_id=' . $config['ApiKey'] . '&redirect_uri=' . $redirectUrl . "&scope=$scopes";
288-
289-
header("Location: $authUrl");
290-
}
291-
292-
/**
293-
* Get Access token for the API
294-
* Call this when being redirected from shopify page ( to the $redirectUrl) after authentication
295-
*
296-
* @param array $config
297-
*
298-
* @return string
299-
*/
300-
public static function getAccessToken($config)
301-
{
302-
if(self::verifyShopifyRequest($config['SharedSecret'])) {
303-
$data = array(
304-
'client_id' => $config['ApiKey'],
305-
'client_secret' => $config['SharedSecret'],
306-
'code' => $_GET['code'],
307-
);
308-
309-
$response = HttpRequestJson::post(self::getAdminUrl($config['ShopUrl']) . 'oauth/access_token', $data);
310-
311-
return isset($response['access_token']) ? $response['access_token'] : null;
312-
}
313-
}
314-
315-
/**
316-
* Get the url of the current page
317-
*
318-
* @return string
319-
*/
320-
public static function getCurrentUrl()
321-
{
322-
if (isset($_SERVER['HTTPS']) &&
323-
($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
324-
isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
325-
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
326-
$protocol = 'https';
327-
}
328-
else {
329-
$protocol = 'http';
330-
}
331-
332-
return "$protocol://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
207+
return new ShopifyClient($config);
333208
}
334209
}

0 commit comments

Comments
 (0)