|
2 | 2 |
|
3 | 3 | namespace CSlant\LaravelTelegramGitNotifier\Services; |
4 | 4 |
|
5 | | -use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException; |
6 | | -use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; |
7 | | -use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException; |
| 5 | +use CSlant\TelegramGitNotifier\Exceptions\{ |
| 6 | + InvalidViewTemplateException, |
| 7 | + MessageIsEmptyException, |
| 8 | + SendNotificationException |
| 9 | +}; |
8 | 10 | use CSlant\TelegramGitNotifier\Models\Setting; |
9 | 11 | use CSlant\TelegramGitNotifier\Notifier; |
10 | 12 | use CSlant\TelegramGitNotifier\Objects\Validator; |
11 | 13 | use Symfony\Component\HttpFoundation\Request; |
12 | 14 |
|
13 | 15 | class NotificationService |
14 | 16 | { |
15 | | - protected Request $request; |
16 | | - |
17 | | - /** |
18 | | - * @var array<int|string> |
19 | | - */ |
| 17 | + /** @var array<string, array<int|string>> */ |
20 | 18 | protected array $chatIds = []; |
21 | 19 |
|
22 | | - protected Notifier $notifier; |
23 | | - |
24 | | - protected Setting $setting; |
25 | | - |
26 | 20 | public function __construct( |
27 | | - Notifier $notifier, |
28 | | - Setting $setting, |
| 21 | + protected Notifier $notifier, |
| 22 | + protected Setting $setting, |
| 23 | + protected Request $request = new Request() |
29 | 24 | ) { |
30 | | - $this->request = Request::createFromGlobals(); |
31 | | - $this->notifier = $notifier; |
32 | | - $this->chatIds = $this->notifier->parseNotifyChatIds(); |
33 | | - |
34 | | - $this->setting = $setting; |
| 25 | + $this->request = $request ?? Request::createFromGlobals(); |
| 26 | + $this->chatIds = $notifier->parseNotifyChatIds(); |
35 | 27 | } |
36 | 28 |
|
37 | 29 | /** |
38 | | - * Handle to send notification from webhook event to telegram. |
39 | | - * |
40 | | - * @return void |
| 30 | + * Handle sending notification from webhook event to Telegram. |
41 | 31 | * |
42 | 32 | * @throws InvalidViewTemplateException |
43 | 33 | * @throws SendNotificationException |
44 | 34 | * @throws MessageIsEmptyException |
45 | 35 | */ |
46 | 36 | public function handle(): void |
47 | 37 | { |
48 | | - $eventName = $this->notifier->handleEventFromRequest($this->request); |
49 | | - if (!empty($eventName)) { |
| 38 | + if ($eventName = $this->notifier->handleEventFromRequest($this->request)) { |
50 | 39 | $this->sendNotification($eventName); |
51 | 40 | } |
52 | 41 | } |
53 | 42 |
|
54 | 43 | /** |
55 | | - * @param string $event |
56 | | - * @return void |
| 44 | + * Send notification to all configured chat IDs and threads. |
57 | 45 | * |
58 | 46 | * @throws InvalidViewTemplateException |
59 | 47 | * @throws SendNotificationException |
60 | 48 | * @throws MessageIsEmptyException |
61 | 49 | */ |
62 | 50 | private function sendNotification(string $event): void |
63 | 51 | { |
64 | | - if (!$this->validateAccessEvent($event)) { |
| 52 | + if (!$this->isValidEvent($event)) { |
65 | 53 | return; |
66 | 54 | } |
67 | 55 |
|
68 | | - foreach ($this->chatIds as $chatId => $thread) { |
| 56 | + foreach ($this->chatIds as $chatId => $threads) { |
69 | 57 | if (empty($chatId)) { |
70 | 58 | continue; |
71 | 59 | } |
72 | 60 |
|
73 | | - if (empty($thread)) { |
74 | | - $this->notifier->sendNotify(null, ['chat_id' => $chatId]); |
75 | | - |
76 | | - continue; |
77 | | - } |
78 | | - |
79 | | - /** @var array<int|string> $thread */ |
80 | | - foreach ($thread as $threadId) { |
81 | | - $this->notifier->sendNotify(null, [ |
82 | | - 'chat_id' => $chatId, 'message_thread_id' => $threadId, |
83 | | - ]); |
84 | | - } |
| 61 | + empty($threads) |
| 62 | + ? $this->sendToChat($chatId) |
| 63 | + : $this->sendToThreads($chatId, $threads); |
85 | 64 | } |
86 | 65 | } |
87 | 66 |
|
88 | 67 | /** |
89 | | - * Validate access event. |
| 68 | + * Send notification to a single chat. |
90 | 69 | * |
91 | | - * @param string $event |
92 | | - * @return bool |
| 70 | + * @throws SendNotificationException |
| 71 | + */ |
| 72 | + private function sendToChat(string $chatId): void |
| 73 | + { |
| 74 | + $this->notifier->sendNotify(null, ['chat_id' => $chatId]); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Send notification to multiple threads in a chat. |
| 79 | + * |
| 80 | + * @param array<int|string> $threads |
93 | 81 | * |
94 | | - * @throws InvalidViewTemplateException|MessageIsEmptyException |
| 82 | + * @throws SendNotificationException |
95 | 83 | */ |
96 | | - private function validateAccessEvent(string $event): bool |
| 84 | + private function sendToThreads(string $chatId, array $threads): void |
97 | 85 | { |
98 | | - $payload = $this->notifier->setPayload($this->request, $event); |
99 | | - $validator = new Validator($this->setting, $this->notifier->event); |
| 86 | + foreach ($threads as $threadId) { |
| 87 | + $this->notifier->sendNotify(null, [ |
| 88 | + 'chat_id' => $chatId, |
| 89 | + 'message_thread_id' => $threadId, |
| 90 | + ]); |
| 91 | + } |
| 92 | + } |
100 | 93 |
|
101 | | - if (empty($payload) || !is_object($payload) |
102 | | - || !$validator->isAccessEvent( |
103 | | - $this->notifier->event->platform, |
104 | | - $event, |
105 | | - $payload |
106 | | - ) |
107 | | - ) { |
| 94 | + /** |
| 95 | + * Check if the event is valid and accessible. |
| 96 | + */ |
| 97 | + private function isValidEvent(string $event): bool |
| 98 | + { |
| 99 | + $payload = $this->notifier->setPayload($this->request, $event); |
| 100 | + |
| 101 | + if (empty($payload) || !is_object($payload)) { |
108 | 102 | return false; |
109 | 103 | } |
110 | 104 |
|
111 | | - return true; |
| 105 | + $validator = new Validator($this->setting, $this->notifier->event); |
| 106 | + |
| 107 | + return $validator->isAccessEvent( |
| 108 | + $this->notifier->event->platform, |
| 109 | + $event, |
| 110 | + $payload |
| 111 | + ); |
112 | 112 | } |
113 | 113 | } |
0 commit comments