Skip to content

Commit 4195ea1

Browse files
authored
Merge pull request #103 from tanhongit/main
Refactor and workflows
2 parents 0aec3a2 + 0a2784b commit 4195ea1

9 files changed

Lines changed: 140 additions & 130 deletions

File tree

.github/workflows/phpstan.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: PHPStan
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
phpstan:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
12+
- name: Setup PHP
13+
uses: shivammathur/setup-php@2.26.0
14+
with:
15+
php-version: '8.1'
16+
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Install dependencies
21+
run: |
22+
composer install --no-interaction --no-progress --no-suggest
23+
24+
- name: Run PHPStan
25+
run: |
26+
composer analyse --error-format=github

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
"homepage": "https://github.com/cslant/telegram-git-notifier-app",
2121
"license": "MIT",
2222
"require": {
23-
"php": "^8.0",
23+
"php": "^8.1",
2424
"ext-json": "*",
2525
"cslant/telegram-git-notifier": "^1.3.1"
2626
},
2727
"require-dev": {
28-
"friendsofphp/php-cs-fixer": "^v3.37.1"
28+
"friendsofphp/php-cs-fixer": "^v3.37.1",
29+
"phpstan/phpstan": "^1.10.39"
2930
},
3031
"autoload": {
3132
"psr-4": {
@@ -48,6 +49,7 @@
4849
"optimize-autoloader": true
4950
},
5051
"scripts": {
52+
"analyse": "vendor/bin/phpstan",
5153
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
5254
"post-install-cmd": [
5355
"bash vendor/cslant/telegram-git-notifier/install.sh"

phpstan.neon.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
level: 8
3+
paths:
4+
- src
5+
tmpDir: build/phpstan
6+
checkMissingIterableValueType: false

src/Http/Actions/CallbackAction.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Http/Actions/CommandAction.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/Http/Actions/IndexAction.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
99
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
1010
use CSlant\TelegramGitNotifier\Notifier;
11+
use CSlant\TelegramGitNotifierApp\Services\CallbackService;
12+
use CSlant\TelegramGitNotifierApp\Services\CommandService;
13+
use CSlant\TelegramGitNotifierApp\Services\NotificationService;
1114
use GuzzleHttp\Client;
1215
use Symfony\Component\HttpFoundation\Request;
1316
use Telegram;
@@ -43,20 +46,20 @@ public function __construct()
4346
public function __invoke(): void
4447
{
4548
if ($this->bot->isCallback()) {
46-
$callbackAction = new CallbackAction($this->bot);
47-
$callbackAction();
49+
$callbackAction = new CallbackService($this->bot);
50+
$callbackAction->handle();
4851

4952
return;
5053
}
5154

5255
if ($this->bot->isMessage() && $this->bot->isOwner()) {
53-
$commandAction = new CommandAction($this->bot);
54-
$commandAction();
56+
$commandAction = new CommandService($this->bot);
57+
$commandAction->handle();
5558

5659
return;
5760
}
5861

59-
$sendNotificationAction = new SendNotificationAction($this->notifier, $this->bot->setting);
60-
$sendNotificationAction();
62+
$sendNotification = new NotificationService($this->notifier, $this->bot->setting);
63+
$sendNotification->handle();
6164
}
6265
}

src/Services/CallbackService.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CSlant\TelegramGitNotifier\Bot;
66
use CSlant\TelegramGitNotifier\Constants\SettingConstant;
7+
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
78
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
89
use CSlant\TelegramGitNotifierApp\Traits\Markup;
910

@@ -61,4 +62,42 @@ public function answerBackButton(string $callback): void
6162
'reply_markup' => $markup,
6263
]);
6364
}
65+
66+
/**
67+
* @return void
68+
* @throws MessageIsEmptyException
69+
* @throws InvalidViewTemplateException
70+
*/
71+
public function handle(): void
72+
{
73+
$callback = $this->bot->telegram->Callback_Data();
74+
75+
if (str_contains($callback, SettingConstant::SETTING_CUSTOM_EVENTS)) {
76+
$this->bot->eventHandle($callback);
77+
78+
return;
79+
}
80+
81+
if (str_contains($callback, SettingConstant::SETTING_BACK)) {
82+
$this->answerBackButton($callback);
83+
84+
return;
85+
}
86+
87+
$callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback);
88+
89+
$settings = $this->bot->setting->getSettings();
90+
if (array_key_exists($callback, $settings)
91+
&& $this->bot->setting->updateSetting(
92+
$callback,
93+
!$settings[$callback]
94+
)
95+
) {
96+
$this->bot->editMessageReplyMarkup([
97+
'reply_markup' => $this->bot->settingMarkup(),
98+
]);
99+
} else {
100+
$this->bot->answerCallbackQuery('Something went wrong!');
101+
}
102+
}
64103
}

src/Services/CommandService.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CSlant\TelegramGitNotifier\Bot;
66
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
7+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
78
use CSlant\TelegramGitNotifierApp\Traits\Markup;
89

910
class CommandService
@@ -36,6 +37,13 @@ class CommandService
3637
],
3738
];
3839

40+
private Bot $bot;
41+
42+
public function __construct(Bot $bot)
43+
{
44+
$this->bot = $bot;
45+
}
46+
3947
/**
4048
* @param Bot $bot
4149
*
@@ -53,4 +61,45 @@ public function sendStartMessage(Bot $bot): void
5361
['caption' => $reply]
5462
);
5563
}
64+
65+
/**
66+
* @return void
67+
* @throws EntryNotFoundException
68+
* @throws MessageIsEmptyException
69+
*/
70+
public function handle(): void
71+
{
72+
$text = $this->bot->telegram->Text();
73+
74+
switch ($text) {
75+
case '/start':
76+
$this->sendStartMessage($this->bot);
77+
78+
break;
79+
case '/menu':
80+
$this->bot->sendMessage(
81+
view('tools.menu'),
82+
['reply_markup' => $this->menuMarkup($this->bot->telegram)]
83+
);
84+
85+
break;
86+
case '/token':
87+
case '/id':
88+
case '/usage':
89+
case '/server':
90+
$this->bot->sendMessage(view('tools.' . trim($text, '/')));
91+
92+
break;
93+
case '/settings':
94+
$this->bot->settingHandle();
95+
96+
break;
97+
case '/set_menu':
98+
$this->bot->setMyCommands(CommandService::MENU_COMMANDS);
99+
100+
break;
101+
default:
102+
$this->bot->sendMessage('🤨 Invalid Request!');
103+
}
104+
}
56105
}

src/Http/Actions/SendNotificationAction.php renamed to src/Services/NotificationService.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?php
22

3-
namespace CSlant\TelegramGitNotifierApp\Http\Actions;
3+
namespace CSlant\TelegramGitNotifierApp\Services;
44

55
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
6+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
67
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
78
use CSlant\TelegramGitNotifier\Models\Setting;
89
use CSlant\TelegramGitNotifier\Notifier;
910
use CSlant\TelegramGitNotifier\Objects\Validator;
1011
use Symfony\Component\HttpFoundation\Request;
1112

12-
class SendNotificationAction
13+
class NotificationService
1314
{
1415
protected Request $request;
1516

@@ -36,8 +37,9 @@ public function __construct(
3637
* @return void
3738
* @throws InvalidViewTemplateException
3839
* @throws SendNotificationException
40+
* @throws MessageIsEmptyException
3941
*/
40-
public function __invoke(): void
42+
public function handle(): void
4143
{
4244
$eventName = $this->notifier->handleEventFromRequest($this->request);
4345
if (!empty($eventName)) {
@@ -51,6 +53,7 @@ public function __invoke(): void
5153
* @return void
5254
* @throws InvalidViewTemplateException
5355
* @throws SendNotificationException
56+
* @throws MessageIsEmptyException
5457
*/
5558
private function sendNotification(string $event): void
5659
{
@@ -83,7 +86,7 @@ private function sendNotification(string $event): void
8386
* @param string $event
8487
*
8588
* @return bool
86-
* @throws InvalidViewTemplateException
89+
* @throws InvalidViewTemplateException|MessageIsEmptyException
8790
*/
8891
private function validateAccessEvent(string $event): bool
8992
{

0 commit comments

Comments
 (0)