Passed
Push — 994-remove_service_message_sys... ( 049589 )
by Armando
02:28
created

executeDeprecatedSystemCommand()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 4
nop 0
dl 0
loc 40
ccs 0
cts 26
cp 0
crap 20
rs 9.504
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Commands\SystemCommands;
12
13
use Longman\TelegramBot\Commands\SystemCommand;
14
use Longman\TelegramBot\Conversation;
15
use Longman\TelegramBot\Entities\ServerResponse;
16
use Longman\TelegramBot\Exception\TelegramException;
17
use Longman\TelegramBot\Request;
18
19
/**
20
 * Generic message command
21
 */
22
class GenericmessageCommand extends SystemCommand
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $name = 'genericmessage';
28
29
    /**
30
     * @var string
31
     */
32
    protected $description = 'Handle generic message';
33
34
    /**
35
     * @var string
36
     */
37
    protected $version = '1.1.0';
38
39
    /**
40
     * @var bool
41
     */
42
    protected $need_mysql = true;
43
44
    /**
45
     * Execution if MySQL is required but not available
46
     *
47
     * @return ServerResponse
48
     */
49
    public function executeNoDb()
50
    {
51
        //Do nothing
52
        return Request::emptyResponse();
53
    }
54
55
    /**
56
     * Execute command
57
     *
58
     * @return ServerResponse
59
     * @throws TelegramException
60
     */
61
    public function execute()
62
    {
63
        //If a conversation is busy, execute the conversation command after handling the message
64
        $conversation = new Conversation(
65
            $this->getMessage()->getFrom()->getId(),
66
            $this->getMessage()->getChat()->getId()
67
        );
68
69
        //Fetch conversation command if it exists and execute it
70
        if ($conversation->exists() && ($command = $conversation->getCommand())) {
71
            return $this->getTelegram()->executeCommand($command);
72
        }
73
74
        // Try to execute any deprecated system commands.
75
        if ($deprecated_system_command_response = $this->executeDeprecatedSystemCommand()) {
76
            return $deprecated_system_command_response;
77
        }
78
79
        return Request::emptyResponse();
80
    }
81
82
83
    public function executeDeprecatedSystemCommand()
84
    {
85
        $telegram = $this->getTelegram();
86
        $update   = $this->getUpdate();
87
        $message  = $this->getMessage();
88
89
        // List of service messages previously handled internally.
90
        $service_messages = [
91
            'editedmessage'         => [$update, 'getEditedMessage'],
92
            'channelpost'           => [$update, 'getChannelPost'],
93
            'editedchannelpost'     => [$update, 'getEditedChannelPost'],
94
            'newchatmembers'        => [$message, 'getNewChatMembers'],
95
            'leftchatmember'        => [$message, 'getLeftChatMember'],
96
            'newchattitle'          => [$message, 'getNewChatTitle'],
97
            'newchatphoto'          => [$message, 'getNewChatPhoto'],
98
            'deletechatphoto'       => [$message, 'getDeleteChatPhoto'],
99
            'groupchatcreated'      => [$message, 'getGroupChatCreated'],
100
            'supergroupchatcreated' => [$message, 'getSupergroupChatCreated'],
101
            'channelchatcreated'    => [$message, 'getChannelChatCreated'],
102
            'migratefromchatid'     => [$message, 'getMigrateFromChatId'],
103
            'migratetochatid'       => [$message, 'getMigrateToChatId'],
104
            'pinnedmessage'         => [$message, 'getPinnedMessage'],
105
            'successfulpayment'     => [$message, 'getSuccessfulPayment'],
106
        ];
107
108
        foreach ($service_messages as $command => $service_message_getter) {
109
            // Let's check if this message is a service message.
110
            if ($service_message_getter() === null) {
111
                continue;
112
            }
113
114
            // Make sure the command exists, otherwise GenericCommand would be executed instead!
115
            if ($telegram->getCommandObject($command) === null) {
116
                break;
117
            }
118
119
            return $telegram->executeCommand($command);
120
        }
121
122
        return null;
123
    }
124
}
125