Completed
Push — master ( c9c19d...a94344 )
by Danilo
06:15
created

MultiCharacterCommand::__construct()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 22
cp 0
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 3
crap 20
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\Exceptions\BotException;
22
23
/**
24
 * \addtogroup Commands
25
 * @{
26
 */
27
28
/** \class MessageCommand
29
 */
30
class MultiCharacterCommand extends BasicCommand
31
{
32
    /** @} */
33
34
    public static $type = 'message';
35
36
    public static $object_class = 'PhpBotFramework\Entities\Message';
37
38
    public static $priority = 1;
39
40
    private $command;
41
42
    private $length;
43
44
    /**
45
     * \brief Add a function that will be executed everytime a message contain the selected command
46
     * \details Use this syntax to create a command:
47
     *
48
     *     $help_cpmmand = new PhpBotFramework\Commands\MultiCharacterCommand("help",
49
     *         function ($bot, $message) {
50
     *             $bot->sendMessage("This is a help message.");
51
     *         }
52
     *     );
53
     *
54
     * Then you can add it to the bot's commands using <code>addCommand</code> method:
55
     *
56
     *     $bot->addCommand($help_command);
57
     *
58
     * @param string $command The command that will trigger this function (e.g. start)
59
     * @param callable $script The function that will be triggered by a command.
60
     * Must take an object(the bot) and an array(the message received).
61
     */
62
    public function __construct(string $command, callable $script, string ...$chars)
63
    {
64
        $chars_count = count($chars);
65
        if ( $chars_count === 0)
66
        {
67
            throw new BotException("No character given for matching the command");
68
        }
69
70
        if ($chars_count === 1)
71
        {
72
            $this->regex_rule = $chars[0] . $command;
0 ignored issues
show
Bug introduced by
The property regex_rule 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...
73
        }
74
        else
75
        {
76
            $this->regex_rule = '(' . $chars[0];
77
            foreach($chars as $char)
78
            {
79
                $this->regex_rule .= '|' . $char;
80
            }
81
            $this->regex_rule .= ')' . $command;
82
        }
83
84
        $this->script = $script;
85
    }
86
87
    /**
88
     * @internal
89
     * \brief Process a message checking if it trigger any MessageCommand.
90
     * @param string $message Message to process.
91
     * @return bool True if the message trigger any command.
92
     */
93
    public function checkCommand(array $message) : bool
94
    {
95
        // If we found a valid command (check first lenght, then use strpos)
96
        if (preg_match("/{$this->regex_rule}/", $message['text'])) {
97
                    // Return
98
                    return true;
99
        }
100
101
        return false;
102
    }
103
}
104