Completed
Push — main ( 750cf9...cfb036 )
by Tan
28s queued 13s
created

NotificationService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace CSlant\TelegramGitNotifierApp\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
    protected array $chatIds = [];
18
19
    protected Notifier $notifier;
20
21
    protected Setting $setting;
22
23
    public function __construct(
24
        Notifier $notifier,
25
        Setting $setting,
26
    ) {
27
        $this->request = Request::createFromGlobals();
28
        $this->notifier = $notifier;
29
        $this->chatIds = $this->notifier->parseNotifyChatIds();
30
31
        $this->setting = $setting;
32
    }
33
34
    /**
35
     * Handle to send notification from webhook event to telegram
36
     *
37
     * @return void
38
     * @throws InvalidViewTemplateException
39
     * @throws SendNotificationException
40
     * @throws MessageIsEmptyException
41
     */
42
    public function handle(): void
43
    {
44
        $eventName = $this->notifier->handleEventFromRequest($this->request);
45
        if (!empty($eventName)) {
46
            $this->sendNotification($eventName);
47
        }
48
    }
49
50
    /**
51
     * @param string $event
52
     *
53
     * @return void
54
     * @throws InvalidViewTemplateException
55
     * @throws SendNotificationException
56
     * @throws MessageIsEmptyException
57
     */
58
    private function sendNotification(string $event): void
59
    {
60
        if (!$this->validateAccessEvent($event)) {
61
            return;
62
        }
63
64
        foreach ($this->chatIds as $chatId => $thread) {
65
            if (empty($chatId)) {
66
                continue;
67
            }
68
69
            if (empty($thread)) {
70
                $this->notifier->sendNotify(null, ['chat_id' => $chatId]);
71
72
                continue;
73
            }
74
75
            foreach ($thread as $threadId) {
76
                $this->notifier->sendNotify(null, [
77
                    'chat_id' => $chatId, 'message_thread_id' => $threadId,
78
                ]);
79
            }
80
        }
81
    }
82
83
    /**
84
     * Validate access event
85
     *
86
     * @param string $event
87
     *
88
     * @return bool
89
     * @throws InvalidViewTemplateException|MessageIsEmptyException
90
     */
91
    private function validateAccessEvent(string $event): bool
92
    {
93
        $payload = $this->notifier->setPayload($this->request, $event);
94
        $validator = new Validator($this->setting, $this->notifier->event);
95
96
        if (empty($payload)
97
            || !$validator->isAccessEvent(
98
                $this->notifier->event->platform,
99
                $event,
100
                $payload
101
            )
102
        ) {
103
            return false;
104
        }
105
106
        return true;
107
    }
108
}
109