Completed
Push — master ( 7268ad...8470e3 )
by
unknown
03:48
created

CallbackCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 64
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addCallbackCommand() 0 7 1
A processCallbackCommand() 0 17 4
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Commands;
20
21
use PhpBotFramework\Entities\CallbackQuery;
22
23
/**
24
 * \addtogroup Modules
25
 * @{
26
 */
27
28
/** \class CallbackCommand
29
 */
30
trait CallbackCommand
31
{
32
    /** @} */
33
34
    /** \brief Chat ID of the current user/group/channel. */
35
    protected $_chat_id;
36
37
    /**
38
     * \addtogroup Bot Bot
39
     * @{
40
     */
41
42
    /**
43
     * \addtogroup Commands
44
     * @{
45
     */
46
47
    /** \brief Store the command triggered on callback query. */
48
    protected $_callback_commands;
49
50
    /**
51
     * \brief Add a function that will be executed everytime a callback query contains a string as data
52
     * \details Use this syntax:
53
     *
54
     *     addMessageCommand("menu", function($bot, $callback_query) {
55
     *         $bot->editMessageText($callback_query['message']['message_id'], "This is the menu"); });
56
     * @param string $data The string that will trigger this function.
57
     * @param callable $script The function that will be triggered by the callback query if it contains the $data string. Must take an object(the bot) and an array(the callback query received).
58
     */
59
    public function addCallbackCommand(string $data, callable $script)
60
    {
61
        $this->_callback_commands[] = [
62
            'data' => $data,
63
            'script' => $script,
64
        ];
65
    }
66
67
    /**
68
     * \brief (<i>Internal</i>) Process the callback query and check if it triggers a command of this type.
69
     * @param array $callback_query Callback query to process.
70
     * @return bool True if the callback query triggered a command.
71
     */
72
    protected function processCallbackCommand(array $callback_query) : bool
73
    {
74
        // Check for callback commands
75
        if (isset($callback_query['data'])) {
76
            foreach ($this->_callback_commands as $trigger) {
77
                // If command is found in callback data
78
                if (strpos($trigger['data'], $callback_query['data']) !== false) {
79
                    $this->_chat_id = $callback_query['message']['chat']['id'];
80
                    $trigger['script']($this, new CallbackQuery($callback_query));
81
82
                    return true;
83
                }
84
            }
85
        }
86
87
        return false;
88
    }
89
90
    /** @} */
91
92
    /** @} */
93
}
94