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