Skip to content

Commit

Permalink
Merge branch 'static_config_change'
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnhind committed Mar 8, 2019
2 parents b5217e3 + 811c29d commit eeb390f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
14 changes: 5 additions & 9 deletions lib/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public static function getCurrentUrl()
*
* @return bool
*/
public static function verifyShopifyRequest()
public static function verifyShopifyRequest( &$config )
{
$data = $_GET;

if(!isset(ShopifySDK::$config['SharedSecret'])) {
if(!isset($config['SharedSecret'])) {
throw new SdkException("Please provide SharedSecret while configuring the SDK client.");
}

$sharedSecret = ShopifySDK::$config['SharedSecret'];
$sharedSecret = $config['SharedSecret'];

//Get the hmac and remove it from array
if (isset($data['hmac'])) {
Expand Down Expand Up @@ -87,10 +87,8 @@ public static function verifyShopifyRequest()
*
* @return void|string
*/
public static function createAuthRequest($scopes, $redirectUrl = null, $state = null, $options = null, $return = false)
public static function createAuthRequest(&$config, $scopes, $redirectUrl = null, $state = null, $options = null, $return = false)
{
$config = ShopifySDK::$config;

if(!isset($config['ShopUrl']) || !isset($config['ApiKey'])) {
throw new SdkException("ShopUrl and ApiKey are required for authentication request. Please check SDK configuration!");
}
Expand Down Expand Up @@ -136,10 +134,8 @@ public static function createAuthRequest($scopes, $redirectUrl = null, $state =
*
* @return string
*/
public static function getAccessToken()
public static function getAccessToken(&$config)
{
$config = ShopifySDK::$config;

if(!isset($config['SharedSecret']) || !isset($config['ApiKey'])) {
throw new SdkException("SharedSecret and ApiKey are required for getting access token. Please check SDK configuration!");
}
Expand Down
10 changes: 6 additions & 4 deletions lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ abstract class ShopifyResource
*/
protected $resourceUrl;

protected $config;

/**
* Key of the API Resource which is used to fetch data from request responses
*
Expand Down Expand Up @@ -108,17 +110,17 @@ abstract class ShopifyResource
/**
* Create a new Shopify API resource instance.
*
* @param array $config
* @param integer $id
* @param string $parentResourceUrl
*
* @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
*/
public function __construct($id = null, $parentResourceUrl = '')
public function __construct($config, $id = null, $parentResourceUrl = '')
{
$this->config = $config;
$this->id = $id;

$config = ShopifySDK::$config;

$this->resourceUrl = ($parentResourceUrl ? $parentResourceUrl . '/' : $config['AdminUrl']) . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');

if (isset($config['AccessToken'])) {
Expand Down Expand Up @@ -181,7 +183,7 @@ public function __call($name, $arguments)
$resourceID = !empty($arguments) ? $arguments[0] : null;


$api = new $childClass($resourceID, $this->resourceUrl);
$api = new $childClass($this->config, $resourceID, $this->resourceUrl);

return $api;
} else {
Expand Down
30 changes: 15 additions & 15 deletions lib/ShopifySDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ShopifySDK
*
* @var array
*/
public static $config = array();
public $config = array();

/**
* List of available resources which can be called from this client
Expand Down Expand Up @@ -223,8 +223,8 @@ class ShopifySDK
public function __construct($config = array())
{
if(!empty($config)) {
ShopifySDK::$config = $config;
ShopifySDK::setAdminUrl();
$this->config = $config;
$this->setAdminUrl();
}
}

Expand Down Expand Up @@ -271,7 +271,7 @@ public function __call($resourceName, $arguments)
$resourceID = !empty($arguments) ? $arguments[0] : null;

//Initiate the resource object
$resource = new $resourceClassName($resourceID);
$resource = new $resourceClassName( $this->config, $resourceID);

return $resource;
}
Expand All @@ -283,15 +283,15 @@ public function __call($resourceName, $arguments)
*
* @return ShopifySDK
*/
public static function config($config)
public function config($config)
{
foreach ($config as $key => $value) {
self::$config[$key] = $value;
$this->config[$key] = $value;
}

//Re-set the admin url if shop url is changed
if(isset($config['ShopUrl'])) {
self::setAdminUrl();
$this->setAdminUrl();
}

//If want to keep more wait time than .5 seconds for each call
Expand All @@ -307,22 +307,22 @@ public static function config($config)
*
* @return string
*/
public static function setAdminUrl()
public function setAdminUrl()
{
$shopUrl = self::$config['ShopUrl'];
$shopUrl = $this->config['ShopUrl'];

//Remove https:// and trailing slash (if provided)
$shopUrl = preg_replace('#^https?://|/$#', '', $shopUrl);

if(isset(self::$config['ApiKey']) && isset(self::$config['Password'])) {
$apiKey = self::$config['ApiKey'];
$apiPassword = self::$config['Password'];
if(isset($this->config['ApiKey']) && isset($this->config['Password'])) {
$apiKey = $this->config['ApiKey'];
$apiPassword = $this->config['Password'];
$adminUrl = "https://$apiKey:$apiPassword@$shopUrl/admin/";
} else {
$adminUrl = "https://$shopUrl/admin/";
}

self::$config['AdminUrl'] = $adminUrl;
$this->config['AdminUrl'] = $adminUrl;

return $adminUrl;
}
Expand All @@ -332,8 +332,8 @@ public static function setAdminUrl()
*
* @return string
*/
public static function getAdminUrl() {
return self::$config['AdminUrl'];
public function getAdminUrl() {
return $this->config['AdminUrl'];
}

/**
Expand Down

0 comments on commit eeb390f

Please sign in to comment.