Completed
Push — master ( a95f7d...eaa6fe )
by Danilo
02:55
created

CallbackCommand::processCallbackCommand()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 8
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
crap 20
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
/**
22
 * \addtogroup Commands
23
 * @{
24
 */
25
26
/** \class CallbackCommand
27
 */
28
class CallbackCommand extends BasicCommand
29
{
30
    /** @} */
31
32
    public static $type = 'callback_query';
33
34
    public static $object_class = 'PhpBotFramework\Entities\CallbackQuery';
35
36
    public static $priority = 1;
37
38
    private $data;
39
40
    /**
41
     * \brief Add a function that will be executed everytime a callback query contains a string as data
42
     * \details Use this syntax:
43
     *
44
     *     addMessageCommand("menu", function($bot, $callback_query) {
45
     *         $bot->editMessageText($callback_query['message']['message_id'], "This is the menu"); });
46
     *
47
     * @param string $data The string that will trigger this function.
48
     * @param callable $script The function that will be triggered by the callback query if it contains
49
     * the $data string. Must take an object(the bot) and an array(the callback query received).
50
     */
51
    public function __construct(string $data, callable $script)
52
    {
53
        $this->data = $data;
54
        $this->script = $script;
55
    }
56
57
    /**
58
     * \brief (<i>Internal</i>) Process the callback query and check if it triggers a command of this type.
59
     * @param array $callback_query Callback query to process.
60
     * @return bool True if the callback query triggered a command.
61
     */
62
    public function checkCommand(array $callback_query) : bool
63
    {
64
        if (isset($callback_query['data'])) {
65
            // If command is found in callback data
66
            if (strpos($this->data, $callback_query['data']) !== false) {
67
                return true;
68
            }
69
        }
70
71
        return false;
72
    }
73
74
    /** @} */
75
76
    /** @} */
77
}
78