1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace CSlant\TelegramGitNotifier; |
6
|
|
|
|
7
|
|
|
use CSlant\TelegramGitNotifier\Constants\EventConstant; |
8
|
|
|
use CSlant\TelegramGitNotifier\Constants\NotificationConstant; |
9
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException; |
10
|
|
|
use CSlant\TelegramGitNotifier\Interfaces\EventInterface; |
11
|
|
|
use CSlant\TelegramGitNotifier\Interfaces\Structures\NotificationInterface; |
12
|
|
|
use CSlant\TelegramGitNotifier\Models\Event; |
13
|
|
|
use CSlant\TelegramGitNotifier\Structures\App; |
14
|
|
|
use CSlant\TelegramGitNotifier\Structures\Notification; |
15
|
|
|
use CSlant\TelegramGitNotifier\Trait\EventTrait; |
16
|
|
|
use GuzzleHttp\Client; |
17
|
|
|
use Telegram; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Notifier |
21
|
|
|
* |
22
|
|
|
* Handles the core notification functionality for Telegram Git events. |
23
|
|
|
* Implements both NotificationInterface and EventInterface for handling |
24
|
|
|
* notifications and events respectively. |
25
|
|
|
*/ |
26
|
|
|
class Notifier implements NotificationInterface, EventInterface |
27
|
|
|
{ |
28
|
|
|
use App; |
29
|
|
|
use Notification; |
|
|
|
|
30
|
|
|
use EventTrait; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** @var Event The event instance */ |
33
|
|
|
public Event $event; |
34
|
|
|
|
35
|
|
|
/** @var Client The HTTP client for making requests */ |
36
|
|
|
public Client $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initialize the Notifier with required dependencies |
40
|
|
|
* |
41
|
|
|
* @param Telegram|null $telegram The Telegram bot instance |
42
|
|
|
* @param string|null $chatBotId The chat bot ID |
43
|
|
|
* @param Event|null $event The event instance |
44
|
|
|
* @param string|null $platform The platform (e.g., 'github', 'gitlab') |
45
|
|
|
* @param string|null $platformFile Path to the platform configuration file |
46
|
|
|
* @param Client|null $client The HTTP client |
47
|
|
|
* |
48
|
|
|
* @throws ConfigFileException If platform configuration is invalid |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
?Telegram $telegram = null, |
52
|
|
|
?string $chatBotId = null, |
53
|
|
|
?Event $event = null, |
54
|
|
|
?string $platform = EventConstant::DEFAULT_PLATFORM, |
55
|
|
|
?string $platformFile = null, |
56
|
|
|
?Client $client = null |
57
|
|
|
) { |
58
|
|
|
$this->initializeEvent($event, $platform, $platformFile); |
|
|
|
|
59
|
|
|
$this->initializeTelegram($telegram, $chatBotId); |
60
|
|
|
$this->client = $client ?? new Client(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Parse notification chat IDs from configuration |
65
|
|
|
* |
66
|
|
|
* Expected format: "chatId1:thread1,thread2|chatId2:thread3,thread4" |
67
|
|
|
* |
68
|
|
|
* @param string|null $chatIds The chat IDs string to parse, falls back to config if null |
69
|
|
|
* @return array<string, array<int, string>> Mapped chat IDs to their thread IDs |
70
|
|
|
*/ |
71
|
|
|
public function parseNotifyChatIds(?string $chatIds = null): array |
72
|
|
|
{ |
73
|
|
|
$chatData = $this->getChatData($chatIds); |
74
|
|
|
return $this->mapChatThreads($chatData); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Initialize the event handler |
79
|
|
|
*/ |
80
|
|
|
private function initializeEvent(?Event $event, string $platform, ?string $platformFile): void |
81
|
|
|
{ |
82
|
|
|
$this->event = $event ?? new Event(); |
83
|
|
|
$this->setPlatFormForEvent($platform, $platformFile); |
84
|
|
|
$this->validatePlatformFile(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Initialize the Telegram client |
89
|
|
|
*/ |
90
|
|
|
private function initializeTelegram(?Telegram $telegram, ?string $chatBotId): void |
91
|
|
|
{ |
92
|
|
|
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); |
93
|
|
|
$this->setCurrentChatBotId($chatBotId); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get chat data from config or provided string |
98
|
|
|
*/ |
99
|
|
|
private function getChatData(?string $chatIds): array |
100
|
|
|
{ |
101
|
|
|
$chatConfig = $chatIds ?? config('telegram-git-notifier.bot.notify_chat_ids'); |
102
|
|
|
return explode(NotificationConstant::CHAT_ID_PAIRS_SEPARATOR, (string) $chatConfig); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Map chat IDs to their respective thread IDs |
107
|
|
|
* |
108
|
|
|
* @param array<string> $chatData Array of chat ID and thread ID pairs |
109
|
|
|
* @return array<string, array<int, string>> Mapped chat threads |
110
|
|
|
*/ |
111
|
|
|
private function mapChatThreads(array $chatData): array |
112
|
|
|
{ |
113
|
|
|
$chatThreadMapping = []; |
114
|
|
|
|
115
|
|
|
foreach ($chatData as $data) { |
116
|
|
|
if (empty(trim($data))) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
[$chatId, $threadIds] = array_merge( |
121
|
|
|
explode(NotificationConstant::CHAT_THREAD_ID_SEPARATOR, $data, 2), |
122
|
|
|
[null, null] |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$chatThreadMapping[$chatId] = $threadIds |
126
|
|
|
? explode(NotificationConstant::THREAD_ID_SEPARATOR, $threadIds) |
127
|
|
|
: []; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $chatThreadMapping; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|