11<?php
22
3- declare (strict_types=1 );
4-
53namespace CSlant \LaravelTelegramGitNotifier \Http \Actions ;
64
75use CSlant \LaravelTelegramGitNotifier \Services \CallbackService ;
1715use CSlant \TelegramGitNotifier \Exceptions \SendNotificationException ;
1816use CSlant \TelegramGitNotifier \Notifier ;
1917use GuzzleHttp \Client ;
20- use GuzzleHttp \ClientInterface ;
2118use Symfony \Component \HttpFoundation \Request ;
22- use Telegram \ Telegram as TelegramSDK ;
19+ use Telegram ;
2320
2421class IndexAction
2522{
26- public function __construct (
27- private ClientInterface $ client ,
28- private Bot $ bot,
29- private Notifier $ notifier ,
30- private Request $ request
31- ) {
32- }
23+ protected Client $ client ;
24+
25+ protected Bot $ bot;
26+
27+ protected Notifier $ notifier ;
28+
29+ protected Request $ request ;
3330
3431 /**
35- * Create a new instance with default dependencies.
36- *
3732 * @throws ConfigFileException
3833 */
39- public static function createDefault (): self
34+ public function __construct ()
4035 {
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- );
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 ();
4741 }
4842
4943 /**
5044 * Handle telegram git notifier app.
5145 *
46+ * @return void
47+ *
5248 * @throws InvalidViewTemplateException
5349 * @throws MessageIsEmptyException
5450 * @throws SendNotificationException
@@ -59,69 +55,23 @@ public static function createDefault(): self
5955 public function __invoke (): void
6056 {
6157 if ($ this ->bot ->isCallback ()) {
62- $ this ->handleCallback ();
63- return ;
64- }
58+ $ callbackAction = new CallbackService ($ this ->bot );
59+ $ callbackAction ->handle ();
6560
66- if ($ this ->shouldHandleCommand ()) {
67- $ this ->handleCommand ();
6861 return ;
6962 }
7063
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- }
64+ if ($ this ->bot ->isMessage () && $ this ->bot ->isOwner ()) {
65+ $ commandAction = new CommandService ($ this ->bot );
66+ $ commandAction ->handle ();
8767
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 ()) {
9768 return ;
9869 }
9970
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 (
71+ $ sendNotification = new NotificationService (
11472 $ this ->notifier ,
11573 $ this ->bot ->setting
11674 );
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 ();
75+ $ sendNotification ->handle ();
12676 }
12777}
0 commit comments