Completed
Push — master ( 1c54e2...48ad84 )
by Armando
02:22
created

DebugCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
c 4
b 0
f 0
lcom 1
cbo 6
dl 0
loc 98
ccs 0
cts 54
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D execute() 0 69 11
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Commands\AdminCommands;
12
13
use Longman\TelegramBot\Commands\AdminCommand;
14
use Longman\TelegramBot\DB;
15
use Longman\TelegramBot\Request;
16
17
/**
18
 * Admin "/debug" command
19
 */
20
class DebugCommand extends AdminCommand
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $name = 'debug';
26
27
    /**
28
     * @var string
29
     */
30
    protected $description = 'Debug command to help find issues';
31
32
    /**
33
     * @var string
34
     */
35
    protected $usage = '/debug';
36
37
    /**
38
     * @var string
39
     */
40
    protected $version = '1.1.0';
41
42
    /**
43
     * Command execute method
44
     *
45
     * @return mixed
46
     * @throws \Longman\TelegramBot\Exception\TelegramException
47
     */
48
    public function execute()
0 ignored issues
show
Coding Style introduced by
execute uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
49
    {
50
        $pdo = DB::getPdo();
51
        $message = $this->getMessage();
52
        $chat = $message->getChat();
53
        $text = strtolower($message->getText(true));
54
55
        $data = ['chat_id' => $chat->getId()];
56
57
        if ($text !== 'glasnost' && !$chat->isPrivateChat()) {
58
            $data['text'] = 'Only available in a private chat.';
59
60
            return Request::sendMessage($data);
61
        }
62
63
        $debug_info = [];
64
65
        $debug_info[] = sprintf('*TelegramBot version:* `%s`', $this->telegram->getVersion());
66
        $debug_info[] = sprintf('*Download path:* `%s`', $this->telegram->getDownloadPath());
67
        $debug_info[] = sprintf('*Upload path:* `%s`', $this->telegram->getUploadPath());
68
69
        // Commands paths.
70
        $debug_info[] = '*Commands paths:*';
71
        $debug_info[] = sprintf(
72
            '```' . PHP_EOL . '%s```',
73
            json_encode($this->telegram->getCommandsPaths(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
74
        );
75
76
        $php_bit = '';
77
        PHP_INT_SIZE === 4 && $php_bit = ' (32bit)';
78
        PHP_INT_SIZE === 8 && $php_bit = ' (64bit)';
79
        $debug_info[] = sprintf('*PHP version:* `%1$s%2$s; %3$s; %4$s`', PHP_VERSION, $php_bit, PHP_SAPI, PHP_OS);
80
        $debug_info[] = sprintf('*Maximum PHP script execution time:* `%d seconds`', ini_get('max_execution_time'));
81
82
        $mysql_version = $pdo ? $pdo->query('SELECT VERSION() AS version')->fetchColumn() : null;
83
        $debug_info[] = sprintf('*MySQL version:* `%s`', $mysql_version ?: 'disabled');
84
85
        $debug_info[] = sprintf('*Operating System:* `%s`', php_uname());
86
87
        if (isset($_SERVER['SERVER_SOFTWARE'])) {
88
            $debug_info[] = sprintf('*Web Server:* `%s`', $_SERVER['SERVER_SOFTWARE']);
89
        }
90
        if (function_exists('curl_init')) {
91
            $curlversion = curl_version();
92
            $debug_info[] = sprintf('*curl version:* `%1$s; %2$s`', $curlversion['version'], $curlversion['ssl_version']);
93
        }
94
95
        $webhook_info_title = '*Webhook Info:*';
96
        try {
97
            // Check if we're actually using the Webhook method.
98
            if (Request::getInput() === '') {
99
                $debug_info[] = $webhook_info_title . ' `Using getUpdates method, not Webhook.`';
100
            } else {
101
                $webhook_info_result = json_encode(json_decode(Request::getWebhookInfo(), true)['result'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
102
                $debug_info[] = $webhook_info_title;
103
                $debug_info[] = sprintf(
104
                    '```' . PHP_EOL . '%s```',
105
                    $webhook_info_result
106
                );
107
            }
108
        } catch (\Exception $e) {
109
            $debug_info[] = $webhook_info_title . sprintf(' `Failed to get webhook info! (%s)`', $e->getMessage());
110
        }
111
112
        $data['parse_mode'] = 'Markdown';
113
        $data['text'] = implode(PHP_EOL, $debug_info);
114
115
        return Request::sendMessage($data);
116
    }
117
}
118