Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #18

Merged
merged 10 commits into from
Oct 19, 2023
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 13 additions & 1 deletion config/tg-notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@
],

'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',
],

'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',
],
],

'view' => [
'path' => $_ENV['TGN_VIEW_PATH'] ??
'resources/views',
Expand Down
108 changes: 108 additions & 0 deletions src/Bot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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\Interfaces\Structures\SettingInterface;
use LbilTech\TelegramGitNotifier\Models\Event;
use LbilTech\TelegramGitNotifier\Models\Setting;
use LbilTech\TelegramGitNotifier\Structures\App;
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 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);
}

public function updateSetting(?string $settingFile = null): void
{
if ($this->setting->getSettingFile()) {
return;
}
$settingFile = $settingFile ?? config('telegram-git-notifier.data_file.setting');
$this->setting->setSettingFile($settingFile);
$this->setting->setSettingConfig();
}

public function setMyCommands(
array $menuCommand,
?string $view = null
): void {
$this->telegram->setMyCommands([
'commands' => json_encode($menuCommand)
]);
$this->sendMessage(
view(
$view ??
config('telegram-git-notifier.view.tools.set_menu_cmd')
)
);
}

public function isCallback(): bool
{
if ($this->telegram->getUpdateType() === Telegram::CALLBACK_QUERY) {
return true;
}

return false;
}

public function isMessage(): bool
{
if ($this->telegram->getUpdateType() === Telegram::MESSAGE) {
return true;
}

return false;
}

public function isOwner(): bool
{
if ($this->telegram->ChatID() == $this->chatBotId) {
return true;
}

return false;
}

public function isNotifyChat(): bool
{
$chatIds = config('telegram-git-notifier.bot.notify_chat_ids');
if (in_array($this->telegram->ChatID(), $chatIds)) {
return true;
}

return false;
}
}
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
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;
}
Loading