Skip to content

Commit f9575da

Browse files
committed
refactor: optimize services
1 parent 0b20a33 commit f9575da

File tree

8 files changed

+521
-218
lines changed

8 files changed

+521
-218
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
}
3737
},
3838
"require": {
39-
"php": "^8.1",
39+
"php": "^8.2",
4040
"cslant/telegram-git-notifier": "^v1.4"
4141
},
4242
"require-dev": {

src/Http/Actions/IndexAction.php

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;
46

57
use CSlant\LaravelTelegramGitNotifier\Services\CallbackService;
@@ -15,36 +17,38 @@
1517
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
1618
use CSlant\TelegramGitNotifier\Notifier;
1719
use GuzzleHttp\Client;
20+
use GuzzleHttp\ClientInterface;
1821
use Symfony\Component\HttpFoundation\Request;
19-
use Telegram;
22+
use Telegram\Telegram as TelegramSDK;
2023

2124
class 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
}

src/Http/Actions/WebhookAction.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;
46

57
use CSlant\LaravelTelegramGitNotifier\Services\WebhookService;
68
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
79

810
class WebhookAction
911
{
10-
protected WebhookService $webhookService;
11-
12-
public function __construct()
13-
{
14-
$this->webhookService = new WebhookService();
12+
public function __construct(
13+
private readonly WebhookService $webhookService
14+
) {
1515
}
1616

1717
/**
1818
* Set webhook for telegram bot.
1919
*
20-
* @return string
21-
*
2220
* @throws WebhookException
2321
*/
2422
public function set(): string
@@ -29,8 +27,6 @@ public function set(): string
2927
/**
3028
* Delete webhook for telegram bot.
3129
*
32-
* @return string
33-
*
3430
* @throws WebhookException
3531
*/
3632
public function delete(): string
@@ -41,8 +37,6 @@ public function delete(): string
4137
/**
4238
* Get webhook update.
4339
*
44-
* @return string
45-
*
4640
* @throws WebhookException
4741
*/
4842
public function getUpdates(): string
@@ -53,8 +47,6 @@ public function getUpdates(): string
5347
/**
5448
* Get webhook info.
5549
*
56-
* @return string
57-
*
5850
* @throws WebhookException
5951
*/
6052
public function getWebHookInfo(): string

0 commit comments

Comments
 (0)