Completed
Push — master ( b9f469...4e0207 )
by Danilo
03:56
created

CommandHandler::processCommands()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
nop 1
crap 4
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 all command that the bot handle, and put them in priority.
45
     */
46 3
    protected function initCommands()
47
    {
48
        // All command types with respective update
49 3
        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 3
        uasort($commands, 'PhpBotFramework\Commands\CommandHandler::sortingPrior');
57
58 3
        $this->_command_types = [];
59
60
        // Iterate over each
61 3
        foreach ($commands as $index => $command) {
62 3
            if (isset($this->{$command['var']}) && !empty($this->{$command['var']})) {
63 3
                $this->_command_types[] = ['method' => "process$index", 'update' => $command['update']];
64
            }
65
        }
66 3
    }
67
68
    /**
69
     * \brief Process updates handling commands.
70
     * @param array $update Update to process.
71
     * @return bool Tru if this update trigger any command.
72
     */
73 3
    protected function processCommands(array $update) : int
74
    {
75
        // For each command active (checked by initCommands())
76 3
        foreach ($this->_command_types as $index => $command) {
77
            // If the update type is right and the update triggered a command
78 1
            if (isset($update[$command['update']]) && $this->{$command['method']}($update[$command['update']])) {
79
                // Return the id as we already processed this update
80 1
                return true;
81
            }
82
        }
83
84
        // Return -1 because this update didn't trigger any command.
85 2
        return false;
86
    }
87
88
    /**
89
     * \brief (<i>Internal</i>) Sort an array based on <code>prior</code> index value.
90
     * @param array $a First array.
91
     * @param array $b Second array.
92
     * @return int 1 If $a > $b, -1 if $a < $b, 0 otherwise.
93
     */
94 3
    public static function sortingPrior($a, $b)
95
    {
96 3
        if ($a['prior'] > $b['prior']) {
97
            return 1;
98
        }
99
100 3
        if ($a['prior'] < $b['prior']) {
101 3
            return -1;
102
        }
103
104 3
        if ($a['prior'] == $b['prior']) {
105 3
            return 0;
106
        }
107
    }
108
109
    /** @} */
110
}
111