18
18
use Osiset \ShopifyApp \Objects \Enums \ApiMethod ;
19
19
use Osiset \ShopifyApp \Objects \Enums \AuthMode ;
20
20
use Osiset \ShopifyApp \Objects \Enums \ChargeType ;
21
+ use Osiset \ShopifyApp \Objects \Enums \DataSource ;
21
22
use Osiset \ShopifyApp \Objects \Transfers \PlanDetails as PlanDetailsTransfer ;
22
23
use Osiset \ShopifyApp \Objects \Transfers \UsageChargeDetails as UsageChargeDetailsTransfer ;
23
24
use Osiset \ShopifyApp \Objects \Values \ChargeReference ;
24
- use Osiset \ShopifyApp \Objects \Values \ShopDomain ;
25
+ use Osiset \ShopifyApp \Objects \Values \NullableShopDomain ;
25
26
26
27
/**
27
28
* Basic helper class for API calls to Shopify.
@@ -43,7 +44,7 @@ public function make(Session $session = null): self
43
44
// Create the options
44
45
$ opts = new Options ();
45
46
46
- $ shop = $ this ->getShop ($ session )->toNative ();
47
+ $ shop = $ this ->getShopDomain ($ session )->toNative ();
47
48
$ opts ->setApiKey (getShopifyConfig ('api_key ' , $ shop ));
48
49
$ opts ->setApiSecret (getShopifyConfig ('api_secret ' , $ shop ));
49
50
$ opts ->setVersion (getShopifyConfig ('api_version ' , $ shop ));
@@ -473,31 +474,42 @@ protected function doRequestGraphQL(string $query, array $payload = null)
473
474
}
474
475
475
476
/**
476
- * @param Session $session
477
- * @throws \InvalidArgumentException
478
- * @return ShopDomain
477
+ * Find shop from session or request.
478
+ *
479
+ * @param Session $session
480
+ *
481
+ * @return NullableShopDomain
479
482
*/
480
- private function getShop (Session $ session = null )
483
+ private function getShopDomain (Session $ session = null ): NullableShopDomain
481
484
{
482
- $ shop = '' ;
485
+ // Check for existing session passed in
483
486
if ($ session && $ session ->getShop ()) {
484
- $ shop = $ session ->getShop ();
487
+ return NullableShopDomain:: fromNative ( $ session ->getShop () );
485
488
}
486
489
487
- if (! $ shop ) {
488
- $ shop = Arr::get (Request::all (), 'shop ' );
489
- }
490
-
491
- if (! $ shop ) {
492
- $ refererQueryParams = [];
493
- parse_str (Request::server ('HTTP_REFERER ' ), $ refererQueryParams );
494
- $ shop = Arr::get ($ refererQueryParams , 'shop ' );
495
- }
496
-
497
- if (! $ shop ) {
498
- $ shop = Request::header ('X-Shop-Domain ' );
490
+ $ options = [
491
+ // Input
492
+ DataSource::INPUT ()->toNative () => function (): ?string {
493
+ return Arr::get (Request::all (), 'shop ' );
494
+ },
495
+ // Headers
496
+ DataSource::HEADER ()->toNative () => function (): ?string {
497
+ return Request::header ('X-Shop-Domain ' );
498
+ },
499
+ // Headers: Referer
500
+ DataSource::REFERER ()->toNative () => function (): ?string {
501
+ $ url = parse_url (Request::server ('HTTP_REFERER ' ), PHP_URL_QUERY );
502
+ parse_str ($ url , $ refererQueryParams );
503
+ return Arr::get ($ refererQueryParams , 'shop ' );
504
+ }
505
+ ];
506
+ foreach ($ options as $ method => $ fn ) {
507
+ $ result = $ fn ();
508
+ if (!is_null ($ result )) {
509
+ return NullableShopDomain::fromNative ($ result );
510
+ }
499
511
}
500
512
501
- return is_a ( $ shop , ' Osiset\ShopifyApp\Objects\Values\ShopDomain ' ) ? $ shop : ShopDomain:: fromNative (( string ) $ shop );
513
+ return NullableShopDomain:: fromNative (null );
502
514
}
503
515
}
0 commit comments