Passed
Pull Request — develop (#1077)
by Marco
02:09
created

DebugCommand::execute()   F

Complexity

Conditions 14
Paths 449

Size

Total Lines 74
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 74
ccs 0
cts 57
cp 0
rs 2.8652
c 0
b 0
f 0
cc 14
nc 449
nop 0
crap 210

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 PhpTelegramBot\Core\Commands\AdminCommands;
13
14
use PhpTelegramBot\Core\Commands\AdminCommand;
15
use PhpTelegramBot\Core\DB;
16
use PhpTelegramBot\Core\Exception\TelegramException;
17
use PhpTelegramBot\Core\Request;
18
19
/**
20
 * Admin "/debug" command
21
 */
22
class DebugCommand extends AdminCommand
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $name = 'debug';
28
29
    /**
30
     * @var string
31
     */
32
    protected $description = 'Debug command to help find issues';
33
34
    /**
35
     * @var string
36
     */
37
    protected $usage = '/debug';
38
39
    /**
40
     * @var string
41
     */
42
    protected $version = '1.1.0';
43
44
    /**
45
     * Command execute method
46
     *
47
     * @return mixed
48
     * @throws TelegramException
49
     */
50
    public function execute()
51
    {
52
        $pdo     = DB::getPdo();
53
        $message = $this->getMessage();
54
        $chat    = $message->getChat();
55
        $text    = strtolower($message->getText(true));
56
57
        $data = ['chat_id' => $chat->getId()];
58
59
        if ($text !== 'glasnost' && !$chat->isPrivateChat()) {
60
            $data['text'] = 'Only available in a private chat.';
61
62
            return Request::sendMessage($data);
63
        }
64
65
        $debug_info = [];
66
67
        $debug_info[] = sprintf('*TelegramBot version:* `%s`', $this->telegram->getVersion());
68
        $debug_info[] = sprintf('*Download path:* `%s`', $this->telegram->getDownloadPath() ?: '`_Not set_`');
69
        $debug_info[] = sprintf('*Upload path:* `%s`', $this->telegram->getUploadPath() ?: '`_Not set_`');
70
71
        // Commands paths.
72
        $debug_info[] = '*Commands paths:*';
73
        $debug_info[] = sprintf(
74
            '```' . PHP_EOL . '%s```',
75
            json_encode($this->telegram->getCommandsPaths(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
76
        );
77
78
        $php_bit = '';
79
        PHP_INT_SIZE === 4 && $php_bit = ' (32bit)';
80
        PHP_INT_SIZE === 8 && $php_bit = ' (64bit)';
81
        $debug_info[] = sprintf('*PHP version:* `%1$s%2$s; %3$s; %4$s`', PHP_VERSION, $php_bit, PHP_SAPI, PHP_OS);
82
        $debug_info[] = sprintf('*Maximum PHP script execution time:* `%d seconds`', ini_get('max_execution_time'));
83
84
        $mysql_version = $pdo ? $pdo->query('SELECT VERSION() AS version')->fetchColumn() : null;
0 ignored issues
show
introduced by
$pdo is of type PDO, thus it always evaluated to true.
Loading history...
85
        $debug_info[]  = sprintf('*MySQL version:* `%s`', $mysql_version ?: 'disabled');
86
87
        $debug_info[] = sprintf('*Operating System:* `%s`', php_uname());
88
89
        if (isset($_SERVER['SERVER_SOFTWARE'])) {
90
            $debug_info[] = sprintf('*Web Server:* `%s`', $_SERVER['SERVER_SOFTWARE']);
91
        }
92
        if (function_exists('curl_init')) {
93
            $curlversion  = curl_version();
94
            $debug_info[] = sprintf('*curl version:* `%1$s; %2$s`', $curlversion['version'], $curlversion['ssl_version']);
95
        }
96
97
        $webhook_info_title = '*Webhook Info:*';
98
        try {
99
            // Check if we're actually using the Webhook method.
100
            if (Request::getInput() === '') {
101
                $debug_info[] = $webhook_info_title . ' `Using getUpdates method, not Webhook.`';
102
            } else {
103
                $webhook_info_result = json_decode(Request::getWebhookInfo(), true)['result'];
104
                // Add a human-readable error date string if necessary.
105
                if (isset($webhook_info_result['last_error_date'])) {
106
                    $webhook_info_result['last_error_date_string'] = date('Y-m-d H:i:s', $webhook_info_result['last_error_date']);
107
                }
108
109
                $webhook_info_result_str = json_encode($webhook_info_result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
110
                $debug_info[]            = $webhook_info_title;
111
                $debug_info[]            = sprintf(
112
                    '```' . PHP_EOL . '%s```',
113
                    $webhook_info_result_str
114
                );
115
            }
116
        } catch (\Exception $e) {
117
            $debug_info[] = $webhook_info_title . sprintf(' `Failed to get webhook info! (%s)`', $e->getMessage());
118
        }
119
120
        $data['parse_mode'] = 'Markdown';
121
        $data['text']       = implode(PHP_EOL, $debug_info);
122
123
        return Request::sendMessage($data);
124
    }
125
}
126