diff --git a/LICENSE b/LICENSE index 7e4cce4..792c300 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Lbil Technologies +Copyright (c) 2023 CSlant Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal 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/Structures/Notification.php b/src/Structures/Notification.php index 4f60e61..6ab09d8 100644 --- a/src/Structures/Notification.php +++ b/src/Structures/Notification.php @@ -94,11 +94,11 @@ public function sendNotify(string $message = null, array $options = []): bool return true; } - throw SendNotificationException::create(); + $body = (string) $response->getBody(); + + throw SendNotificationException::create($body); } catch (GuzzleException $e) { - error_log($e->getMessage()); + throw SendNotificationException::create($e->getMessage()); } - - return false; } } 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(); +});