Completed
Push — master ( 549b3b...080752 )
by Danilo
15:29
created

CommandHandler::initCommands()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 7
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 0
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
/**
22
 * \addtogroup Modules
23
 * @{
24
 */
25
26
/** \class CallbackCommandHandler
27
 */
28
trait CommandHandler
29
{
30
    /** @} */
31
32
    /**
33
     * \addtogroup Commands
34
     * \brief What commands are
35
     * @{
36
     */
37
38
    /** \brief (<i>Internal</i>)contains all command that can trigger the bot.
39
     * \details E.g. Add each type of command processed by the bot into this array to avoid overhead. */
40
    protected $_command_types;
41
42
    /**
43
     * \brief (<i>Internal</i>) Init commands to speed up processing.
44
     * \details Get all command that the bot handle, and put them in priority.
45
     */
46
    protected function initCommands()
47
    {
48
        // All command types with respective update
49
        static $commands = ['MessageCommand' =>
50
            ['var' => '_message_commands', 'update' => 'message', 'prior' => '1'],
51
            'CallbackCommand' =>
52
            ['var' => '_callback_commands', 'update' => 'callback_query', 'prior' => '1'],
53
            'MessageRegexCommand' => ['var' => '_message_regex_commands', 'update' => 'message', 'prior' => '2']];
54
55
        // Sort them by priority
56
        uasort($commands, 'PhpBotFramework\Commands\CommandHandler::sortingPrior');
57
58
        // Iterate over each
59
        foreach ($commands as $index => $command) {
60
            // If there is at least a command of that type (by checking that the container exists and it is not empty)
61
            if (isset($this->{$command['var']}) && !empty($this->{$command['var']})) {
62
                // Add the type to the command container
63
                $this->_command_types[] = ['method' => "process$index", 'update' => $command['update']];
64
            }
65
        }
66
    }
67
68
    /**
69
     * \brief Process updates handling first commands, and then general methods (e.g. BaseBot::processMessage())
70
     * @param $update Update to process.
71
     * @return Id of the update processed.
72
     */
73
    protected function processUpdate(array $update) : int
74
    {
75
        // For each command active (checked by initCommands())
76
        foreach ($this->_command_types as $index => $command) {
77
            // If the update type is right and the update triggered a command
78
            if (isset($update[$command['update']]) && $this->{$command['method']}($update[$command['update']])) {
79
                // Return the id as we already processed this update
80
                return $update['update_id'];
81
            }
82
        }
83
84
        // Call the parent method because this update didn't trigger any command
85
        return parent::processUpdate($update);
86
    }
87
88
    // Use to sort _command_types based on prior
89
    public static function sortingPrior($a, $b)
90
    {
91
        if ($a['prior'] > $b['prior']) {
92
            return 1;
93
        }
94
95
        if ($a['prior'] < $b['prior']) {
96
            return -1;
97
        }
98
99
        if ($a['prior'] == $b['prior']) {
100
            return 0;
101
        }
102
    }
103
104
    /** @} */
105
}
106