@@ -70,7 +70,7 @@ class ShopifyClient
7070 *
7171 * @var array
7272 */
73- public static $ config ;
73+ public static $ config = array () ;
7474
7575 /**
7676 * List of available resources which can be called from this client
@@ -137,15 +137,12 @@ class ShopifyClient
137137 *
138138 * @return void
139139 */
140- public function __construct ($ config )
140+ public function __construct ($ config = array () )
141141 {
142- if (isset ($ config ['ApiKey ' ]) && isset ($ config ['Password ' ])) {
143- $ config ['ApiUrl ' ] = AuthHelper::getAdminUrl ($ config ['ShopUrl ' ], $ config ['ApiKey ' ], $ config ['Password ' ]);
144- } else {
145- $ config ['ApiUrl ' ] = AuthHelper::getAdminUrl ($ config ['ShopUrl ' ]);
142+ if (!empty ($ config )) {
143+ ShopifyClient::$ config = $ config ;
144+ ShopifyClient::setAdminUrl ();
146145 }
147-
148- ShopifyClient::$ config = $ config ;
149146 }
150147
151148 /**
@@ -199,11 +196,55 @@ public function __call($resourceName, $arguments)
199196 /**
200197 * Configure the SDK client
201198 *
202- * @param $config
199+ * @param array $config
200+ *
203201 * @return ShopifyClient
204202 */
205203 public static function config ($ config )
206204 {
207- return new ShopifyClient ($ config );
205+ foreach ($ config as $ key => $ value ) {
206+ self ::$ config [$ key ] = $ value ;
207+ }
208+
209+ //Re-set the admin url if shop url is changed
210+ if (isset ($ config ['ShopUrl ' ])) {
211+ self ::setAdminUrl ();
212+ }
213+
214+ return new ShopifyClient ;
215+ }
216+
217+ /**
218+ * Set the admin url, based on the configured shop url
219+ *
220+ * @return string
221+ */
222+ public static function setAdminUrl ()
223+ {
224+ $ shopUrl = self ::$ config ['ShopUrl ' ];
225+
226+ //Remove https:// and trailing slash (if provided)
227+ $ shopUrl = preg_replace ('#^https?://|/$# ' , '' , $ shopUrl );
228+
229+ if (isset (self ::$ config ['ApiKey ' ]) && isset (self ::$ config ['Password ' ])) {
230+ $ apiKey = self ::$ config ['ApiKey ' ];
231+ $ apiPassword = self ::$ config ['Password ' ];
232+ $ adminUrl = "https:// $ apiKey: $ apiPassword@ $ shopUrl/admin/ " ;
233+ } else {
234+ $ adminUrl = "https:// $ shopUrl/admin/ " ;
235+ }
236+
237+ self ::$ config ['AdminUrl ' ] = $ adminUrl ;
238+
239+ return $ adminUrl ;
240+ }
241+
242+ /**
243+ * Get the admin url of the configured shop
244+ *
245+ * @return string
246+ */
247+ public static function getAdminUrl () {
248+ return self ::$ config ['AdminUrl ' ];
208249 }
209250}
0 commit comments