Skip to content

Commit

Permalink
Merge pull request #103 from tanhongit/main
Browse files Browse the repository at this point in the history
Refactor and workflows
  • Loading branch information
tanhongit authored Nov 3, 2023
2 parents 0aec3a2 + 0a2784b commit 4195ea1
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 130 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: PHPStan

on: [push, pull_request]

jobs:
phpstan:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/[email protected]
with:
php-version: '8.1'

- name: Checkout code
uses: actions/checkout@v4

- name: Install dependencies
run: |
composer install --no-interaction --no-progress --no-suggest
- name: Run PHPStan
run: |
composer analyse --error-format=github
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
"homepage": "https://github.com/cslant/telegram-git-notifier-app",
"license": "MIT",
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-json": "*",
"cslant/telegram-git-notifier": "^1.3.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^v3.37.1"
"friendsofphp/php-cs-fixer": "^v3.37.1",
"phpstan/phpstan": "^1.10.39"
},
"autoload": {
"psr-4": {
Expand All @@ -48,6 +49,7 @@
"optimize-autoloader": true
},
"scripts": {
"analyse": "vendor/bin/phpstan",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"post-install-cmd": [
"bash vendor/cslant/telegram-git-notifier/install.sh"
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 8
paths:
- src
tmpDir: build/phpstan
checkMissingIterableValueType: false
55 changes: 0 additions & 55 deletions src/Http/Actions/CallbackAction.php

This file was deleted.

63 changes: 0 additions & 63 deletions src/Http/Actions/CommandAction.php

This file was deleted.

15 changes: 9 additions & 6 deletions src/Http/Actions/IndexAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
use CSlant\TelegramGitNotifier\Notifier;
use CSlant\TelegramGitNotifierApp\Services\CallbackService;
use CSlant\TelegramGitNotifierApp\Services\CommandService;
use CSlant\TelegramGitNotifierApp\Services\NotificationService;
use GuzzleHttp\Client;
use Symfony\Component\HttpFoundation\Request;
use Telegram;
Expand Down Expand Up @@ -43,20 +46,20 @@ public function __construct()
public function __invoke(): void
{
if ($this->bot->isCallback()) {
$callbackAction = new CallbackAction($this->bot);
$callbackAction();
$callbackAction = new CallbackService($this->bot);
$callbackAction->handle();

return;
}

if ($this->bot->isMessage() && $this->bot->isOwner()) {
$commandAction = new CommandAction($this->bot);
$commandAction();
$commandAction = new CommandService($this->bot);
$commandAction->handle();

return;
}

$sendNotificationAction = new SendNotificationAction($this->notifier, $this->bot->setting);
$sendNotificationAction();
$sendNotification = new NotificationService($this->notifier, $this->bot->setting);
$sendNotification->handle();
}
}
39 changes: 39 additions & 0 deletions src/Services/CallbackService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CSlant\TelegramGitNotifier\Bot;
use CSlant\TelegramGitNotifier\Constants\SettingConstant;
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use CSlant\TelegramGitNotifierApp\Traits\Markup;

Expand Down Expand Up @@ -61,4 +62,42 @@ public function answerBackButton(string $callback): void
'reply_markup' => $markup,
]);
}

/**
* @return void
* @throws MessageIsEmptyException
* @throws InvalidViewTemplateException
*/
public function handle(): void
{
$callback = $this->bot->telegram->Callback_Data();

if (str_contains($callback, SettingConstant::SETTING_CUSTOM_EVENTS)) {
$this->bot->eventHandle($callback);

return;
}

if (str_contains($callback, SettingConstant::SETTING_BACK)) {
$this->answerBackButton($callback);

return;
}

$callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback);

$settings = $this->bot->setting->getSettings();
if (array_key_exists($callback, $settings)
&& $this->bot->setting->updateSetting(
$callback,
!$settings[$callback]
)
) {
$this->bot->editMessageReplyMarkup([
'reply_markup' => $this->bot->settingMarkup(),
]);
} else {
$this->bot->answerCallbackQuery('Something went wrong!');
}
}
}
49 changes: 49 additions & 0 deletions src/Services/CommandService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CSlant\TelegramGitNotifier\Bot;
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use CSlant\TelegramGitNotifierApp\Traits\Markup;

class CommandService
Expand Down Expand Up @@ -36,6 +37,13 @@ class CommandService
],
];

private Bot $bot;

public function __construct(Bot $bot)
{
$this->bot = $bot;
}

/**
* @param Bot $bot
*
Expand All @@ -53,4 +61,45 @@ public function sendStartMessage(Bot $bot): void
['caption' => $reply]
);
}

/**
* @return void
* @throws EntryNotFoundException
* @throws MessageIsEmptyException
*/
public function handle(): void
{
$text = $this->bot->telegram->Text();

switch ($text) {
case '/start':
$this->sendStartMessage($this->bot);

break;
case '/menu':
$this->bot->sendMessage(
view('tools.menu'),
['reply_markup' => $this->menuMarkup($this->bot->telegram)]
);

break;
case '/token':
case '/id':
case '/usage':
case '/server':
$this->bot->sendMessage(view('tools.' . trim($text, '/')));

break;
case '/settings':
$this->bot->settingHandle();

break;
case '/set_menu':
$this->bot->setMyCommands(CommandService::MENU_COMMANDS);

break;
default:
$this->bot->sendMessage('🤨 Invalid Request!');
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

namespace CSlant\TelegramGitNotifierApp\Http\Actions;
namespace CSlant\TelegramGitNotifierApp\Services;

use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
use CSlant\TelegramGitNotifier\Models\Setting;
use CSlant\TelegramGitNotifier\Notifier;
use CSlant\TelegramGitNotifier\Objects\Validator;
use Symfony\Component\HttpFoundation\Request;

class SendNotificationAction
class NotificationService
{
protected Request $request;

Expand All @@ -36,8 +37,9 @@ public function __construct(
* @return void
* @throws InvalidViewTemplateException
* @throws SendNotificationException
* @throws MessageIsEmptyException
*/
public function __invoke(): void
public function handle(): void
{
$eventName = $this->notifier->handleEventFromRequest($this->request);
if (!empty($eventName)) {
Expand All @@ -51,6 +53,7 @@ public function __invoke(): void
* @return void
* @throws InvalidViewTemplateException
* @throws SendNotificationException
* @throws MessageIsEmptyException
*/
private function sendNotification(string $event): void
{
Expand Down Expand Up @@ -83,7 +86,7 @@ private function sendNotification(string $event): void
* @param string $event
*
* @return bool
* @throws InvalidViewTemplateException
* @throws InvalidViewTemplateException|MessageIsEmptyException
*/
private function validateAccessEvent(string $event): bool
{
Expand Down

0 comments on commit 4195ea1

Please sign in to comment.