Skip to content

Commit

Permalink
Merge pull request cslant#20 from tanhongit/refactor
Browse files Browse the repository at this point in the history
Fix bot setting feature
  • Loading branch information
tanhongit authored Oct 20, 2023
2 parents 72cb669 + 3b5466e commit 9467199
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 56 deletions.
7 changes: 4 additions & 3 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LbilTech\TelegramGitNotifier\Constants\EventConstant;
use LbilTech\TelegramGitNotifier\Interfaces\BotInterface;
use LbilTech\TelegramGitNotifier\Interfaces\SettingInterface;
use LbilTech\TelegramGitNotifier\Interfaces\Structures\AppInterface;
use LbilTech\TelegramGitNotifier\Interfaces\EventInterface;
use LbilTech\TelegramGitNotifier\Models\Event;
Expand All @@ -15,7 +16,7 @@
use LbilTech\TelegramGitNotifier\Trait\EventTrait;
use Telegram;

class Bot implements AppInterface, BotInterface, EventInterface
class Bot implements AppInterface, BotInterface, EventInterface, SettingInterface
{
use App;
use TelegramBot;
Expand All @@ -31,11 +32,11 @@ class Bot implements AppInterface, BotInterface, EventInterface
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,
Setting $setting = null,
?string $settingFile = null,
) {
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

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

use LbilTech\TelegramGitNotifier\Constants\EventConstant;
use LbilTech\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use LbilTech\TelegramGitNotifier\Trait\BotSettingTrait;
use LbilTech\TelegramGitNotifier\Trait\EventSettingTrait;

interface SettingInterface
{
Expand All @@ -15,11 +17,9 @@ interface SettingInterface
* @param string $platform
*
* @return array
* @see EventSettingTrait::eventMarkup()
*/
public function eventMarkup(
?string $parentEvent = null,
string $platform = EventConstant::DEFAULT_PLATFORM
): array;
public function eventMarkup(?string $parentEvent = null, string $platform = EventConstant::DEFAULT_PLATFORM): array;

/**
* Get callback data for markup
Expand All @@ -30,13 +30,9 @@ public function eventMarkup(
* @param string|null $parentEvent
*
* @return string
* @see EventSettingTrait::getCallbackData()
*/
public function getCallbackData(
string $event,
string $platform,
array|bool $value = false,
?string $parentEvent = null
): string;
public function getCallbackData(string $event, string $platform, array|bool $value = false, ?string $parentEvent = null): string;

/**
* Get event name for markup
Expand All @@ -45,6 +41,7 @@ public function getCallbackData(
* @param $value
*
* @return string
* @see EventSettingTrait::getEventName()
*/
public function getEventName(string $event, $value): string;

Expand All @@ -55,38 +52,32 @@ public function getEventName(string $event, $value): string;
* @param string|null $parentEvent
*
* @return array
* @see EventSettingTrait::getEndKeyboard()
*/
public function getEndKeyboard(
string $platform,
?string $parentEvent = null
): array;
public function getEndKeyboard(string $platform, ?string $parentEvent = null): array;

/**
* Handle event callback settings
*
* @param string|null $callback
* @param string|null $platform
* @param string|null $platformFile
*
* @return void
* @throws InvalidViewTemplateException
* @see EventSettingTrait::eventHandle()
*/
public function eventHandle(
?string $callback = null,
?string $platform = null
): void;

public function eventHandle(?string $callback = null, ?string $platform = null, string $platformFile = null): void;
/**
* Get the platform from callback
*
* @param string|null $callback
* @param string|null $platform
*
* @return string
* @see EventSettingTrait::getPlatformFromCallback()
*/
public function getPlatformFromCallback(
?string $callback,
?string $platform
): string;
public function getPlatformFromCallback(?string $callback, ?string $platform): string;

/**
* First event settings
Expand All @@ -96,20 +87,18 @@ public function getPlatformFromCallback(
* @param string|null $view
*
* @return bool
* @see EventSettingTrait::sendSettingEventMessage()
* @throws InvalidViewTemplateException
*/
public function sendSettingEventMessage(
string $platform,
?string $callback = null,
?string $view = null
): bool;
public function sendSettingEventMessage(string $platform, ?string $callback = null, ?string $view = null): bool;

/**
* Get event name from callback
*
* @param string|null $callback
*
* @return string
* @see EventSettingTrait::getEventFromCallback()
*/
public function getEventFromCallback(?string $callback): string;

Expand All @@ -121,6 +110,7 @@ public function getEventFromCallback(?string $callback): string;
* @param string|null $view
*
* @return bool
* @see EventSettingTrait::handleEventWithActions()
* @throws InvalidViewTemplateException
*/
public function handleEventWithActions(
Expand All @@ -134,29 +124,34 @@ public function handleEventWithActions(
*
* @param string $event
* @param string $platform
* @param string|null $platFormFile
*
* @return void
* @throws InvalidViewTemplateException
* @see EventSettingTrait::handleEventUpdate()
*/
public function handleEventUpdate(string $event, string $platform): void;
public function handleEventUpdate(string $event, string $platform, string $platFormFile = null): void;

/**
* Handle event update
*
* @param string $event
* @param string $platform
* @param string|null $platFormFile
*
* @return void
* @throws InvalidViewTemplateException
* @see EventSettingTrait::eventUpdateHandle()
*/
public function eventUpdateHandle(string $event, string $platform): void;
public function eventUpdateHandle(string $event, string $platform, string $platFormFile = null): void;

/**
* Send a setting message
*
* @param string|null $view
*
* @return void
* @see BotSettingTrait::settingHandle()
* @throws MessageIsEmptyException
*/
public function settingHandle(?string $view = null): void;
Expand All @@ -165,13 +160,15 @@ public function settingHandle(?string $view = null): void;
* Generate setting markup
*
* @return array[]
* @see BotSettingTrait::settingMarkup()
*/
public function settingMarkup(): array;

/**
* @param array $markup
*
* @return array
* @see BotSettingTrait::customEventMarkup()
*/
public function customEventMarkup(array $markup): array;
}
4 changes: 2 additions & 2 deletions src/Interfaces/Structures/AppInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public function sendMessage(
* Send a photo to telegram
*
* @param string $photo (path to photo)
* @param string $caption
* @param array $caption
*
* @return void
* @throws EntryNotFoundException
* @see App::sendPhoto()
*/
public function sendPhoto(string $photo = '', string $caption = ''): void;
public function sendPhoto(string $photo = '', array $caption = []): void;

/**
* Send callback response to telegram (show alert)
Expand Down
5 changes: 3 additions & 2 deletions src/Structures/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ public function sendMessage(string $message = '', array $options = []): void
$this->telegram->sendMessage($content);
}

public function sendPhoto(string $photo = '', string $caption = ''): void
public function sendPhoto(string $photo = '', array $options = []): void
{
if (empty($photo)) {
throw EntryNotFoundException::fileNotFound();
}

$content = $this->createTelegramBaseContent();
$content['photo'] = curl_file_create($photo);
$content['caption'] = $caption;

$content = $content + $options;

$this->telegram->sendPhoto($content);
}
Expand Down
38 changes: 25 additions & 13 deletions src/Trait/EventSettingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ trait EventSettingTrait
{
public function eventMarkup(
?string $parentEvent = null,
string $platform = EventConstant::DEFAULT_PLATFORM
string $platform = EventConstant::DEFAULT_PLATFORM,
string $platformFile = null
): array {
$replyMarkup = $replyMarkupItem = [];

$this->event->setEventConfig($platform);
$this->setPlatFormForEvent($platform, $platformFile);

$events = $parentEvent === null ? $this->event->getEventConfig()
: $this->event->getEventConfig()[$parentEvent];

Expand Down Expand Up @@ -81,8 +83,7 @@ public function getEndKeyboard(
$back = SettingConstant::SETTING_BACK_TO_SETTINGS_MENU;

if ($parentEvent) {
$back = SettingConstant::SETTING_BACK_TO_EVENTS_MENU
. $platform;
$back = SettingConstant::SETTING_BACK_TO_EVENTS_MENU . ".$platform";
}

return [
Expand All @@ -97,7 +98,8 @@ public function getEndKeyboard(

public function eventHandle(
?string $callback = null,
?string $platform = null
?string $platform = null,
?string $platFormFile = null
): void {
$platform = $this->getPlatformFromCallback($callback, $platform);

Expand All @@ -111,7 +113,7 @@ public function eventHandle(
return;
}

$this->handleEventUpdate($event, $platform);
$this->handleEventUpdate($event, $platform, $platFormFile);
}

public function getPlatformFromCallback(
Expand Down Expand Up @@ -186,23 +188,33 @@ public function handleEventWithActions(
return false;
}

public function handleEventUpdate(string $event, string $platform): void
{
public function handleEventUpdate(
string $event,
string $platform,
string $platFormFile = null
): void {
if (str_contains($event, EventConstant::EVENT_UPDATE_SEPARATOR)) {
$event = str_replace(
EventConstant::EVENT_UPDATE_SEPARATOR,
'',
$event
);
$this->eventUpdateHandle($event, $platform);
$this->eventUpdateHandle($event, $platform, $platFormFile);
}
}

public function eventUpdateHandle(string $event, string $platform): void
{
[$event, $action] = explode('.', $event);
public function eventUpdateHandle(
string $event,
string $platform,
string $platFormFile = null
): void {
if (str_contains($event, '.')) {
[$event, $action] = explode('.', $event);
} else {
$action = null;
}

$this->event->setEventConfig($platform);
$this->setPlatFormForEvent($platform);
$this->event->updateEvent($event, $action);
$this->eventHandle(
$action
Expand Down
8 changes: 3 additions & 5 deletions src/Trait/EventTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ trait EventTrait

public function setPlatFormForEvent(string $platform, string $platformFile = null): void
{
if (!$this->event->getPlatformFile()) {
/** @var array $platformFileDefaults */
$platformFileDefaults = config('telegram-git-notifier.data_file.platform');
$this->event->setPlatformFile($platformFile ?? $platformFileDefaults[$platform]);
}
/** @var array $platformFileDefaults */
$platformFileDefaults = config('telegram-git-notifier.data_file.platform');
$this->event->setPlatformFile($platformFile ?? $platformFileDefaults[$platform]);
$this->event->setEventConfig($platform);
}

Expand Down

0 comments on commit 9467199

Please sign in to comment.