Completed
Push — master ( 3845e2...a95f7d )
by Danilo
09:37
created

MessageCommand::addMessageCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 2
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\Message;
22
23
/**
24
 * \addtogroup Modules
25
 * @{
26
 */
27
28
/** \class MessageCommand
29
 */
30
trait MessageCommand
31
{
32
    /** @} */
33
34
    /** \brief Chat id of the current user/group/channel. */
35
    protected $_chat_id;
36
37
    /**
38
     * \addtogroup Commands
39
     * \brief What commands are
40
     * @{
41
     */
42
43
    /** \brief (<i>Internal</i>)Store the command triggered on message. */
44
    protected $_message_commands = [];
45
46
    /**
47
     * \brief Add a function that will be executed everytime a message contain the selected command
48
     * \details Use this syntax:
49
     *
50
     *     addMessageCommand("start", function($bot, $message) {
51
     *         $bot->sendMessage("Hi"); });
52
     * @param string $command The command that will trigger this function (without slash). Eg: "start", "help", "about"
53
     * @param callable $script The function that will be triggered by a command.
54
     * Must take an object(the bot) and an array(the message received).
55
     */
56
    public function addMessageCommand(string $command, callable $script)
57
    {
58
        $this->_message_commands[] = [
59
            'script' => $script,
60
            'command' => '/' . $command,
61
            'length' => strlen($command) + 1,
62
        ];
63
    }
64
65
    /**
66
     * \brief (<i>Internal</i>)Process a message checking if it trigger any MessageCommand.
67
     * @param string $message Message to process.
68
     * @return bool True if the message trigger any command.
69
     */
70 View Code Duplication
    protected function processMessageCommand(array $message) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        if (isset($message['entities']) && $message['entities'][0]['type'] === 'bot_command') {
73
            // For each command added by the user
74
            foreach ($this->_message_commands as $trigger) {
75
                // If we found a valid command (check first length, then use strpos)
76
                if ($trigger['length'] == $message['entities'][0]['length'] &&
77
                    mb_strpos($trigger['command'], $message['text'], $message['entities'][0]['offset']) !== false) {
78
                    // Execute the script.
79
                    $this->_chat_id = $message['chat']['id'];
80
                    $trigger['script']($this, new Message($message));
81
82
                    return true;
83
                }
84
            }
85
        }
86
87
        return false;
88
    }
89
}
90