Skip to content

Commit d0c5ecb

Browse files
authored
Merge pull request #19 from tanhongit/develop
Add phpstan & update send notification handle
2 parents 3b9ca5e + 593c3cd commit d0c5ecb

File tree

9 files changed

+256
-17
lines changed

9 files changed

+256
-17
lines changed

.github/workflows/phpstan.yml

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

composer.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
"name": "Tan Nguyen",
2323
"email": "tannp27@gmail.com",
2424
"homepage": "https://tanhongit.com",
25-
"role": "Lead"
26-
},
27-
{
28-
"name": "Xuan Thinh",
29-
"email": "pxthinh.vn@gmail.com",
3025
"role": "Developer"
3126
}
3227
],
@@ -42,13 +37,20 @@
4237
},
4338
"require": {
4439
"php": "^8.1",
45-
"cslant/telegram-git-notifier": "^v1.3.2"
40+
"cslant/telegram-git-notifier": "^v1.3"
4641
},
4742
"require-dev": {
48-
"friendsofphp/php-cs-fixer": "^v3.37.1",
49-
"pestphp/pest": "^2.24"
43+
"friendsofphp/php-cs-fixer": "^v3.37",
44+
"nunomaduro/collision": "^7.10",
45+
"nunomaduro/larastan": "^2.6",
46+
"orchestra/testbench": "^8.14",
47+
"pestphp/pest": "^2.24",
48+
"phpstan/extension-installer": "^1.3",
49+
"phpstan/phpstan-deprecation-rules": "^1.1",
50+
"phpstan/phpstan-phpunit": "^1.3"
5051
},
5152
"scripts": {
53+
"analyse": "vendor/bin/phpstan analyse",
5254
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
5355
"post-install-cmd": [
5456
"bash vendor/cslant/telegram-git-notifier/install.sh"
@@ -70,7 +72,8 @@
7072
"config": {
7173
"sort-packages": true,
7274
"allow-plugins": {
73-
"pestphp/pest-plugin": true
75+
"pestphp/pest-plugin": true,
76+
"phpstan/extension-installer": true
7477
}
7578
},
7679
"minimum-stability": "dev",

config/telegram-git-notifier.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969

7070
/** Set the path to the view file */
7171
'view' => [
72+
'namespace' => env('TGN_VIEW_NAMESPACE', 'telegram-git-notifier').'::',
73+
7274
'default' => env(
7375
'TGN_VIEW_DEFAULT',
7476
base_path('resources/views/vendor/telegram-git-notifier')

phpstan-baseline.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
excludePaths:
3+
- src/Http/Actions/WebhookAction.php

phpstan.neon.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
4+
parameters:
5+
level: 9
6+
paths:
7+
- src
8+
- routes
9+
- config
10+
tmpDir: build/phpstan
11+
checkOctaneCompatibility: true
12+
checkModelProperties: true
13+
checkMissingIterableValueType: false

routes/bot.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use CSlant\LaravelTelegramGitNotifier\Http\Actions\IndexAction;
34
use CSlant\LaravelTelegramGitNotifier\Http\Actions\WebhookAction;
45
use Illuminate\Support\Facades\Route;
56

@@ -15,8 +16,10 @@
1516
*/
1617

1718
Route::prefix('telegram-git-notifier')->group(function () {
19+
Route::any('/', [IndexAction::class, 'index'])->name('telegram-git-notifier.index');
20+
1821
Route::prefix('webhook')->group(function () {
19-
Route::get('/set', [WebhookAction::class, 'set'])->name('webhook.set');
20-
Route::get('/delete', [WebhookAction::class, 'delete'])->name('webhook.delete');
22+
Route::get('/set', [WebhookAction::class, 'set'])->name('telegram-git-notifier.webhook.set');
23+
Route::get('/delete', [WebhookAction::class, 'delete'])->name('telegram-git-notifier.webhook.delete');
2124
});
2225
});

src/Http/Actions/IndexAction.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;
4+
5+
use CSlant\LaravelTelegramGitNotifier\Services\NotificationService;
6+
use CSlant\TelegramGitNotifier\Bot;
7+
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
8+
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
9+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
10+
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
11+
use CSlant\TelegramGitNotifier\Notifier;
12+
use GuzzleHttp\Client;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Telegram;
15+
16+
class IndexAction
17+
{
18+
protected Client $client;
19+
20+
protected Bot $bot;
21+
22+
protected Notifier $notifier;
23+
24+
protected Request $request;
25+
26+
/**
27+
* @throws ConfigFileException
28+
*/
29+
public function __construct()
30+
{
31+
$this->client = new Client();
32+
33+
$telegram = new Telegram(config('telegram-git-notifier.bot.token'));
34+
$this->bot = new Bot($telegram);
35+
$this->notifier = new Notifier();
36+
}
37+
38+
/**
39+
* Handle telegram git notifier app.
40+
*
41+
* @return void
42+
*
43+
* @throws InvalidViewTemplateException
44+
* @throws MessageIsEmptyException
45+
* @throws SendNotificationException
46+
*/
47+
public function index(): void
48+
{
49+
$sendNotification = new NotificationService(
50+
$this->notifier,
51+
$this->bot->setting
52+
);
53+
$sendNotification->handle();
54+
}
55+
}

src/Http/Actions/WebhookAction.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;
44

5+
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
56
use CSlant\TelegramGitNotifier\Webhook;
67

78
class WebhookAction
@@ -18,30 +19,48 @@ public function __construct()
1819
/**
1920
* Set webhook for telegram bot.
2021
*
21-
* @return false|string
22+
* @return string
23+
*
24+
* @throws WebhookException
2225
*/
23-
public function set(): false|string
26+
public function set(): string
2427
{
2528
return $this->webhook->setWebhook();
2629
}
2730

2831
/**
2932
* Delete webhook for telegram bot.
3033
*
31-
* @return false|string
34+
* @return string
35+
*
36+
* @throws WebhookException
3237
*/
33-
public function delete(): false|string
38+
public function delete(): string
3439
{
3540
return $this->webhook->deleteWebHook();
3641
}
3742

3843
/**
3944
* Get webhook update.
4045
*
41-
* @return false|string
46+
* @return string
47+
*
48+
* @throws WebhookException
4249
*/
43-
public function getUpdates(): false|string
50+
public function getUpdates(): string
4451
{
4552
return $this->webhook->getUpdates();
4653
}
54+
55+
/**
56+
* Get webhook info.
57+
*
58+
* @return string
59+
*
60+
* @throws WebhookException
61+
*/
62+
public function getWebHookInfo(): string
63+
{
64+
return $this->webhook->getWebHookInfo();
65+
}
4766
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Services;
4+
5+
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
6+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
7+
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
8+
use CSlant\TelegramGitNotifier\Models\Setting;
9+
use CSlant\TelegramGitNotifier\Notifier;
10+
use CSlant\TelegramGitNotifier\Objects\Validator;
11+
use Symfony\Component\HttpFoundation\Request;
12+
13+
class NotificationService
14+
{
15+
protected Request $request;
16+
17+
protected array $chatIds = [];
18+
19+
protected Notifier $notifier;
20+
21+
protected Setting $setting;
22+
23+
public function __construct(
24+
Notifier $notifier,
25+
Setting $setting,
26+
) {
27+
$this->request = Request::createFromGlobals();
28+
$this->notifier = $notifier;
29+
$this->chatIds = $this->notifier->parseNotifyChatIds();
30+
31+
$this->setting = $setting;
32+
}
33+
34+
/**
35+
* Handle to send notification from webhook event to telegram.
36+
*
37+
* @return void
38+
*
39+
* @throws InvalidViewTemplateException
40+
* @throws SendNotificationException
41+
* @throws MessageIsEmptyException
42+
*/
43+
public function handle(): void
44+
{
45+
$eventName = $this->notifier->handleEventFromRequest($this->request);
46+
if (!empty($eventName)) {
47+
$this->sendNotification($eventName);
48+
}
49+
}
50+
51+
/**
52+
* @param string $event
53+
* @return void
54+
*
55+
* @throws InvalidViewTemplateException
56+
* @throws SendNotificationException
57+
* @throws MessageIsEmptyException
58+
*/
59+
private function sendNotification(string $event): void
60+
{
61+
if (!$this->validateAccessEvent($event)) {
62+
return;
63+
}
64+
65+
foreach ($this->chatIds as $chatId => $thread) {
66+
if (empty($chatId)) {
67+
continue;
68+
}
69+
70+
if (empty($thread)) {
71+
$this->notifier->sendNotify(null, ['chat_id' => $chatId]);
72+
73+
continue;
74+
}
75+
76+
foreach ($thread as $threadId) {
77+
$this->notifier->sendNotify(null, [
78+
'chat_id' => $chatId, 'message_thread_id' => $threadId,
79+
]);
80+
}
81+
}
82+
}
83+
84+
/**
85+
* Validate access event.
86+
*
87+
* @param string $event
88+
* @return bool
89+
*
90+
* @throws InvalidViewTemplateException|MessageIsEmptyException
91+
*/
92+
private function validateAccessEvent(string $event): bool
93+
{
94+
$payload = $this->notifier->setPayload($this->request, $event);
95+
$validator = new Validator($this->setting, $this->notifier->event);
96+
97+
if (empty($payload) || !is_object($payload)
98+
|| !$validator->isAccessEvent(
99+
$this->notifier->event->platform,
100+
$event,
101+
$payload
102+
)
103+
) {
104+
return false;
105+
}
106+
107+
return true;
108+
}
109+
}

0 commit comments

Comments
 (0)