Skip to content

Commit 0b20a33

Browse files
committed
refactor: optimize notification service
1 parent aee5268 commit 0b20a33

File tree

1 file changed

+55
-55
lines changed

1 file changed

+55
-55
lines changed

src/Services/NotificationService.php

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,112 +2,112 @@
22

33
namespace CSlant\LaravelTelegramGitNotifier\Services;
44

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+
};
810
use CSlant\TelegramGitNotifier\Models\Setting;
911
use CSlant\TelegramGitNotifier\Notifier;
1012
use CSlant\TelegramGitNotifier\Objects\Validator;
1113
use Symfony\Component\HttpFoundation\Request;
1214

1315
class NotificationService
1416
{
15-
protected Request $request;
16-
17-
/**
18-
* @var array<int|string>
19-
*/
17+
/** @var array<string, array<int|string>> */
2018
protected array $chatIds = [];
2119

22-
protected Notifier $notifier;
23-
24-
protected Setting $setting;
25-
2620
public function __construct(
27-
Notifier $notifier,
28-
Setting $setting,
21+
protected Notifier $notifier,
22+
protected Setting $setting,
23+
protected Request $request = new Request()
2924
) {
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();
3527
}
3628

3729
/**
38-
* Handle to send notification from webhook event to telegram.
39-
*
40-
* @return void
30+
* Handle sending notification from webhook event to Telegram.
4131
*
4232
* @throws InvalidViewTemplateException
4333
* @throws SendNotificationException
4434
* @throws MessageIsEmptyException
4535
*/
4636
public function handle(): void
4737
{
48-
$eventName = $this->notifier->handleEventFromRequest($this->request);
49-
if (!empty($eventName)) {
38+
if ($eventName = $this->notifier->handleEventFromRequest($this->request)) {
5039
$this->sendNotification($eventName);
5140
}
5241
}
5342

5443
/**
55-
* @param string $event
56-
* @return void
44+
* Send notification to all configured chat IDs and threads.
5745
*
5846
* @throws InvalidViewTemplateException
5947
* @throws SendNotificationException
6048
* @throws MessageIsEmptyException
6149
*/
6250
private function sendNotification(string $event): void
6351
{
64-
if (!$this->validateAccessEvent($event)) {
52+
if (!$this->isValidEvent($event)) {
6553
return;
6654
}
6755

68-
foreach ($this->chatIds as $chatId => $thread) {
56+
foreach ($this->chatIds as $chatId => $threads) {
6957
if (empty($chatId)) {
7058
continue;
7159
}
7260

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);
8564
}
8665
}
8766

8867
/**
89-
* Validate access event.
68+
* Send notification to a single chat.
9069
*
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
9381
*
94-
* @throws InvalidViewTemplateException|MessageIsEmptyException
82+
* @throws SendNotificationException
9583
*/
96-
private function validateAccessEvent(string $event): bool
84+
private function sendToThreads(string $chatId, array $threads): void
9785
{
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+
}
10093

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)) {
108102
return false;
109103
}
110104

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+
);
112112
}
113113
}

0 commit comments

Comments
 (0)