Passed
Pull Request — develop (#1233)
by
unknown
02:43
created

CallbackqueryCommand::proper_parse_str()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 31
ccs 0
cts 11
cp 0
crap 20
rs 9.8666
1
<?php
2
3
/**
4
 * This file is part of the TelegramBot package.
5
 *
6
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Longman\TelegramBot\Commands\SystemCommands;
13
14
use Longman\TelegramBot\Commands\SystemCommand;
15
use Longman\TelegramBot\Entities\ServerResponse;
16
17
/**
18
 * Callback query command
19
 */
20
class CallbackqueryCommand extends SystemCommand
21
{
22
    /**
23
     * @var callable[]
24
     */
25
    protected static $callbacks = [];
26
27
    /**
28
     * @var string
29
     */
30
    protected $name = 'callbackquery';
31
32
    /**
33
     * @var string
34
     */
35
    protected $description = 'Reply to callback query';
36
37
    /**
38
     * @var string
39
     */
40
    protected $version = '1.0.0';
41
42
    /**
43
     * Command execute method
44
     *
45
     * @return ServerResponse
46
     */
47
    public function execute(): ServerResponse
48
    {
49
        //$callback_query = $this->getCallbackQuery();
50
        //$user_id        = $callback_query->getFrom()->getId();
51
        //$query_id       = $callback_query->getId();
52
        //$query_data     = $callback_query->getData();
53
        //$query_data_array = $this->proper_parse_str($query_data);
54
55
        $answer         = null;
56
        $callback_query = $this->getCallbackQuery();
57
58
        // Call all registered callbacks.
59
        foreach (self::$callbacks as $callback) {
60
            $answer = $callback($callback_query);
61
            //$answer = $callback($callback_query, $query_data_array);
62
        }
63
64
        return ($answer instanceof ServerResponse) ? $answer : $callback_query->answer();
65
    }
66
67
    /**
68
     * Add a new callback handler for callback queries.
69
     *
70
     * @param $callback
71
     */
72
    public static function addCallbackHandler($callback): void
73
    {
74
        if (!in_array($callback, self::$callbacks))
75
            self::$callbacks[] = $callback;
76
    }
77
    
78
     /**
79
     * Pharses a query string and returns an array of it.
80
     * https://www.php.net/manual/en/function.parse-str.php
81
     * @param $str
82
     */
83
84
    private function proper_parse_str($str)
0 ignored issues
show
Unused Code introduced by
The method proper_parse_str() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
85
    {
86
        # result array
87
        $arr = array();
88
89
        # split on outer delimiter
90
        $pairs = explode('&', $str);
91
92
      # loop through each pair
93
      foreach ($pairs as $i) {
94
        # split into name and value
95
        list($name,$value) = explode('=', $i, 2);
96
97
        # if name already exists
98
        if( isset($arr[$name]) ) {
99
          # stick multiple values into an array
100
          if( is_array($arr[$name]) ) {
101
            $arr[$name][] = $value;
102
          }
103
          else {
104
            $arr[$name] = array($arr[$name], $value);
105
          }
106
        }
107
        # otherwise, simply stick it in a scalar
108
        else {
109
          $arr[$name] = $value;
110
        }
111
      }
112
113
      # return result array
114
      return $arr;
115
    }
116
}
117