|
2 | 2 |
|
3 | 3 | namespace TelegramGithubNotify\App\Services; |
4 | 4 |
|
| 5 | +use TelegramGithubNotify\App\Models\Setting; |
| 6 | + |
5 | 7 | class SettingService extends AppService |
6 | 8 | { |
| 9 | + public Setting $setting; |
| 10 | + |
| 11 | + public array $settingConfig = []; |
| 12 | + |
| 13 | + public function __construct() |
| 14 | + { |
| 15 | + parent::__construct(); |
| 16 | + $this->setting = new Setting(); |
| 17 | + $this->settingConfig = $this->setting->getSettingConfig(); |
| 18 | + } |
7 | 19 | /** |
8 | 20 | * @return void |
9 | 21 | */ |
10 | 22 | public function settingHandle(): void |
11 | 23 | { |
12 | | - $settings = setting_config(); |
13 | | - |
14 | | - if ($settings['is_notified']) { |
15 | | - $notificationSetting = $this->telegram->buildInlineKeyBoardButton('🔕 Disable Notification', '', 'setting.disable_notification'); |
| 24 | + if ($this->settingConfig['is_notified']) { |
| 25 | + $notificationSetting = $this->telegram->buildInlineKeyBoardButton('❌ Notification', '', 'setting.is_notified'); |
16 | 26 | } else { |
17 | | - $notificationSetting = $this->telegram->buildInlineKeyBoardButton('🔔 Enable Notification', '', 'setting.enable_notification'); |
| 27 | + $notificationSetting = $this->telegram->buildInlineKeyBoardButton('✅ Notification', '', 'setting.is_notified'); |
18 | 28 | } |
19 | 29 |
|
20 | | - if ($settings['enable_all_event']) { |
21 | | - $eventSetting = $this->telegram->buildInlineKeyBoardButton('🔕 Disable All Events', '', 'setting.disable_all_events'); |
| 30 | + if ($this->settingConfig['all_events_notify']) { |
| 31 | + $eventSetting = $this->telegram->buildInlineKeyBoardButton('🔕 All Events Notify', '', 'setting.all_events_notify'); |
22 | 32 | } else { |
23 | | - $eventSetting = $this->telegram->buildInlineKeyBoardButton('🔔 Enable All Events', '', 'setting.enable_all_events'); |
| 33 | + $eventSetting = $this->telegram->buildInlineKeyBoardButton('🔔 All Events Notify', '', 'setting.all_events_notify'); |
24 | 34 | } |
25 | 35 |
|
26 | 36 | $keyboard = [ |
27 | 37 | [ |
28 | 38 | $notificationSetting, |
29 | 39 | ], [ |
30 | 40 | $eventSetting, |
31 | | - $this->telegram->buildInlineKeyBoardButton('Check Events', '', 'setting.check_events'), |
32 | | - ], |
| 41 | + $this->telegram->buildInlineKeyBoardButton('Custom individual events', '', 'setting.custom_events'), |
| 42 | + ], [ |
| 43 | + $this->telegram->buildInlineKeyBoardButton('🔙 Back', '', 'back.menu'), |
| 44 | + ] |
33 | 45 | ]; |
34 | 46 |
|
35 | 47 | $this->sendMessage(view('tools.settings'), ['reply_markup' => $keyboard]); |
36 | 48 | } |
37 | 49 |
|
38 | | - public function settingCallbackHandler(string $callback) |
| 50 | + /** |
| 51 | + * @param string $callback |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function settingCallbackHandler(string $callback): void |
39 | 55 | { |
| 56 | + if ($callback === 'setting.custom_events') { |
| 57 | + (new EventService())->eventHandle(); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + $callback = str_replace('setting.', '', $callback); |
| 62 | + |
| 63 | + $this->updateSetting($callback, !$this->settingConfig[$callback]); |
| 64 | + $this->settingHandle(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param string $settingName |
| 69 | + * @param $settingValue |
| 70 | + * @return bool |
| 71 | + */ |
| 72 | + public function updateSetting(string $settingName, $settingValue = null): bool |
| 73 | + { |
| 74 | + $keys = explode('.', $settingName); |
| 75 | + $lastKey = array_pop($keys); |
| 76 | + $nestedSettings = &$this->settingConfig; |
| 77 | + |
| 78 | + foreach ($keys as $key) { |
| 79 | + if (!isset($nestedSettings[$key]) || !is_array($nestedSettings[$key])) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + $nestedSettings = &$nestedSettings[$key]; |
| 83 | + } |
| 84 | + |
| 85 | + if (isset($nestedSettings[$lastKey])) { |
| 86 | + $nestedSettings[$lastKey] = $settingValue ?? !$nestedSettings[$lastKey]; |
| 87 | + if ($this->saveSettingsToFile()) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @return bool |
| 97 | + */ |
| 98 | + private function saveSettingsToFile(): bool |
| 99 | + { |
| 100 | + $json = json_encode($this->settingConfig, JSON_PRETTY_PRINT); |
| 101 | + if (file_exists(Setting::SETTING_FILE)) { |
| 102 | + file_put_contents(Setting::SETTING_FILE, $json, LOCK_EX); |
| 103 | + |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + return false; |
40 | 108 | } |
41 | 109 | } |
0 commit comments