Completed
Push — master ( 69097b...4e1c03 )
by Danilo
04:33
created

MessageCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 51
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B checkCommand() 0 13 6
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:
45
     *
46
     *     addMessageCommand("start", function($bot, $message) {
47
     *         $bot->sendMessage("Hi"); });
48
     * @param string $command The command that will trigger this function (without slash). Eg: "start", "help", "about"
49
     * @param callable $script The function that will be triggered by a command.
50
     * Must take an object(the bot) and an array(the message received).
51
     */
52 1
    public function __construct(string $command, callable $script)
53
    {
54 1
        $this->command = "/$command";
55 1
        $this->script = $script;
56 1
        $this->length = strlen($command) + 1;
57 1
    }
58
59
    /**
60
     * @internal
61
     * \brief Process a message checking if it trigger any MessageCommand.
62
     * @param string $message Message to process.
63
     * @return bool True if the message trigger any command.
64
     */
65 1
    public function checkCommand(array $message) : bool
66
    {
67
        // If the message contains a bot command at the start
68 1
        $message_is_command = (isset($message['entities']) && $message['entities'][0]['type'] === 'bot_command') ? true : false;
69
70
        // If we found a valid command (check first lenght, then use strpos)
71 1
        if ($message_is_command && $this->length == $message['entities'][0]['length'] && mb_strpos($this->command, $message['text'], $message['entities'][0]['offset']) !== false) {
72
                    // Return
73 1
                    return true;
74
        }
75
76
        return false;
77
    }
78
}
79