Completed
Push — master ( 23095d...54d687 )
by Danilo
02:12
created

MessageCommand::processMessageCommand()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 35
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 3
nop 1
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace PhpBotFramework\Commands;
4
5
use PhpBotFramework\Entities\Message;
6
7
trait MessageCommand {
8
9
    /**
10
     * \addtogroup Commands
11
     * \brief What commands are
12
     * @{
13
     */
14
15
    /** \brief (<i>Internal</i>)Store the command triggered on message. */
16
    protected $_message_commands;
17
18
    /**
19
     * \brief Add a function that will be executed everytime a message contain the selected command
20
     * \details Use this syntax:
21
     *
22
     *     addMessageCommand("start", function($bot, $message) {
23
     *         $bot->sendMessage("Hi"); });
24
     * @param $command The command that will trigger this function (without slash). Eg: "start", "help", "about"
25
     * @param $script The function that will be triggered by a command. Must take an object(the bot) and an array(the message received).
26
     */
27
    public function addMessageCommand(string $command, callable $script) {
28
29
        $this->_message_commands[] = [
30
            'script' => $script,
31
            'command' => '/' . $command,
32
            'length' => strlen($command) + 1,
33
        ];
34
35
    }
36
37
    /**
38
     * \brief (<i>Internal</i>)Process a message checking if it trigger any MessageCommand.
39
     * @param $message Message to process.
40
     * @return True if the message triggered any command.
41
     */
42
    protected function processMessageCommand(array $message) : bool {
43
44
        // If the message contains a bot command at the start
45
        if (isset($message['entities']) && $message['entities'][0]['type'] === 'bot_command') {
46
47
            // The lenght of the command
48
            $length = $message['entities'][0]['length'];
49
50
            // Offset of the command
51
            $offset = $message['entities'][0]['offset'];
52
53
            // For each command added by the user
54
            foreach ($this->_message_commands as $trigger) {
55
56
                // If we found a valid command
57
                if ($trigger['length'] == $length && mb_strpos($trigger['command'], $message['text'], $offset) !== false) {
58
59
                    // Set chat_id
60
                    $this->_chat_id = $message['chat']['id'];
0 ignored issues
show
Bug introduced by
The property _chat_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
62
                    // Execute script,
63
                    $trigger['script']($this, new Message($message));
64
65
                    // Return
66
                    return true;
67
                }
68
69
            }
70
71
        }
72
73
        // No command were triggered, return false
74
        return false;
75
76
    }
77
78
}
79