Skip to content

Commit

Permalink
pref: change some hard coded logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thatsgolden committed Jul 15, 2024
1 parent 1b036dd commit 6ad05a2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 33 deletions.
3 changes: 1 addition & 2 deletions js/src/admin/components/GeeTestPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export default class GeeTestPage extends ExtensionPage {
});

// context enabled status
Object.keys(EnumContextEvent).forEach((enumKey) => {
const key = EnumContextEvent[enumKey as keyof typeof EnumContextEvent];
Object.values(EnumContextEvent).forEach((key) => {
const path = AdminSettings.getPath(key);
const value = this.settings[path]();
this.settings[path](value === '1');
Expand Down
3 changes: 2 additions & 1 deletion js/src/common/utils/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export interface Config extends Omit<BaseConfig, 'standalone'> {
}

export const BASE_CONFIG = ['product', 'id', 'key'] as unknown as [keyof Omit<BaseConfig, 'standalone'>];
export const CONFIG = ([] as unknown as [keyof Config]).concat(BASE_CONFIG, 'productService', 'login', 'signup', 'forgot');
export const CONFIG = [...BASE_CONFIG, 'productService', ...Object.values(EnumContextEvent)] as (keyof Config)[];
;

export default class CommonSettings {
static PREFIX = 'ffans-geetest';
Expand Down
60 changes: 37 additions & 23 deletions src/ForumAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Str;
use ReflectionClass;

class ForumAttributes
{
Expand All @@ -36,39 +37,52 @@ public function __construct(SettingsRepositoryInterface $settings)

public function __invoke(ForumSerializer $serializer, $model, array $attributes): array
{
$settingNames = [
$reflectionClass = new ReflectionClass(ContextEvent::class);
$contextEvents = array_values($reflectionClass->getConstants());
$baseSettings = [
self::ProductService,
self::Product,
self::Id,
ContextEvent::Signup . '.' . self::Product,
ContextEvent::Signup . '.' . self::Id,
ContextEvent::Login . '.' . self::Product,
ContextEvent::Login . '.' . self::Id,
ContextEvent::Forgot . '.' . self::Product,
ContextEvent::Forgot . '.' . self::Id,
];

foreach ($settingNames as $name) {
$attributes[Utils::getSettingPath(Str::camel($name))] = $this->settings->get(Utils::getSettingPath($name));
}
// base setting names
$settingNames = array_merge(
$baseSettings,
array_map(function ($event) { return "$event." . self::Product; }, $contextEvents),
array_map(function ($event) { return "$event." . self::Id; }, $contextEvents)
);

$boolSettings = [
ContextEvent::Signup,
ContextEvent::Login,
ContextEvent::Forgot,
ContextEvent::Signup . '.' . self::Standalone,
ContextEvent::Login . '.' . self::Standalone,
ContextEvent::Forgot . '.' . self::Standalone,
];
// bool setting names
$boolSettings = array_merge(
$contextEvents,
array_map(function ($event) { return "$event." . self::Standalone; }, $contextEvents)
);

// init base setting maps
$allSettings = array_map(function ($name) {
return ['name' => $name, 'isBool' => false];
}, $settingNames);

foreach ($boolSettings as $name) {
$attributes[Utils::getSettingPath(Str::camel($name))] = $this->settings->get(Utils::getSettingPath($name)) === '1';
// merge bool setting maps
$allSettings = array_merge($allSettings, array_map(function ($name) {
return ['name' => $name, 'isBool' => true];
}, $boolSettings));

// assignment settings
foreach ($allSettings as $setting) {
$value = $this->settings->get(Utils::getSettingPath($setting['name']));
if ($setting['isBool']) {
$value = $value === '1';
}
$attributes[Utils::getSettingPath(Str::camel($setting['name']))] = $value;
}

// assignment general configured or not
$attributes[Utils::getSettingPath(self::Configured)] = Utils::isExtensionSetup($this->settings);
$attributes[Utils::getSettingPath(ContextEvent::Signup . '.' . self::Configured)] = Utils::isExtensionSetup($this->settings, ContextEvent::Signup);
$attributes[Utils::getSettingPath(ContextEvent::Login . '.' . self::Configured)] = Utils::isExtensionSetup($this->settings, ContextEvent::Login);
$attributes[Utils::getSettingPath(ContextEvent::Forgot . '.' . self::Configured)] = Utils::isExtensionSetup($this->settings, ContextEvent::Forgot);
// assignment context event configured or not
foreach ($contextEvents as $event) {
$attributes[Utils::getSettingPath("$event." . self::Configured)] = Utils::isExtensionSetup($this->settings, $event);
}

return $attributes;
}
Expand Down
30 changes: 23 additions & 7 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,37 @@

use FFans\GeeTest\Enums\ContextEvent;
use Flarum\Settings\SettingsRepositoryInterface;
use ReflectionClass;

class Utils
{
const PREFIX = 'ffans-geetest';

private static $configKeys = [
ContextEvent::Signup => [ContextEvent::Signup . '.product', ContextEvent::Signup . '.id', ContextEvent::Signup . '.key'],
ContextEvent::Login => [ContextEvent::Login . '.product', ContextEvent::Login . '.id', ContextEvent::Login . '.key'],
ContextEvent::Forgot => [ContextEvent::Forgot . '.product', ContextEvent::Forgot . '.id', ContextEvent::Forgot . '.key'],
'default' => ['product_service', 'product', 'id', 'key'],
];
private static function generateConfigKeys(): array
{
$reflectionClass = new ReflectionClass(ContextEvent::class);
$contextEvents = array_values($reflectionClass->getConstants());

$configKeys = [];

foreach ($contextEvents as $event) {
$configKeys[$event] = [
"$event.product",
"$event.id",
"$event.key"
];
}

$configKeys['default'] = ['product_service', 'product', 'id', 'key'];

return $configKeys;
}

public static function isExtensionSetup(SettingsRepositoryInterface $settings, string $type = 'default'): bool
{
return !self::isNil($settings, self::$configKeys[$type]);
$configKeys = self::generateConfigKeys();
$key = $configKeys[$type] ?? $configKeys['default'];
return !self::isNil($settings, $key);
}

private static function isNil(SettingsRepositoryInterface $settings, array $keys)
Expand Down

0 comments on commit 6ad05a2

Please sign in to comment.