Skip to content

Add api key finder class and related configurations #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/ApiKeyFinder/CurrentApiKeyFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Osiset\ShopifyApp\ApiKeyFinder;

use Illuminate\Support\Facades\Config;
use Osiset\ShopifyApp\Contracts\CurrentApiKeyFinderInterface;
use Osiset\ShopifyApp\Objects\Values\NullableShopDomain;

class CurrentApiKeyFinder implements CurrentApiKeyFinderInterface
{
private const SUPPORTED_KEYS = [
'api_key',
'api_secret'
];

public static function resolve(string $key, $shop = null): ?string
{
$fullKey = "shopify-app.{$key}";
if (! $shop || ! in_array($key, self::SUPPORTED_KEYS)) {
// No shop passed, return default
return Config::get($fullKey);
}

$shopDomain = $shop instanceof NullableShopDomain ? $shop->toNative() : $shop;
$shopDomain = explode('.', $shopDomain)[0];
$searchKey = "shopify-app.config_api_shop_keys.{$key}_{$shopDomain}";

if (! Config::has($searchKey)) {
return Config::get($fullKey);
}

return Config::get($searchKey);
}
}
8 changes: 8 additions & 0 deletions src/Contracts/CurrentApiKeyFinderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Osiset\ShopifyApp\Contracts;

interface CurrentApiKeyFinderInterface
{
public static function resolve(string $key, $shop = null): ?string;
}
28 changes: 18 additions & 10 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,24 @@ public static function getShopifyConfig(string $key, $shop = null)
);
}

// Check if config API callback is defined
if (Str::startsWith($key, 'api')
&& Arr::exists($config, 'config_api_callback')
&& is_callable($config['config_api_callback'])) {
// It is, use this to get the config value
return call_user_func(
Arr::get($config, 'config_api_callback'),
$key,
$shop
);
// Check if config API callback or class is defined
if (Str::startsWith($key, 'api')) {
if (
Arr::exists($config, 'config_api_callback')
&& is_callable($config['config_api_callback'])
) {
// It is, use this to get the config value
return call_user_func(
Arr::get($config, 'config_api_callback'),
$key,
$shop
);
} else if (
Arr::exists($config, 'config_api_class')
&& class_exists($config['config_api_class'])
) {
return $config['config_api_class']::resolve($key, $shop);
}
}

return Arr::get($config, $key);
Expand Down
27 changes: 27 additions & 0 deletions src/resources/config/shopify-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@
'scripttags' => env('SCRIPTTAGS_JOB_CONNECTION', null),
'after_authenticate' => env('AFTER_AUTHENTICATE_JOB_CONNECTION', null),
],

/*
|--------------------------------------------------------------------------
| Config API Callback
Expand All @@ -487,11 +488,37 @@
| A closure/callable is required.
| The first argument will be the key string.
| The second argument will be something to help identify the shop.
|
| This will break caching config values because Closures can not be serialized.
| Use config_api_class below instead.
|
*/

'config_api_callback' => null,

/*
|--------------------------------------------------------------------------
| Config API Resolver Class
|--------------------------------------------------------------------------
|
| This option can be used to modify what returns when `getConfig('api_*')`
| is used. A use-case for this is modifying the return of `api_secret`
| or something similar.
|
| A class name is required
| The class must implement Osiset\ShopifyApp\Contracts\CurrentApiKeyFinderInterface
| A default class is provided and can be uncommented below.
|
| config_api_callback will take priority for backwards-compatibility however,
| this option is the recommended way because closures can not be serialized.
|
*/

// 'config_api_class' => Osiset\ShopifyApp\ApiKeyFinder\CurrentApiKeyFinder::class,
// 'config_api_shop_keys' => [
// 'api_key_shop-name' => env('API_KEY_SHOPNAME', ''),
// ],

/*
|--------------------------------------------------------------------------
| Customize Models and Table Name
Expand Down