11<?php
22
3+ declare (strict_types=1 );
4+
35namespace CSlant \LaravelTelegramGitNotifier \Http \Actions ;
46
57use CSlant \LaravelTelegramGitNotifier \Services \CallbackService ;
1517use CSlant \TelegramGitNotifier \Exceptions \SendNotificationException ;
1618use CSlant \TelegramGitNotifier \Notifier ;
1719use GuzzleHttp \Client ;
20+ use GuzzleHttp \ClientInterface ;
1821use Symfony \Component \HttpFoundation \Request ;
19- use Telegram ;
22+ use Telegram \ Telegram as TelegramSDK ;
2023
2124class IndexAction
2225{
23- protected Client $ client ;
24-
25- protected Bot $ bot;
26-
27- protected Notifier $ notifier ;
28-
29- protected Request $ request ;
26+ public function __construct (
27+ private ClientInterface $ client ,
28+ private Bot $ bot,
29+ private Notifier $ notifier ,
30+ private Request $ request
31+ ) {
32+ }
3033
3134 /**
35+ * Create a new instance with default dependencies.
36+ *
3237 * @throws ConfigFileException
3338 */
34- public function __construct ()
39+ public static function createDefault (): self
3540 {
36- $ this ->client = new Client ();
37-
38- $ telegram = new Telegram (config ('telegram-git-notifier.bot.token ' ));
39- $ this ->bot = new Bot ($ telegram );
40- $ this ->notifier = new Notifier ();
41+ return new self (
42+ new Client (),
43+ new Bot (new TelegramSDK (config ('telegram-git-notifier.bot.token ' ))),
44+ new Notifier (),
45+ Request::createFromGlobals ()
46+ );
4147 }
4248
4349 /**
4450 * Handle telegram git notifier app.
4551 *
46- * @return void
47- *
4852 * @throws InvalidViewTemplateException
4953 * @throws MessageIsEmptyException
5054 * @throws SendNotificationException
@@ -55,23 +59,69 @@ public function __construct()
5559 public function __invoke (): void
5660 {
5761 if ($ this ->bot ->isCallback ()) {
58- $ callbackAction = new CallbackService ($ this ->bot );
59- $ callbackAction ->handle ();
62+ $ this ->handleCallback ();
63+ return ;
64+ }
6065
66+ if ($ this ->shouldHandleCommand ()) {
67+ $ this ->handleCommand ();
6168 return ;
6269 }
6370
64- if ($ this ->bot ->isMessage () && $ this ->bot ->isOwner ()) {
65- $ commandAction = new CommandService ($ this ->bot );
66- $ commandAction ->handle ();
71+ $ this ->handleNotification ();
72+ }
73+
74+ /**
75+ * Handle callback actions.
76+ *
77+ * @throws CallbackException
78+ * @throws EntryNotFoundException
79+ * @throws InvalidViewTemplateException
80+ * @throws MessageIsEmptyException
81+ */
82+ private function handleCallback (): void
83+ {
84+ $ callbackAction = new CallbackService ($ this ->bot );
85+ $ callbackAction ->handle ();
86+ }
6787
88+ /**
89+ * Handle command messages.
90+ *
91+ * @throws EntryNotFoundException
92+ * @throws MessageIsEmptyException
93+ */
94+ private function handleCommand (): void
95+ {
96+ if (!$ this ->bot ->isMessage () || !$ this ->bot ->isOwner ()) {
6897 return ;
6998 }
7099
71- $ sendNotification = new NotificationService (
100+ $ commandAction = new CommandService ($ this ->bot );
101+ $ commandAction ->handle ();
102+ }
103+
104+ /**
105+ * Handle notification sending.
106+ *
107+ * @throws InvalidViewTemplateException
108+ * @throws MessageIsEmptyException
109+ * @throws SendNotificationException
110+ */
111+ private function handleNotification (): void
112+ {
113+ $ notificationService = new NotificationService (
72114 $ this ->notifier ,
73115 $ this ->bot ->setting
74116 );
75- $ sendNotification ->handle ();
117+ $ notificationService ->handle ();
118+ }
119+
120+ /**
121+ * Check if the current message should be handled as a command.
122+ */
123+ private function shouldHandleCommand (): bool
124+ {
125+ return $ this ->bot ->isMessage () && $ this ->bot ->isOwner ();
76126 }
77127}
0 commit comments