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