-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from tanhongit/develop
Add phpstan & update send notification handle
- Loading branch information
Showing
9 changed files
with
256 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,11 +22,6 @@ | |
"name": "Tan Nguyen", | ||
"email": "[email protected]", | ||
"homepage": "https://tanhongit.com", | ||
"role": "Lead" | ||
}, | ||
{ | ||
"name": "Xuan Thinh", | ||
"email": "[email protected]", | ||
"role": "Developer" | ||
} | ||
], | ||
|
@@ -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" | ||
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
parameters: | ||
excludePaths: | ||
- src/Http/Actions/WebhookAction.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |