Completed
Pull Request — develop (#288)
by Armando
27:44 queued 12:40
created

KeyboardCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 73
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 73
rs 9.0675
cc 1
eloc 43
nc 1
nop 0

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\UserCommand;
14
use Longman\TelegramBot\Request;
15
use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
16
17
/**
18
 * User "/keyboard" command
19
 */
20
class KeyboardCommand extends UserCommand
21
{
22
    /**#@+
23
     * {@inheritdoc}
24
     */
25
    protected $name = 'keyboard';
26
    protected $description = 'Show a custom keyboard with reply markup';
27
    protected $usage = '/keyboard';
28
    protected $version = '0.1.0';
29
    /**#@-*/
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function execute()
35
    {
36
        $message = $this->getMessage();
37
        $chat_id = $message->getChat()->getId();
38
39
        $data = [
40
            'chat_id' => $chat_id,
41
            'text'    => 'Press a Button:',
42
        ];
43
44
        //Keyboard examples
45
        $keyboards = [];
46
47
        //Example 0
48
        $keyboard    = [];
49
        $keyboard[]  = ['7', '8', '9'];
50
        $keyboard[]  = ['4', '5', '6'];
51
        $keyboard[]  = ['1', '2', '3'];
52
        $keyboard[]  = [' ', '0', ' '];
53
        $keyboards[] = $keyboard;
54
55
        //Example 1
56
        $keyboard    = [];
57
        $keyboard[]  = ['7', '8', '9', '+'];
58
        $keyboard[]  = ['4', '5', '6', '-'];
59
        $keyboard[]  = ['1', '2', '3', '*'];
60
        $keyboard[]  = [' ', '0', ' ', '/'];
61
        $keyboards[] = $keyboard;
62
63
        //Example 2
64
        $keyboard    = [];
65
        $keyboard[]  = ['A'];
66
        $keyboard[]  = ['B'];
67
        $keyboard[]  = ['C'];
68
        $keyboards[] = $keyboard;
69
70
        //Example 3
71
        $keyboard    = [];
72
        $keyboard[]  = ['A'];
73
        $keyboard[]  = ['B'];
74
        $keyboard[]  = ['C', 'D'];
75
        $keyboards[] = $keyboard;
76
77
        //Example 4 (bots version 2.0)
78
        $keyboard    = [];
79
        $keyboard[]  = [
80
            [
81
                'text'            => 'Send my contact',
82
                'request_contact' => true,
83
            ],
84
            [
85
                'text'             => 'Send my location',
86
                'request_location' => true,
87
            ],
88
        ];
89
        $keyboards[] = $keyboard;
90
91
        //Return a random keyboard.
92
        $keyboard             = $keyboards[mt_rand(0, count($keyboards) - 1)];
93
        $data['reply_markup'] = new ReplyKeyboardMarkup(
94
            [
95
                'keyboard'          => $keyboard,
96
                'resize_keyboard'   => true,
97
                'one_time_keyboard' => false,
98
                'selective'         => false,
99
            ]
100
        );
101
102
        return Request::sendMessage($data);
103
    }
104
}
105