Skip to content

Commit

Permalink
Merge pull request #18 from tanhongit/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
tanhongit authored Oct 19, 2023
2 parents 6cb3148 + 5a4c9d2 commit 329d324
Show file tree
Hide file tree
Showing 33 changed files with 423 additions and 234 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 32 additions & 5 deletions config/tg-notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,53 @@
'telegram-git-notifier' => [
'app' => [
'name' => $_ENV['TGN_APP_NAME'] ?? 'Telegram Git Notifier',

// Required for the bot to work properly
'url' => $_ENV['TGN_APP_URL'] ?? 'http://localhost:3000',

'timezone' => $_ENV['TIMEZONE'] ?? 'Asia/Ho_Chi_Minh',
],

'bot' => [
'token' => $_ENV['TELEGRAM_BOT_TOKEN'] ?? '',
'chat_id' => $_ENV['TELEGRAM_BOT_CHAT_ID'] ?? '',
'notify_chat_ids' => explode(
',',
$_ENV['TELEGRAM_NOTIFY_CHAT_IDS'] ?? ''
),

/**
* Set the chat ids that will receive notifications
* You can add the owner bot id, group id, ...
* -------------------------------------------------------
* Note:
* Please use semicolon ";" to separate chat ids
* And use colon ":" to separate chat id and thread id
* And use comma "," if you want to add multiple thread ids
* -------------------------------------------------------
* The environment variable is expected to be in the format:
* "chat_id1;chat_id2:thread_id2;chat_id3:thread_id3_1,thread_id3_2;..."
*/
'notify_chat_ids' => $_ENV['TELEGRAM_NOTIFY_CHAT_IDS'] ?? '',
],

'author' => [
'discussion' => $_ENV['TGN_AUTHOR_DISCUSSION'] ??
'discussion' => $_ENV['TGN_AUTHOR_DISCUSSION'] ??
'https://github.com/lbiltech/telegram-git-notifier/discussions',
'source_code' => $_ENV['TGN_AUTHOR_SOURCE_CODE'] ??
'https://github.com/lbiltech/telegram-git-notifier',
],

/** Set the path to the data file */
'data_file' => [
'setting' => $_ENV['TGN_PATH_SETTING'] ??
'storage/json/tgn/tgn-settings.json',

'platform' => [
'gitlab' => $_ENV['TGN_PATH_PLATFORM_GITLAB'] ??
'storage/json/tgn/gitlab-events.json',
'github' => $_ENV['TGN_PATH_PLATFORM_GITHUB'] ??
'storage/json/tgn/github-events.json',
],
],

/** Set the path to the view file */
'view' => [
'path' => $_ENV['TGN_VIEW_PATH'] ??
'resources/views',
Expand Down
48 changes: 48 additions & 0 deletions src/Bot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace LbilTech\TelegramGitNotifier;

use LbilTech\TelegramGitNotifier\Constants\EventConstant;
use LbilTech\TelegramGitNotifier\Interfaces\BotInterface;
use LbilTech\TelegramGitNotifier\Interfaces\Structures\AppInterface;
use LbilTech\TelegramGitNotifier\Interfaces\EventInterface;
use LbilTech\TelegramGitNotifier\Models\Event;
use LbilTech\TelegramGitNotifier\Models\Setting;
use LbilTech\TelegramGitNotifier\Structures\App;
use LbilTech\TelegramGitNotifier\Structures\TelegramBot;
use LbilTech\TelegramGitNotifier\Trait\BotSettingTrait;
use LbilTech\TelegramGitNotifier\Trait\EventSettingTrait;
use LbilTech\TelegramGitNotifier\Trait\EventTrait;
use Telegram;

class Bot implements AppInterface, BotInterface, EventInterface
{
use App;
use TelegramBot;

use EventTrait;
use BotSettingTrait;
use EventSettingTrait;

public Event $event;

public Setting $setting;

public function __construct(
Telegram $telegram = null,
?string $chatBotId = null,
Setting $setting = null,
Event $event = null,
?string $settingFile = null,
?string $platform = EventConstant::DEFAULT_PLATFORM,
?string $platformFile = 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->setting = $setting ?? new Setting();
$this->updateSetting($settingFile);
}
}
2 changes: 1 addition & 1 deletion src/Constants/EventConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LbilTech\TelegramGitNotifier\Constants;

class EventConstant
final class EventConstant
{
public const DEFAULT_PLATFORM = 'github';

Expand Down
15 changes: 15 additions & 0 deletions src/Constants/NotificationConstant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace LbilTech\TelegramGitNotifier\Constants;

final class NotificationConstant
{
// Separation between chat id pairs
public const CHAT_ID_PAIRS_SEPARATOR = ';';

// Separation between chat id and thread id
public const CHAT_THREAD_ID_SEPARATOR = ':';

// Separation between thread id
public const THREAD_ID_SEPARATOR = ',';
}
2 changes: 1 addition & 1 deletion src/Constants/SettingConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LbilTech\TelegramGitNotifier\Constants;

class SettingConstant
final class SettingConstant
{
public const SETTING_PREFIX = 'stg.';

Expand Down
8 changes: 3 additions & 5 deletions src/Exceptions/CallbackException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

namespace LbilTech\TelegramGitNotifier\Exceptions;

use Exception;

class CallbackException extends Exception
final class CallbackException extends TelegramGitNotifierException
{
public static function isEmpty(): self
{
return new static('Callback is empty');
return new self('Callback is empty');
}

public static function invalid(): self
{
return new static('Callback is invalid');
return new self('Callback is invalid');
}
}
10 changes: 4 additions & 6 deletions src/Exceptions/EntryNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

namespace LbilTech\TelegramGitNotifier\Exceptions;

use Exception;

class EntryNotFoundException extends Exception
final class EntryNotFoundException extends TelegramGitNotifierException
{
public static function fileNotFound(): self
{
return new static('File not found');
return new self('File not found');
}

public static function configNotFound($config): self
{
return new static("Config {$config} not found");
return new self("Config {$config} not found");
}

public static function viewNotFound($view): self
{
return new static("View {$view} not found");
return new self("View {$view} not found");
}
}
3 changes: 1 addition & 2 deletions src/Exceptions/InvalidViewTemplateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace LbilTech\TelegramGitNotifier\Exceptions;

use Exception;
use Throwable;

class InvalidViewTemplateException extends Exception
final class InvalidViewTemplateException extends TelegramGitNotifierException
{
public static function create(
string $view,
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/MessageIsEmptyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace LbilTech\TelegramGitNotifier\Exceptions;

use Exception;

class MessageIsEmptyException extends Exception
final class MessageIsEmptyException extends TelegramGitNotifierException
{
public static function create(): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/SendNotificationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace LbilTech\TelegramGitNotifier\Exceptions;

use Exception;

class SendNotificationException extends Exception
final class SendNotificationException extends TelegramGitNotifierException
{
public static function create(): self
{
Expand Down
18 changes: 18 additions & 0 deletions src/Exceptions/TelegramGitNotifierException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace LbilTech\TelegramGitNotifier\Exceptions;

use Exception;

class TelegramGitNotifierException extends Exception
{
public static function isEmpty(): self
{
return new static('Telegram Git Notifier is empty');
}

public static function invalid(): self
{
return new static('Telegram Git Notifier is invalid');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException;

interface TelegramInterface
interface BotInterface
{
/**
* Set the menu button for a telegram
Expand Down
37 changes: 23 additions & 14 deletions src/Interfaces/EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,39 @@

namespace LbilTech\TelegramGitNotifier\Interfaces;

use LbilTech\TelegramGitNotifier\Trait\ActionEventTrait;
use Symfony\Component\HttpFoundation\Request;

interface EventInterface
{
/**
* Validate access event before send notify
* Get action name of event from payload data
*
* @param string $platform Source code platform (GitHub, GitLab)
* @param string $event Event name (push, pull_request)
* @param $payload
*
* @return bool
* @return string
* @see ActionEventTrait::getActionOfEvent()
*/
public function validateAccessEvent(
string $platform,
string $event,
$payload
): bool;
public function getActionOfEvent($payload): string;

/**
* Get action name of event from payload data
* Set platform and platform file for event
*
* @param $payload
* @param string $platform
* @param string|null $platformFile
*
* @return string
* @see ActionEventTrait::getActionOfEvent()
* @return void
* @see EventTrait::setPlatFormForEvent()
*/
public function getActionOfEvent($payload): string;
public function setPlatFormForEvent(string $platform, string $platformFile = null): void;

/**
* Set event config and get event name
*
* @param Request $request
*
* @return string|null
* @see EventTrait::handleEventFromRequest()
*/
public function handleEventFromRequest(Request $request): ?string;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace LbilTech\TelegramGitNotifier\Interfaces;
namespace LbilTech\TelegramGitNotifier\Interfaces\Structures;

use LbilTech\TelegramGitNotifier\Exceptions\EntryNotFoundException;
use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
Expand All @@ -15,6 +15,7 @@ interface AppInterface
*
* @return void
* @throws MessageIsEmptyException
* @see App::sendMessage()
*/
public function sendMessage(
string $message = '',
Expand All @@ -29,6 +30,7 @@ public function sendMessage(
*
* @return void
* @throws EntryNotFoundException
* @see App::sendPhoto()
*/
public function sendPhoto(string $photo = '', string $caption = ''): void;

Expand All @@ -39,6 +41,7 @@ public function sendPhoto(string $photo = '', string $caption = ''): void;
*
* @return void
* @throws MessageIsEmptyException
* @see App::answerCallbackQuery()
*/
public function answerCallbackQuery(string $text = null): void;

Expand All @@ -49,6 +52,7 @@ public function answerCallbackQuery(string $text = null): void;
* @param array $options
*
* @return void
* @see App::editMessageText()
*/
public function editMessageText(
?string $text = null,
Expand All @@ -61,13 +65,15 @@ public function editMessageText(
* @param array $options
*
* @return void
* @see App::editMessageReplyMarkup()
*/
public function editMessageReplyMarkup(array $options = []): void;

/**
* Get the text from callback message
*
* @return string
* @see App::getCallbackTextMessage()
*/
public function getCallbackTextMessage(): string;

Expand All @@ -77,27 +83,31 @@ public function getCallbackTextMessage(): string;
* @param array $options
*
* @return array
* @see App::setCallbackContentMessage()
*/
public function setCallbackContentMessage(array $options = []): array;

/**
* @param string $chatId
*
* @return void
* @see App::setCurrentChatBotId()
*/
public function setCurrentChatId(string $chatId): void;
public function setCurrentChatBotId(string $chatId): void;

/**
* Get the username of the bot
*
* @return string|null
* @see App::getBotName()
*/
public function getBotName(): ?string;

/**
* Get the command message from a telegram
*
* @return string
* @see App::getCommandMessage()
*/
public function getCommandMessage(): string;
}
Loading

0 comments on commit 329d324

Please sign in to comment.