Skip to content

Commit

Permalink
refactor: structures for notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhongit committed Oct 17, 2023
1 parent 90e1697 commit 4e77922
Show file tree
Hide file tree
Showing 21 changed files with 290 additions and 167 deletions.
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
49 changes: 36 additions & 13 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,56 @@

namespace LbilTech\TelegramGitNotifier;

use LbilTech\TelegramGitNotifier\Constants\EventConstant;
use LbilTech\TelegramGitNotifier\Interfaces\BotInterface;
use LbilTech\TelegramGitNotifier\Interfaces\Structures\AppInterface;
use LbilTech\TelegramGitNotifier\Interfaces\Structures\EventInterface;
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\Structures\Event;
use LbilTech\TelegramGitNotifier\Structures\Setting;
use LbilTech\TelegramGitNotifier\Trait\BotSettingTrait;
use LbilTech\TelegramGitNotifier\Trait\EventSettingTrait;
use LbilTech\TelegramGitNotifier\Trait\ValidationEventTrait;
use LbilTech\TelegramGitNotifier\Trait\ActionEventTrait;
use LbilTech\TelegramGitNotifier\Trait\EventTrait;
use Telegram;

class Bot implements AppInterface, EventInterface, SettingInterface, BotInterface
class Bot implements AppInterface, BotInterface, EventInterface
{
use App;
use Event;
use Setting;
use ValidationEventTrait;
use ActionEventTrait;
use EventTrait;
use BotSettingTrait;
use EventSettingTrait;

public function __construct(Telegram $telegram = null)
{
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();
$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(
Expand Down
55 changes: 55 additions & 0 deletions src/Interfaces/EventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace LbilTech\TelegramGitNotifier\Interfaces;

use LbilTech\TelegramGitNotifier\Constants\EventConstant;
use Symfony\Component\HttpFoundation\Request;

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

/**
* Get action name of event from payload data
*
* @param $payload
*
* @return string
* @see EventTrait::getActionOfEvent()
*/
public function getActionOfEvent($payload): string;

/**
* Set platform and platform file for event
*
* @param string $platform
* @param string|null $platformFile
*
* @return void
* @see EventTrait::setPlatFormForEvent()
*/
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;
}
68 changes: 0 additions & 68 deletions src/Interfaces/Structures/EventInterface.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Interfaces/Structures/NotificationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function sendNotify(string $chatId, string $message = null): bool;
* @param $payload
*
* @return string
* @see ActionEventTrait::getActionOfEvent()
* @see EventTrait::getActionOfEvent()
*/
public function getActionOfEvent($payload): string;
}
17 changes: 0 additions & 17 deletions src/Interfaces/Structures/SettingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,4 @@ public function settingMarkup(): array;
* @return array
*/
public function customEventMarkup(array $markup): array;

/**
* Update setting item value and save to json file
*
* @param string $settingName
* @param $settingValue
*
* @return bool
*/
public function updateSetting(string $settingName, $settingValue = null): bool;

/**
* Condition for checking the notification status
*
* @return bool
*/
public function isNotified(): bool;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

interface WebhookInterface
{
Expand Down
27 changes: 25 additions & 2 deletions src/Structures/Event.php → src/Models/Event.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
<?php

namespace LbilTech\TelegramGitNotifier\Structures;
namespace LbilTech\TelegramGitNotifier\Models;

use LbilTech\TelegramGitNotifier\Constants\EventConstant;

trait Event
class Event
{
public array $eventConfig = [];

public string $platform = EventConstant::DEFAULT_PLATFORM;

private string $platformFile = '';

/**
* @return string
*/
public function getPlatformFile(): string
{
return $this->platformFile;
}

/**
* @param string $platformFile
*
* @return void
*/
public function setPlatformFile(string $platformFile): void
{
$this->platformFile = $platformFile;
}

/**
* Set event config
*
* @param string $platform
*
* @return void
*/
public function setEventConfig(
string $platform = EventConstant::DEFAULT_PLATFORM
): void {
Expand All @@ -31,6 +46,14 @@ public function setEventConfig(
$this->eventConfig = json_decode($json, true);
}

/**
* Update event config by event and action
*
* @param string $event
* @param string|null $action
*
* @return void
*/
public function updateEvent(string $event, string|null $action): void
{
if (!empty($action)) {
Expand Down
19 changes: 16 additions & 3 deletions src/Structures/Setting.php → src/Models/Setting.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace LbilTech\TelegramGitNotifier\Structures;
namespace LbilTech\TelegramGitNotifier\Models;

use LbilTech\TelegramGitNotifier\Constants\SettingConstant;

trait Setting
class Setting
{
private array $settings = [];
public array $settings = [];

private string $settingFile = '';

Expand Down Expand Up @@ -61,6 +61,11 @@ public function isAllEventsNotification(): bool
return false;
}

/**
* Condition for checking the notification status
*
* @return bool
*/
public function isNotified(): bool
{
if (!empty($this->settings)
Expand All @@ -72,6 +77,14 @@ public function isNotified(): bool
return false;
}

/**
* Update setting item value and save to file
*
* @param string $settingName
* @param $settingValue
*
* @return bool
*/
public function updateSetting(
string $settingName,
$settingValue = null
Expand Down
Loading

0 comments on commit 4e77922

Please sign in to comment.