Skip to content

Commit

Permalink
fix: update exception for config files
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhongit committed Nov 7, 2023
1 parent 1e073b8 commit f507dce
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 7 deletions.
26 changes: 24 additions & 2 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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());
}
}
}
16 changes: 16 additions & 0 deletions src/Exceptions/ConfigFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace CSlant\TelegramGitNotifier\Exceptions;

final class ConfigFileException extends TelegramGitNotifierException
{
public static function settingFile(?string $settingFile = null): self
{
return new self('Something went wrong while reading settings file. Check your settings file path: ' . ($settingFile ?? 'null'));
}

public static function platformFile(?string $platform = null, ?string $platformFile = null): self
{
return new self('Something went wrong while reading platform file. Check your platform file path: ' . ($platformFile ?? 'null') . ' for platform: ' . ($platform ?? 'null'));
}
}
7 changes: 7 additions & 0 deletions src/Interfaces/BotInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CSlant\TelegramGitNotifier\Interfaces;

use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;

interface BotInterface
Expand Down Expand Up @@ -46,4 +47,10 @@ public function isOwner(): bool;
* @return bool
*/
public function isNotifyChat(): bool;

/**
* @return void
* @throws ConfigFileException
*/
public function validateSettingFile(): void;
}
9 changes: 8 additions & 1 deletion src/Interfaces/EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CSlant\TelegramGitNotifier\Interfaces;

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Trait\ActionEventTrait;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -27,7 +28,7 @@ public function getActionOfEvent(object $payload): string;
* @return void
* @see EventTrait::setPlatFormForEvent()
*/
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void;
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, ?string $platformFile = null): void;

/**
* Set event config and get event name
Expand All @@ -38,4 +39,10 @@ public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_P
* @see EventTrait::handleEventFromRequest()
*/
public function handleEventFromRequest(Request $request): ?string;

/**
* @return void
* @throws ConfigFileException
*/
public function validatePlatformFile(): void;
}
18 changes: 15 additions & 3 deletions src/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Constants\NotificationConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Interfaces\EventInterface;
use CSlant\TelegramGitNotifier\Interfaces\Structures\AppInterface;
use CSlant\TelegramGitNotifier\Interfaces\Structures\NotificationInterface;
Expand All @@ -25,6 +26,16 @@ class Notifier implements AppInterface, NotificationInterface, EventInterface

public Client $client;

/**
* @param Telegram|null $telegram
* @param string|null $chatBotId
* @param Event|null $event
* @param string|null $platform
* @param string|null $platformFile
* @param Client|null $client
*
* @throws ConfigFileException
*/
public function __construct(
Telegram $telegram = null,
?string $chatBotId = null,
Expand All @@ -33,11 +44,12 @@ public function __construct(
?string $platformFile = null,
Client $client = 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->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);

$this->client = $client ?? new Client();
}
Expand Down
13 changes: 12 additions & 1 deletion src/Trait/EventTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<platform, platformFile> */
$platformFileDefaults = config('telegram-git-notifier.data_file.platform');
Expand All @@ -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()
);
}
}
}
14 changes: 14 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit f507dce

Please sign in to comment.