Completed
Pull Request — master (#41)
by
unknown
03:59
created

MessageCommand::checkCommand()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 8
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 5
nc 8
nop 1
crap 42
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 MessageCommand
27
 */
28
class MessageCommand extends BasicCommand
29
{
30
    /** @} */
31
32
    public static $type = 'message';
33
34
    public static $object_class = 'PhpBotFramework\Entities\Message';
35
36
    public static $priority = 1;
37
38
    private $command;
39
40
    private $length;
41
42
    /**
43
     * \brief Add a function that will be executed everytime a message contain the selected command
44
     * \details Use this syntax to create a command:
45
     *
46
     *     $start_command = new PhpBotFramework\Commands\MessageCommand("start",
47
     *         function ($bot, $message) {
48
     *             $bot->sendMessage("Hello, folks!");
49
     *         }
50
     *     );
51
     *
52
     * Then you can add it to the bot's commands using <code>addCommand</code> method:
53
     *
54
     *     $bot->addCommand($start_command);
55
     *
56
     * @param string $command The command that will trigger this function (e.g. start)
57
     * @param callable $script The function that will be triggered by a command.
58
     * Must take an object(the bot) and an array(the message received).
59
     */
60
    public function __construct(string $command, callable $script)
61
    {
62
        $this->command = "/$command";
63
        $this->script = $script;
64
        $this->length = strlen($command) + 1;
65
    }
66
67
    /**
68
     * @internal
69
     * \brief Process a message checking if it trigger any MessageCommand.
70
     * @param string $message Message to process.
71
     * @return bool True if the message trigger any command.
72
     */
73
    public function checkCommand(array $message) : bool
74
    {
75
        // If the message contains a bot command at the start
76
        $message_is_command = (isset($message['entities']) && $message['entities'][0]['type'] === 'bot_command') ? true : false;
77
78
        // If we found a valid command (check first lenght, then use strpos)
79
        if ($message_is_command && $this->length == $message['entities'][0]['length'] && mb_strpos($this->command, $message['text'], $message['entities'][0]['offset']) !== false) {
80
                    // Return
81
                    return true;
82
        }
83
84
        return false;
85
    }
86
}
87