Skip to content

Commit 36e9bb3

Browse files
tanhongitCopilot
andcommitted
refactor: PHP 8.5 + architecture improvements
- Add Platform enum and ChatTarget DTO - Add in-memory caching for Event and Setting models - Replace error_log() with PSR-3 LoggerInterface in Validator - Fix EventTrait bug (was assigning string to Event object) - Fix all implicit nullable parameter deprecations - Add retry logic with exponential backoff for Telegram API - Replace unsafe extract() in ConfigHelper with closure scope - Use match expressions, readonly properties, #[NoDiscard] - Centralize Telegram API calls in Webhook (eliminate 4x duplication) - Add strict type comparisons throughout - Fix test paths (storage/json/tgn/ → config/jsons/) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent dffab87 commit 36e9bb3

26 files changed

+487
-267
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"php": "^8.4|^8.5",
4343
"eleirbag89/telegrambotphp": "^1.4",
4444
"guzzlehttp/guzzle": "^7.8",
45+
"psr/log": "^3.0",
4546
"symfony/http-foundation": "^7.0",
4647
"vlucas/phpdotenv": "^5.6"
4748
},

config/jsons/github-events.json

100644100755
File mode changed.

config/jsons/gitlab-events.json

100644100755
File mode changed.

config/jsons/tgn-settings.json

100644100755
File mode changed.

config/tg-notifier.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@
4242
/** Set the path to the data file */
4343
'data_file' => [
4444
'setting' => $_ENV['TGN_PATH_SETTING'] ??
45-
'storage/json/tgn/tgn-settings.json',
45+
'config/jsons/tgn-settings.json',
4646

4747
'platform' => [
4848
'gitlab' => $_ENV['TGN_PATH_PLATFORM_GITLAB'] ??
49-
'storage/json/tgn/gitlab-events.json',
49+
'config/jsons/gitlab-events.json',
5050
'github' => $_ENV['TGN_PATH_PLATFORM_GITHUB'] ??
51-
'storage/json/tgn/github-events.json',
51+
'config/jsons/github-events.json',
5252
],
5353
],
5454

src/Bot.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ class Bot implements BotInterface
3939
* @throws ConfigFileException
4040
*/
4141
public function __construct(
42-
Telegram $telegram = null,
42+
?Telegram $telegram = null,
4343
?string $chatBotId = null,
44-
Event $event = null,
44+
?Event $event = null,
4545
?string $platform = EventConstant::DEFAULT_PLATFORM,
4646
?string $platformFile = null,
47-
Setting $setting = null,
47+
?Setting $setting = null,
4848
?string $settingFile = null,
4949
) {
5050
$this->event = $event ?? new Event();

src/Constants/EventConstant.php

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

33
namespace CSlant\TelegramGitNotifier\Constants;
44

5+
use CSlant\TelegramGitNotifier\Enums\Platform;
6+
57
final class EventConstant
68
{
79
public const DEFAULT_PLATFORM = 'github';
@@ -25,4 +27,9 @@ final class EventConstant
2527
'github' => self::GITHUB_EVENT_SEPARATOR,
2628
'gitlab' => self::GITLAB_EVENT_SEPARATOR,
2729
];
30+
31+
public static function platformEnum(string $platform): Platform
32+
{
33+
return Platform::from($platform);
34+
}
2835
}

src/DTOs/ChatTarget.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace CSlant\TelegramGitNotifier\DTOs;
4+
5+
readonly class ChatTarget
6+
{
7+
/**
8+
* @param string $chatId
9+
* @param list<string> $threadIds
10+
*/
11+
public function __construct(
12+
public string $chatId,
13+
public array $threadIds = [],
14+
) {}
15+
16+
public function hasThreads(): bool
17+
{
18+
return $this->threadIds !== [];
19+
}
20+
21+
/**
22+
* Parse a single "chatId:thread1,thread2" string.
23+
*/
24+
public static function fromString(string $raw): self
25+
{
26+
[$chatId, $threadIds] = explode(':', $raw) + [null, null];
27+
28+
return new self(
29+
chatId: (string) $chatId,
30+
threadIds: $threadIds ? explode(',', $threadIds) : [],
31+
);
32+
}
33+
34+
/**
35+
* Parse "id1;id2:t1,t2;id3" format into an array of ChatTarget.
36+
*
37+
* @return list<self>
38+
*/
39+
public static function parseMultiple(string $raw): array
40+
{
41+
if (trim($raw) === '') {
42+
return [];
43+
}
44+
45+
return array_map(
46+
static fn (string $pair) => self::fromString($pair),
47+
explode(';', $raw),
48+
);
49+
}
50+
}

src/Enums/Platform.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace CSlant\TelegramGitNotifier\Enums;
4+
5+
enum Platform: string
6+
{
7+
case GitHub = 'github';
8+
case GitLab = 'gitlab';
9+
10+
public function eventSeparator(): string
11+
{
12+
return match ($this) {
13+
self::GitHub => 'gh.',
14+
self::GitLab => 'gl.',
15+
};
16+
}
17+
18+
public function webhookEventHeader(): string
19+
{
20+
return match ($this) {
21+
self::GitHub => 'HTTP_X_GITHUB_EVENT',
22+
self::GitLab => 'HTTP_X_GITLAB_EVENT',
23+
};
24+
}
25+
26+
/**
27+
* @return array<string, string>
28+
*/
29+
public static function webhookEventHeaders(): array
30+
{
31+
return array_combine(
32+
array_map(fn (self $p) => $p->value, self::cases()),
33+
array_map(fn (self $p) => $p->webhookEventHeader(), self::cases()),
34+
);
35+
}
36+
37+
/**
38+
* @return array<string, string>
39+
*/
40+
public static function eventSeparators(): array
41+
{
42+
return array_combine(
43+
array_map(fn (self $p) => $p->value, self::cases()),
44+
array_map(fn (self $p) => $p->eventSeparator(), self::cases()),
45+
);
46+
}
47+
48+
public static function default(): self
49+
{
50+
return self::GitHub;
51+
}
52+
}

src/Exceptions/InvalidViewTemplateException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class InvalidViewTemplateException extends TelegramGitNotifierException
88
{
99
public static function create(
1010
string $view,
11-
Throwable $previous = null
11+
?Throwable $previous = null
1212
): self {
1313
return new self(
1414
"Invalid view template: {$view}",

0 commit comments

Comments
 (0)