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

CommandHandler.php ➔ sortingPrior()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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