From f507dce796e7787c986cd9cd56ab0547b1f3f96d Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 7 Nov 2023 23:13:16 +0700 Subject: [PATCH] fix: update exception for config files --- src/Bot.php | 26 ++++++++++++++++++++++++-- src/Exceptions/ConfigFileException.php | 16 ++++++++++++++++ src/Interfaces/BotInterface.php | 7 +++++++ src/Interfaces/EventInterface.php | 9 ++++++++- src/Notifier.php | 18 +++++++++++++++--- src/Trait/EventTrait.php | 13 ++++++++++++- tests/ConfigTest.php | 14 ++++++++++++++ 7 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 src/Exceptions/ConfigFileException.php diff --git a/src/Bot.php b/src/Bot.php index 63eb059..1430503 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -3,6 +3,7 @@ namespace CSlant\TelegramGitNotifier; use CSlant\TelegramGitNotifier\Constants\EventConstant; +use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException; use CSlant\TelegramGitNotifier\Interfaces\BotInterface; use CSlant\TelegramGitNotifier\Interfaces\EventInterface; use CSlant\TelegramGitNotifier\Interfaces\SettingInterface; @@ -29,6 +30,17 @@ class Bot implements AppInterface, BotInterface, EventInterface, SettingInterfac public Setting $setting; + /** + * @param Telegram|null $telegram + * @param string|null $chatBotId + * @param Event|null $event + * @param string|null $platform + * @param string|null $platformFile + * @param Setting|null $setting + * @param string|null $settingFile + * + * @throws ConfigFileException + */ public function __construct( Telegram $telegram = null, ?string $chatBotId = null, @@ -38,12 +50,22 @@ public function __construct( Setting $setting = null, ?string $settingFile = null, ) { - $this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); - $this->setCurrentChatBotId($chatBotId); $this->event = $event ?? new Event(); $this->setPlatFormForEvent($platform, $platformFile); + $this->validatePlatformFile(); $this->setting = $setting ?? new Setting(); $this->updateSetting($settingFile); + $this->validateSettingFile(); + + $this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); + $this->setCurrentChatBotId($chatBotId); + } + + public function validateSettingFile(): void + { + if (empty($this->setting->getSettingFile())) { + throw ConfigFileException::settingFile($this->setting->getSettingFile()); + } } } diff --git a/src/Exceptions/ConfigFileException.php b/src/Exceptions/ConfigFileException.php new file mode 100644 index 0000000..ef7f731 --- /dev/null +++ b/src/Exceptions/ConfigFileException.php @@ -0,0 +1,16 @@ +telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); - $this->setCurrentChatBotId($chatBotId); - $this->event = $event ?? new Event(); $this->setPlatFormForEvent($platform, $platformFile); + $this->validatePlatformFile(); + + $this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); + $this->setCurrentChatBotId($chatBotId); $this->client = $client ?? new Client(); } diff --git a/src/Trait/EventTrait.php b/src/Trait/EventTrait.php index 049431c..b6aed21 100644 --- a/src/Trait/EventTrait.php +++ b/src/Trait/EventTrait.php @@ -3,13 +3,14 @@ namespace CSlant\TelegramGitNotifier\Trait; use CSlant\TelegramGitNotifier\Constants\EventConstant; +use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException; use Symfony\Component\HttpFoundation\Request; trait EventTrait { use ActionEventTrait; - public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void + public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, ?string $platformFile = null): void { /** @var array $platformFileDefaults */ $platformFileDefaults = config('telegram-git-notifier.data_file.platform'); @@ -31,4 +32,14 @@ public function handleEventFromRequest(Request $request): ?string return null; } + + public function validatePlatformFile(): void + { + if (empty($this->event->getEventConfig())) { + throw ConfigFileException::platformFile( + $this->event->platform, + $this->event->getPlatformFile() + ); + } + } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index ef343ca..3c14054 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -37,3 +37,17 @@ expect($this->bot->event->getEventConfig())->toBeArray(); expect($this->bot->event->getEventConfig())->toHaveKey('tag_push'); }); + +it('setting file is valid', function () { + $this->bot->updateSetting(); + $this->bot->validateSettingFile(); + + expect($this->bot->setting->getSettings())->toBeArray(); +}); + +it('platform file is valid', function () { + $this->bot->setPlatFormForEvent(); + $this->bot->validatePlatformFile(); + + expect($this->bot->event->getEventConfig())->toBeArray(); +});