Completed
Pull Request — develop (#457)
by
unknown
03:35 queued 54s
created

HelpCommand::execute()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 63
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 5
Bugs 0 Features 1
Metric Value
dl 0
loc 63
ccs 0
cts 50
cp 0
rs 8.6498
c 5
b 0
f 1
cc 6
eloc 39
nc 4
nop 0
crap 42

How to fix   Long Method   

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
 * 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\UserCommands;
12
13
use Longman\TelegramBot\Commands\Command;
14
use Longman\TelegramBot\Commands\UserCommand;
15
use Longman\TelegramBot\Request;
16
17
/**
18
 * User "/help" command
19
 */
20
class HelpCommand extends UserCommand
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $name = 'help';
26
27
    /**
28
     * @var string
29
     */
30
    protected $description = 'Show bot commands help';
31
32
    /**
33
     * @var string
34
     */
35
    protected $usage = '/help or /help <command>';
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()
49
    {
50
        $message = $this->getMessage();
51
        $chat_id = $message->getChat()->getId();
52
53
        $message_id = $message->getMessageId();
54
        $command    = trim($message->getText(true));
55
56
        //Only get enabled Admin and User commands
57
        /** @var Command[] $command_objs */
58
        $command_objs = array_filter($this->telegram->getCommandsList(), function ($command_obj) {
59
            /** @var Command $command_obj */
60
            return !$command_obj->isSystemCommand() && $command_obj->isEnabled();
61
        });
62
63
        //If no command parameter is passed, show the list
64
        if ($command === '') {
65
            $text = sprintf(
66
                '%s v. %s' . PHP_EOL . PHP_EOL . 'Commands List:' . PHP_EOL,
67
                $this->telegram->getBotName(),
68
                $this->telegram->getVersion()
69
            );
70
71
            foreach ($command_objs as $command) {
72
                if (!$command->showInHelp()) {
73
                    continue;
74
                }
75
76
                $text .= sprintf(
77
                    '/%s - %s' . PHP_EOL,
78
                    $command->getName(),
79
                    $command->getDescription()
80
                );
81
            }
82
83
            $text .= PHP_EOL . 'For exact command help type: /help <command>';
84
        } else {
85
            $command = str_replace('/', '', $command);
86
            if (isset($command_objs[$command])) {
87
                /** @var Command $command_obj */
88
                $command_obj = $command_objs[$command];
89
                $text = sprintf(
90
                    'Command: %s v%s' . PHP_EOL .
91
                    'Description: %s' . PHP_EOL .
92
                    'Usage: %s',
93
                    $command_obj->getName(),
94
                    $command_obj->getVersion(),
95
                    $command_obj->getDescription(),
96
                    $command_obj->getUsage()
97
                );
98
            } else {
99
                $text = 'No help available: Command /' . $command . ' not found';
100
            }
101
        }
102
103
        $data = [
104
            'chat_id'             => $chat_id,
105
            'reply_to_message_id' => $message_id,
106
            'text'                => $text,
107
        ];
108
109
        return Request::sendMessage($data);
110
    }
111
}
112