Skip to content

Commit

Permalink
Merge pull request #19 from tanhongit/develop
Browse files Browse the repository at this point in the history
Add phpstan & update send notification handle
  • Loading branch information
tanhongit authored Nov 11, 2023
2 parents 3b9ca5e + 593c3cd commit d0c5ecb
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 17 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: PHPStan

on: [push, pull_request]

jobs:
phpstan:
runs-on: ${{ matrix.os }}
name: PHPStan - P${{ matrix.php }}

strategy:
matrix:
os: [ ubuntu-latest ]
php: [ '8.1', '8.2', '8.3' ]

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/[email protected]
with:
php-version: ${{ matrix.php }}

- 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
21 changes: 12 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
"name": "Tan Nguyen",
"email": "[email protected]",
"homepage": "https://tanhongit.com",
"role": "Lead"
},
{
"name": "Xuan Thinh",
"email": "[email protected]",
"role": "Developer"
}
],
Expand All @@ -42,13 +37,20 @@
},
"require": {
"php": "^8.1",
"cslant/telegram-git-notifier": "^v1.3.2"
"cslant/telegram-git-notifier": "^v1.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^v3.37.1",
"pestphp/pest": "^2.24"
"friendsofphp/php-cs-fixer": "^v3.37",
"nunomaduro/collision": "^7.10",
"nunomaduro/larastan": "^2.6",
"orchestra/testbench": "^8.14",
"pestphp/pest": "^2.24",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3"
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"post-install-cmd": [
"bash vendor/cslant/telegram-git-notifier/install.sh"
Expand All @@ -70,7 +72,8 @@
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
}
},
"minimum-stability": "dev",
Expand Down
2 changes: 2 additions & 0 deletions config/telegram-git-notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@

/** Set the path to the view file */
'view' => [
'namespace' => env('TGN_VIEW_NAMESPACE', 'telegram-git-notifier').'::',

'default' => env(
'TGN_VIEW_DEFAULT',
base_path('resources/views/vendor/telegram-git-notifier')
Expand Down
3 changes: 3 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
excludePaths:
- src/Http/Actions/WebhookAction.php
13 changes: 13 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
includes:
- phpstan-baseline.neon

parameters:
level: 9
paths:
- src
- routes
- config
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false
7 changes: 5 additions & 2 deletions routes/bot.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use CSlant\LaravelTelegramGitNotifier\Http\Actions\IndexAction;
use CSlant\LaravelTelegramGitNotifier\Http\Actions\WebhookAction;
use Illuminate\Support\Facades\Route;

Expand All @@ -15,8 +16,10 @@
*/

Route::prefix('telegram-git-notifier')->group(function () {
Route::any('/', [IndexAction::class, 'index'])->name('telegram-git-notifier.index');

Route::prefix('webhook')->group(function () {
Route::get('/set', [WebhookAction::class, 'set'])->name('webhook.set');
Route::get('/delete', [WebhookAction::class, 'delete'])->name('webhook.delete');
Route::get('/set', [WebhookAction::class, 'set'])->name('telegram-git-notifier.webhook.set');
Route::get('/delete', [WebhookAction::class, 'delete'])->name('telegram-git-notifier.webhook.delete');
});
});
55 changes: 55 additions & 0 deletions src/Http/Actions/IndexAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;

use CSlant\LaravelTelegramGitNotifier\Services\NotificationService;
use CSlant\TelegramGitNotifier\Bot;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
use CSlant\TelegramGitNotifier\Notifier;
use GuzzleHttp\Client;
use Symfony\Component\HttpFoundation\Request;
use Telegram;

class IndexAction
{
protected Client $client;

protected Bot $bot;

protected Notifier $notifier;

protected Request $request;

/**
* @throws ConfigFileException
*/
public function __construct()
{
$this->client = new Client();

$telegram = new Telegram(config('telegram-git-notifier.bot.token'));
$this->bot = new Bot($telegram);
$this->notifier = new Notifier();
}

/**
* Handle telegram git notifier app.
*
* @return void
*
* @throws InvalidViewTemplateException
* @throws MessageIsEmptyException
* @throws SendNotificationException
*/
public function index(): void
{
$sendNotification = new NotificationService(
$this->notifier,
$this->bot->setting
);
$sendNotification->handle();
}
}
31 changes: 25 additions & 6 deletions src/Http/Actions/WebhookAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;

use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
use CSlant\TelegramGitNotifier\Webhook;

class WebhookAction
Expand All @@ -18,30 +19,48 @@ public function __construct()
/**
* Set webhook for telegram bot.
*
* @return false|string
* @return string
*
* @throws WebhookException
*/
public function set(): false|string
public function set(): string
{
return $this->webhook->setWebhook();
}

/**
* Delete webhook for telegram bot.
*
* @return false|string
* @return string
*
* @throws WebhookException
*/
public function delete(): false|string
public function delete(): string
{
return $this->webhook->deleteWebHook();
}

/**
* Get webhook update.
*
* @return false|string
* @return string
*
* @throws WebhookException
*/
public function getUpdates(): false|string
public function getUpdates(): string
{
return $this->webhook->getUpdates();
}

/**
* Get webhook info.
*
* @return string
*
* @throws WebhookException
*/
public function getWebHookInfo(): string
{
return $this->webhook->getWebHookInfo();
}
}
109 changes: 109 additions & 0 deletions src/Services/NotificationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace CSlant\LaravelTelegramGitNotifier\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 NotificationService
{
protected Request $request;

protected array $chatIds = [];

protected Notifier $notifier;

protected Setting $setting;

public function __construct(
Notifier $notifier,
Setting $setting,
) {
$this->request = Request::createFromGlobals();
$this->notifier = $notifier;
$this->chatIds = $this->notifier->parseNotifyChatIds();

$this->setting = $setting;
}

/**
* Handle to send notification from webhook event to telegram.
*
* @return void
*
* @throws InvalidViewTemplateException
* @throws SendNotificationException
* @throws MessageIsEmptyException
*/
public function handle(): void
{
$eventName = $this->notifier->handleEventFromRequest($this->request);
if (!empty($eventName)) {
$this->sendNotification($eventName);
}
}

/**
* @param string $event
* @return void
*
* @throws InvalidViewTemplateException
* @throws SendNotificationException
* @throws MessageIsEmptyException
*/
private function sendNotification(string $event): void
{
if (!$this->validateAccessEvent($event)) {
return;
}

foreach ($this->chatIds as $chatId => $thread) {
if (empty($chatId)) {
continue;
}

if (empty($thread)) {
$this->notifier->sendNotify(null, ['chat_id' => $chatId]);

continue;
}

foreach ($thread as $threadId) {
$this->notifier->sendNotify(null, [
'chat_id' => $chatId, 'message_thread_id' => $threadId,
]);
}
}
}

/**
* Validate access event.
*
* @param string $event
* @return bool
*
* @throws InvalidViewTemplateException|MessageIsEmptyException
*/
private function validateAccessEvent(string $event): bool
{
$payload = $this->notifier->setPayload($this->request, $event);
$validator = new Validator($this->setting, $this->notifier->event);

if (empty($payload) || !is_object($payload)
|| !$validator->isAccessEvent(
$this->notifier->event->platform,
$event,
$payload
)
) {
return false;
}

return true;
}
}

0 comments on commit d0c5ecb

Please sign in to comment.