Skip to content

Commit aad43cc

Browse files
committed
Optimized behaviour of effective entities
1 parent 956a43c commit aad43cc

File tree

3 files changed

+16
-52
lines changed

3 files changed

+16
-52
lines changed

src/Telegram/Commands/CallbackqueryCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function execute(): ServerResponse
4040
}
4141

4242
// Check if conversation is active
43-
$user = $this->getEffectiveUser();
44-
$chat = $this->getEffectiveChat();
43+
$user = $this->getEffectiveUser($this->getUpdate());
44+
$chat = $this->getEffectiveChat($this->getUpdate());
4545

4646
$conversation = new Conversation(
4747
user_id: $user->getId(),

src/Telegram/Commands/GenericmessageCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function execute(): ServerResponse
2525
return $return;
2626
}
2727

28-
$user = $this->getEffectiveUser();
29-
$chat = $this->getEffectiveChat();
28+
$user = $this->getEffectiveUser($this->getUpdate());
29+
$chat = $this->getEffectiveChat($this->getUpdate());
3030

3131
// Check Conversation
3232
$conversation = new Conversation(

src/Telegram/UsesEffectiveEntities.php

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,74 +7,38 @@
77
use Longman\TelegramBot\Entities\Update;
88
use Longman\TelegramBot\Entities\User;
99

10-
/**
11-
* @method getUpdate(): Update
12-
*/
1310
trait UsesEffectiveEntities
1411
{
1512

16-
protected function getEffectiveUser(): ?User
13+
protected function getEffectiveUser(Update $update): ?User
1714
{
18-
if (! method_exists($this, 'getUpdate')) {
19-
return null;
20-
}
21-
22-
$update = $this->getUpdate();
23-
24-
if (! $update instanceof Update) {
25-
return null;
26-
}
27-
28-
$data = $update->getRawData();
2915
$type = $update->getUpdateType();
3016

31-
$user = $data[$type]['from']
32-
?? $data['poll_answer']['user']
17+
$user = $update->$type['from']
18+
?? $update->poll_answer['user']
3319
?? null;
3420

3521
return $user ? new User($user) : null;
3622
}
3723

38-
protected function getEffectiveChat(): ?Chat
24+
protected function getEffectiveChat(Update $update): ?Chat
3925
{
40-
if (! method_exists($this, 'getUpdate')) {
41-
return null;
42-
}
43-
44-
$update = $this->getUpdate();
45-
46-
if (! $update instanceof Update) {
47-
return null;
48-
}
49-
50-
$data = $update->getRawData();
5126
$type = $update->getUpdateType();
5227

53-
$chat = $data[$type]['chat']
54-
?? $data['callback_query']['message']['chat']
28+
$chat = $update->$type['chat']
29+
?? $update->callback_query['message']['chat']
5530
?? null;
5631

5732
return $chat ? new Chat($chat) : null;
5833
}
5934

60-
protected function getEffectiveMessage(): ?Message
35+
protected function getEffectiveMessage(Update $update): ?Message
6136
{
62-
if (! method_exists($this, 'getUpdate')) {
63-
return null;
64-
}
65-
66-
$update = $this->getUpdate();
67-
68-
if (! $update instanceof Update) {
69-
return null;
70-
}
71-
72-
$data = $update->getRawData();
73-
$message = $data['edited_channel_post']
74-
?? $data['channel_post']
75-
?? $data['callback_query']['message']
76-
?? $data['edited_message']
77-
?? $data['message']
37+
$message = $update->edited_channel_post
38+
?? $update->channel_post
39+
?? $update->callback_query['message']
40+
?? $update->edited_message
41+
?? $update->message
7842
?? null;
7943

8044
return $message ? new Message($message) : null;

0 commit comments

Comments
 (0)