Skip to content

Commit

Permalink
Merge pull request cslant#21 from tanhongit/main
Browse files Browse the repository at this point in the history
phpstan test workflow
  • Loading branch information
tanhongit authored Oct 30, 2023
2 parents ca60e6f + 745015e commit 84fb30a
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 53 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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: Install composer dependencies
uses: ramsey/composer-install@v2

- name: Run PHPStan
run: vendor/bin/phpstan --error-format=github
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ npm-debug.log
/log

package-lock.json
yarn.lock
yarn.lock

build
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Telegram Git Notifier

This package provides the ability to integrate the Telegram messaging service and GitHub/GitLab.
This package provides the ability to integrate the Telegram messaging service and GitHub and GitLab.
With this package,
you can create a Telegram bot to receive notifications from GitHub or GitLab events
and manage customization through messages and buttons on Telegram.
Expand All @@ -18,7 +18,7 @@ and manage customization through messages and buttons on Telegram.

## 📋 Requirements

- PHP ^8.0
- PHP ^8.1
- Composer
- Telegram Bot

Expand Down
7 changes: 4 additions & 3 deletions common/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ function config(string $string): mixed
* @param string $partialPath
* @param array $data
*
* @return bool|string
* @return null|string
*/
function view(string $partialPath, array $data = []): bool|string
function view(string $partialPath, array $data = []): null|string
{
return (new ConfigHelper())->getTemplateData($partialPath, $data);
$content = (new ConfigHelper())->getTemplateData($partialPath, $data);
return $content ?: null;
}
}
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@
]
},
"require": {
"php": "^8.0",
"php": "^8.1",
"eleirbag89/telegrambotphp": "^1.4",
"guzzlehttp/guzzle": "^7.8",
"symfony/http-foundation": "^6.3",
"vlucas/phpdotenv": "^5.5"
},
"require-dev": {
"phpstan/phpstan": "^1.10.39"
},
"scripts": {
"analyse": "vendor/bin/phpstan"
},
"support": {
"issues": "https://github.com/lbiltech/telegram-git-notifier/issues"
},
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
4 changes: 2 additions & 2 deletions src/Exceptions/EntryNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public static function fileNotFound(): self
return new self('File not found');
}

public static function configNotFound($config): self
public static function configNotFound(string $config): self
{
return new self("Config {$config} not found");
}

public static function viewNotFound($view): self
public static function viewNotFound(string $view): self
{
return new self("View {$view} not found");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/TelegramGitNotifierException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class TelegramGitNotifierException extends Exception
{
public static function isEmpty(): self
{
return new static('Telegram Git Notifier is empty');
return new self('Telegram Git Notifier is empty');
}

public static function invalid(): self
{
return new static('Telegram Git Notifier is invalid');
return new self('Telegram Git Notifier is invalid');
}
}
8 changes: 6 additions & 2 deletions src/Helpers/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class ConfigHelper
{
/**
* @var array<string, mixed>
*/
public array $config;

public function __construct()
Expand Down Expand Up @@ -39,12 +42,12 @@ public function execConfig(string $string): mixed
/**
* Return template data
*
* @param $partialPath
* @param string $partialPath
* @param array $data
*
* @return bool|string
*/
public function getTemplateData($partialPath, array $data = []): bool|string
public function getTemplateData(string $partialPath, array $data = []): bool|string
{
$viewPathFile = $this->execConfig('telegram-git-notifier.view.path') . '/'
. str_replace('.', '/', $partialPath) . '.php';
Expand All @@ -53,6 +56,7 @@ public function getTemplateData($partialPath, array $data = []): bool|string
return '';
}

$content = '';
ob_start();
try {
extract($data, EXTR_SKIP);
Expand Down
9 changes: 5 additions & 4 deletions src/Interfaces/EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LbilTech\TelegramGitNotifier\Interfaces;

use LbilTech\TelegramGitNotifier\Constants\EventConstant;
use LbilTech\TelegramGitNotifier\Trait\ActionEventTrait;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -10,23 +11,23 @@ interface EventInterface
/**
* Get action name of event from payload data
*
* @param $payload
* @param object $payload
*
* @return string
* @see ActionEventTrait::getActionOfEvent()
*/
public function getActionOfEvent($payload): string;
public function getActionOfEvent(object $payload): string;

/**
* Set platform and platform file for event
*
* @param string $platform
* @param string|null $platform
* @param string|null $platformFile
*
* @return void
* @see EventTrait::setPlatFormForEvent()
*/
public function setPlatFormForEvent(string $platform, string $platformFile = null): void;
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void;

/**
* Set event config and get event name
Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/SettingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public function getCallbackData(string $event, string $platform, array|bool $val
* Get event name for markup
*
* @param string $event
* @param $value
* @param bool|array $value
*
* @return string
* @see EventSettingTrait::getEventName()
*/
public function getEventName(string $event, $value): string;
public function getEventName(string $event, bool|array $value): string;

/**
* Get end keyboard buttons
Expand Down
12 changes: 3 additions & 9 deletions src/Interfaces/Structures/AppInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ interface AppInterface
/**
* Send a message to telegram
*
* @param string $message
* @param string|null $message
* @param array $options
*
* @return void
* @throws MessageIsEmptyException
* @see App::sendMessage()
*/
public function sendMessage(
string $message = '',
array $options = []
): void;
public function sendMessage(?string $message = '', array $options = []): void;

/**
* Send a photo to telegram
Expand Down Expand Up @@ -54,10 +51,7 @@ public function answerCallbackQuery(string $text = null): void;
* @return void
* @see App::editMessageText()
*/
public function editMessageText(
?string $text = null,
array $options = []
): void;
public function editMessageText(string $text = null, array $options = []): void;

/**
* Edit message reply markup from a telegram
Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/Structures/NotificationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public function sendNotify(string $message = null, array $options = []): bool;
/**
* Get action name of event from payload data
*
* @param $payload
* @param object $payload
*
* @return string
* @see EventTrait::getActionOfEvent()
*/
public function getActionOfEvent($payload): string;
public function getActionOfEvent(object $payload): string;

/**
* Convert chat and thread ids to array
Expand Down
11 changes: 7 additions & 4 deletions src/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ public function getEventConfig(): array
/**
* Set event config
*
* @param string $platform
* @param string|null $platform
*
* @return void
*/
public function setEventConfig(
string $platform = EventConstant::DEFAULT_PLATFORM
string $platform = null
): void {
$this->platform = $platform;
$this->platform = $platform ?? EventConstant::DEFAULT_PLATFORM;

$json = file_get_contents($this->platformFile);
$this->eventConfig = json_decode($json, true);

if (!empty($json)) {
$this->eventConfig = json_decode($json, true);
}
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public function getSettingFile(): string
public function setSettingConfig(): void
{
$json = file_get_contents($this->settingFile);
$this->settings = json_decode($json, true);

if (!empty($json)) {
$this->settings = json_decode($json, true);
}
}

/**
Expand Down Expand Up @@ -81,13 +84,13 @@ public function isNotified(): bool
* Update setting item value and save to file
*
* @param string $settingName
* @param $settingValue
* @param array|string|bool|int|null $settingValue
*
* @return bool
*/
public function updateSetting(
string $settingName,
$settingValue = null
mixed $settingValue = null
): bool {
$settingKeys = explode('.', $settingName);
$lastKey = array_pop($settingKeys);
Expand Down
4 changes: 2 additions & 2 deletions src/Objects/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public function __construct(Setting $setting, Event $event)
*
* @param string $platform Source code platform (GitHub, GitLab)
* @param string $event Event name (push, pull_request)
* @param $payload
* @param object $payload
*
* @return bool
*/
public function isAccessEvent(
string $platform,
string $event,
$payload
object $payload
): bool {
if (!$this->setting->isNotified()) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Structures/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private function createTelegramBaseContent(): array
];
}

public function sendMessage(string $message = '', array $options = []): void
public function sendMessage(?string $message = '', array $options = []): void
{
if (empty($message)) {
throw MessageIsEmptyException::create();
Expand Down
23 changes: 19 additions & 4 deletions src/Structures/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use GuzzleHttp\Exception\GuzzleException;
use LbilTech\TelegramGitNotifier\Constants\EventConstant;
use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use LbilTech\TelegramGitNotifier\Exceptions\SendNotificationException;
use Symfony\Component\HttpFoundation\Request;

trait Notification
{
public mixed $payload;
public object $payload;

public string $message = '';

Expand All @@ -30,11 +31,18 @@ public function accessDenied(

public function setPayload(Request $request, string $event)
{
$content = null;

if ($this->event->platform === 'gitlab') {
$this->payload = json_decode($request->getContent());
$content = $request->getContent();
} elseif ($this->event->platform === EventConstant::DEFAULT_PLATFORM) {
$this->payload = json_decode($request->request->get('payload'));
$content = $request->request->get('payload');
}

if (is_string($content)) {
$this->payload = json_decode($content);
}

$this->setMessage($event);

return $this->payload;
Expand All @@ -46,6 +54,7 @@ public function setPayload(Request $request, string $event)
* @param string $typeEvent
*
* @return void
* @throws MessageIsEmptyException
*/
private function setMessage(string $typeEvent): void
{
Expand All @@ -56,10 +65,16 @@ private function setMessage(string $typeEvent): void
? "events.{$this->event->platform}.{$event}.default"
: "events.{$this->event->platform}.{$event}.{$action}";

$this->message = view($viewTemplate, [
$viewResult = view($viewTemplate, [
'payload' => $this->payload,
'event' => tgn_convert_event_name($typeEvent),
]);

if ($viewResult === null) {
throw MessageIsEmptyException::create();
}

$this->message = $viewResult;
}

public function sendNotify(string $message = null, array $options = []): bool
Expand Down
Loading

0 comments on commit 84fb30a

Please sign in to comment.