Skip to content

Commit c8d7355

Browse files
committed
Revert "refactor: optimize notification service"
This reverts commit 0b20a33
1 parent 3f20f02 commit c8d7355

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

src/Services/NotificationService.php

Lines changed: 54 additions & 54 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\{
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;
108
use CSlant\TelegramGitNotifier\Models\Setting;
119
use CSlant\TelegramGitNotifier\Notifier;
1210
use CSlant\TelegramGitNotifier\Objects\Validator;
1311
use Symfony\Component\HttpFoundation\Request;
1412

1513
class NotificationService
1614
{
17-
/** @var array<string, array<int|string>> */
15+
protected Request $request;
16+
17+
/**
18+
* @var array<int|string>
19+
*/
1820
protected array $chatIds = [];
1921

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

2937
/**
30-
* Handle sending notification from webhook event to Telegram.
38+
* Handle to send notification from webhook event to telegram.
39+
*
40+
* @return void
3141
*
3242
* @throws InvalidViewTemplateException
3343
* @throws SendNotificationException
3444
* @throws MessageIsEmptyException
3545
*/
3646
public function handle(): void
3747
{
38-
if ($eventName = $this->notifier->handleEventFromRequest($this->request)) {
48+
$eventName = $this->notifier->handleEventFromRequest($this->request);
49+
if (!empty($eventName)) {
3950
$this->sendNotification($eventName);
4051
}
4152
}
4253

4354
/**
44-
* Send notification to all configured chat IDs and threads.
55+
* @param string $event
56+
* @return void
4557
*
4658
* @throws InvalidViewTemplateException
4759
* @throws SendNotificationException
4860
* @throws MessageIsEmptyException
4961
*/
5062
private function sendNotification(string $event): void
5163
{
52-
if (!$this->isValidEvent($event)) {
64+
if (!$this->validateAccessEvent($event)) {
5365
return;
5466
}
5567

56-
foreach ($this->chatIds as $chatId => $threads) {
68+
foreach ($this->chatIds as $chatId => $thread) {
5769
if (empty($chatId)) {
5870
continue;
5971
}
6072

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]);
6675

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+
}
7678

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+
}
9185
}
9286
}
9387

9488
/**
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
9695
*/
97-
private function isValidEvent(string $event): bool
96+
private function validateAccessEvent(string $event): bool
9897
{
9998
$payload = $this->notifier->setPayload($this->request, $event);
99+
$validator = new Validator($this->setting, $this->notifier->event);
100100

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+
) {
102108
return false;
103109
}
104110

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

0 commit comments

Comments
 (0)