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

MessageRegexCommand::addMessageCommandRegex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpBotFramework\Commands;
4
5
use PhpBotFramework\Entities\Message;
6
7
trait MessageRegexCommand {
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_regex_commands;
17
18
    /**
19
     * \brief Add a function that will be executed everytime a message contain a command that match the regex
20
     * \details Use this syntax:
21
     *
22
     *     addMessageCommandRegex("number\d", function($bot, $message, $result) {
23
     *         $bot->sendMessage("You sent me a number"); });
24
     * @param $regex_rule Regex rule that will called for evalueting the command received.
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 addMessageCommandRegex(string $regex_rule, callable $script) {
28
29
        $this->_message_commands[] = [
0 ignored issues
show
Bug introduced by
The property _message_commands 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...
30
            'script' => $script,
31
            'regex_rule' => $regex_rule
32
        ];
33
34
    }
35
36
    /**
37
     * \brief (<i>Internal</i>) Process the message to check if it triggers a command of this type.
38
     * @param $message Message to process.
39
     * @return True if the message triggered a command.
40
     */
41
    protected function processMessageRegexCommand(array $message) : bool {
42
43
        // and there are bot commands in the message, checking message entities
44
        if (isset($message['entities']) && $message['entities'][0]['type'] === 'bot_command') {
45
46
            // The lenght of the command
47
            $length = $message['entities'][0]['length'];
48
49
            // Offset of the command
50
            $offset = $message['entities'][0]['offset'];
51
52
            // For each command added by the user
53
            foreach ($this->_message_commands as $trigger) {
54
55
                // Use preg_match to check if it is true
56
                if (preg_match('/' . $trigger['regex_rule'] . '/', substr($message['text'], $offset + 1, $length))) {
57
58
                    $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...
59
60
                    // Trigger the script
61
                    $trigger['script']($this, new Message($message));
62
63
                    // The message triggered a command, return true
64
                    return true;
65
66
                }
67
68
            }
69
70
        }
71
72
        // No command were triggered
73
        return false;
74
75
    }
76
77
}
78