Passed
Push — develop ( d5b99f...e9c693 )
by Armando
07:24
created

SystemCommand::executeDeprecatedSystemCommand()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 5
nop 0
dl 0
loc 38
ccs 0
cts 0
cp 0
crap 30
rs 9.2408
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;
12
13
use Longman\TelegramBot\Conversation;
14
use Longman\TelegramBot\Entities\ServerResponse;
15
use Longman\TelegramBot\Exception\TelegramException;
16
use Longman\TelegramBot\Request;
17
18
abstract class SystemCommand extends Command
19
{
20
    /**
21
     * @var bool Try to execute any deprecated system command.
22
     */
23
    public static $execute_deprecated = false;
24
25
    /**
26
     * @{inheritdoc}
27
     *
28
     * Set to empty string to disallow users calling system commands.
29
     */
30
    protected $usage = '';
31
32
    /**
33
     * A system command just executes
34
     *
35
     * Although system commands should just work and return a successful ServerResponse,
36
     * each system command can override this method to add custom functionality.
37
     *
38
     * @return ServerResponse
39
     */
40
    public function execute()
41
    {
42
        // System command, return empty ServerResponse by default
43
        return Request::emptyResponse();
44
    }
45
46
    /**
47
     * Method to execute any active conversation.
48
     *
49
     * @return ServerResponse|null
50
     * @throws TelegramException
51
     * @internal
52
     */
53
    protected function executeActiveConversation()
54
    {
55
        $message = $this->getMessage();
56
        if ($message === null) {
57
            return null;
58
        }
59
60
        $user = $message->getFrom();
61
        $chat = $message->getChat();
62
        if ($user === null || $chat === null) {
63
            return null;
64
        }
65
66
        // If a conversation is busy, execute the conversation command after handling the message.
67
        $conversation = new Conversation($user->getId(), $chat->getId());
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
        return null;
75
    }
76
77
    /**
78
     * BC helper method to execute deprecated system commands.
79
     *
80
     * @return ServerResponse|null
81
     * @throws TelegramException
82
     * @internal
83
     */
84
    protected function executeDeprecatedSystemCommand()
85
    {
86
        $message = $this->getMessage();
87
        if ($message === null) {
88
            return null;
89
        }
90
91
        // List of service messages previously handled internally.
92
        $service_message_getters = [
93
            'newchatmembers'        => 'getNewChatMembers',
94
            'leftchatmember'        => 'getLeftChatMember',
95
            'newchattitle'          => 'getNewChatTitle',
96
            'newchatphoto'          => 'getNewChatPhoto',
97
            'deletechatphoto'       => 'getDeleteChatPhoto',
98
            'groupchatcreated'      => 'getGroupChatCreated',
99
            'supergroupchatcreated' => 'getSupergroupChatCreated',
100
            'channelchatcreated'    => 'getChannelChatCreated',
101
            'migratefromchatid'     => 'getMigrateFromChatId',
102
            'migratetochatid'       => 'getMigrateToChatId',
103
            'pinnedmessage'         => 'getPinnedMessage',
104
            'successfulpayment'     => 'getSuccessfulPayment',
105
        ];
106
107
        foreach ($service_message_getters as $command => $service_message_getter) {
108
            // Let's check if this message is a service message.
109
            if ($message->$service_message_getter() === null) {
110
                continue;
111
            }
112
113
            // Make sure the command exists otherwise GenericCommand would be executed.
114
            if ($this->getTelegram()->getCommandObject($command) === null) {
115
                break;
116
            }
117
118
            return $this->getTelegram()->executeCommand($command);
119
        }
120
121
        return null;
122
    }
123
}
124