Completed
Push — master ( 3845e2...a95f7d )
by Danilo
09:37
created

CommandHandler::sortingPrior()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 2
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 be triggered by 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>) Initialize commands to speed up processing.
44
     * \details Get commands handled by the bot and prioritize them.
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
        $this->_command_types = [];
59
60
        foreach ($commands as $index => $command) {
61
            if (isset($this->{$command['var']}) && !empty($this->{$command['var']})) {
62
                $this->_command_types[] = ['method' => "process$index", 'update' => $command['update']];
63
            }
64
        }
65
    }
66
67
    /**
68
     * \brief Process updates prioritizing bot's commands over the general methods (e.g. BaseBot::processMessage())
69
     * @param array $update Update to process.
70
     * @return bool True if this update trigger any command.
71
     */
72
    protected function processCommands(array $update) : bool
73
    {
74
        // For each command active (checked by initCommands())
75
        foreach ($this->_command_types as $index => $command) {
76
            // If the update type is right and the update triggered a command
77
            if (isset($update[$command['update']]) && $this->{$command['method']}($update[$command['update']])) {
78
                // Return the id as we already processed this update
79
                return true;
80
            }
81
        }
82
83
        // Return -1 because this update didn't trigger any command.
84
        return false;
85
    }
86
87
    /**
88
     * \brief (<i>Internal</i>) Sort an array based on <code>prior</code> index value.
89
     * @param array $a First array.
90
     * @param array $b Second array.
91
     * @return int 1 If $a > $b, -1 if $a < $b, 0 otherwise.
92
     */
93
    public static function sortingPrior($a, $b)
94
    {
95
        if ($a['prior'] > $b['prior']) {
96
            return 1;
97
        }
98
99
        if ($a['prior'] < $b['prior']) {
100
            return -1;
101
        }
102
103
        if ($a['prior'] == $b['prior']) {
104
            return 0;
105
        }
106
    }
107
108
    /** @} */
109
}
110