Completed
Pull Request — develop (#291)
by Armando
15:42
created

SurveyCommand   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 229
Duplicated Lines 26.2 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 25
c 0
b 0
f 0
lcom 1
cbo 11
dl 60
loc 229
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F execute() 60 188 25

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Conversation;
15
use Longman\TelegramBot\Entities\Keyboard;
16
use Longman\TelegramBot\Entities\KeyboardButton;
17
use Longman\TelegramBot\Entities\PhotoSize;
18
use Longman\TelegramBot\Request;
19
20
/**
21
 * User "/survery" command
22
 */
23
class SurveyCommand extends UserCommand
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $name = 'survey';
29
30
    /**
31
     * @var string
32
     */
33
    protected $description = 'Survery for bot users';
34
35
    /**
36
     * @var string
37
     */
38
    protected $usage = '/survey';
39
40
    /**
41
     * @var string
42
     */
43
    protected $version = '0.3.0';
44
45
    /**
46
     * @var bool
47
     */
48
    protected $need_mysql = true;
49
50
    /**
51
     * Conversation Object
52
     *
53
     * @var \Longman\TelegramBot\Conversation
54
     */
55
    protected $conversation;
56
57
    /**
58
     * Command execute method
59
     *
60
     * @return mixed
61
     * @throws \Longman\TelegramBot\Exception\TelegramException
62
     */
63
    public function execute()
64
    {
65
        $message = $this->getMessage();
66
67
        $chat = $message->getChat();
68
        $user = $message->getFrom();
69
        $text = trim($message->getText(true));
70
        $chat_id = $chat->getId();
71
        $user_id = $user->getId();
72
73
        //Preparing Response
74
        $data = [
75
            'chat_id' => $chat_id,
76
        ];
77
78
        if ($chat->isGroupChat() || $chat->isSuperGroup()) {
79
            //reply to message id is applied by default
80
            //Force reply is applied by default so it can work with privacy on
81
            $data['reply_markup'] = Keyboard::forceReply(['selective' => true]);
82
        }
83
84
        //Conversation start
85
        $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
86
87
        $notes = &$this->conversation->notes;
88
        !is_array($notes) && $notes = [];
89
90
        //cache data from the tracking session if any
91
        $state = 0;
92
        if (isset($notes['state'])) {
93
            $state = $notes['state'];
94
        }
95
96
        $result = Request::emptyResponse();
97
98
        //State machine
99
        //Entrypoint of the machine state if given by the track
100
        //Every time a step is achieved the track is updated
101
        switch ($state) {
102
            case 0:
103 View Code Duplication
                if ($text === '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
                    $notes['state'] = 0;
105
                    $this->conversation->update();
106
107
                    $data['text']         = 'Type your name:';
108
                    $data['reply_markup'] = Keyboard::hide(['selective' => true]);
109
110
                    $result = Request::sendMessage($data);
111
                    break;
112
                }
113
114
                $notes['name'] = $text;
115
                $text          = '';
116
117
            // no break
118
            case 1:
119 View Code Duplication
                if ($text === '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
                    $notes['state'] = 1;
121
                    $this->conversation->update();
122
123
                    $data['text'] = 'Type your surname:';
124
125
                    $result = Request::sendMessage($data);
126
                    break;
127
                }
128
129
                $notes['surname'] = $text;
130
                $text             = '';
131
132
            // no break
133
            case 2:
134
                if ($text === '' || !is_numeric($text)) {
135
                    $notes['state'] = 2;
136
                    $this->conversation->update();
137
138
                    $data['text'] = 'Type your age:';
139
                    if ($text !== '') {
140
                        $data['text'] = 'Type your age, must be a number:';
141
                    }
142
143
                    $result = Request::sendMessage($data);
144
                    break;
145
                }
146
147
                $notes['age'] = $text;
148
                $text         = '';
149
150
            // no break
151
            case 3:
152
                if ($text === '' || !in_array($text, ['M', 'F'], true)) {
153
                    $notes['state'] = 3;
154
                    $this->conversation->update();
155
156
                    $data['reply_markup'] = (new Keyboard(['M', 'F']))
157
                        ->setResizeKeyboard(true)
158
                        ->setOneTimeKeyboard(true)
159
                        ->setSelective(true);
160
161
                    $data['text'] = 'Select your gender:';
162
                    if ($text !== '') {
163
                        $data['text'] = 'Select your gender, choose a keyboard option:';
164
                    }
165
166
                    $result = Request::sendMessage($data);
167
                    break;
168
                }
169
170
                $notes['gender'] = $text;
171
172
            // no break
173
            case 4:
174 View Code Duplication
                if ($message->getLocation() === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
                    $notes['state'] = 4;
176
                    $this->conversation->update();
177
178
                    $data['reply_markup'] = (new Keyboard(
179
                        (new KeyboardButton('Share Location'))->setRequestLocation(true)
180
                    ))
181
                        ->setOneTimeKeyboard(true)
182
                        ->setResizeKeyboard(true)
183
                        ->setSelective(true);
184
185
                    $data['text'] = 'Share your location:';
186
187
                    $result = Request::sendMessage($data);
188
                    break;
189
                }
190
191
                $notes['longitude'] = $message->getLocation()->getLongitude();
192
                $notes['latitude']  = $message->getLocation()->getLatitude();
193
194
            // no break
195
            case 5:
196 View Code Duplication
                if ($message->getPhoto() === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
                    $notes['state'] = 5;
198
                    $this->conversation->update();
199
200
                    $data['text'] = 'Insert your picture:';
201
202
                    $result = Request::sendMessage($data);
203
                    break;
204
                }
205
206
                /** @var PhotoSize $photo */
207
                $photo             = $message->getPhoto()[0];
208
                $notes['photo_id'] = $photo->getFileId();
209
210
            // no break
211
            case 6:
212 View Code Duplication
                if ($message->getContact() === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
                    $notes['state'] = 6;
214
                    $this->conversation->update();
215
216
                    $data['reply_markup'] = (new Keyboard(
217
                        (new KeyboardButton('Share Contact'))->setRequestContact(true)
218
                    ))
219
                        ->setOneTimeKeyboard(true)
220
                        ->setResizeKeyboard(true)
221
                        ->setSelective(true);
222
223
                    $data['text'] = 'Share your contact information:';
224
225
                    $result = Request::sendMessage($data);
226
                    break;
227
                }
228
229
                $notes['phone_number'] = $message->getContact()->getPhoneNumber();
230
231
            // no break
232
            case 7:
233
                $this->conversation->update();
234
                $out_text = '/Survey result:' . PHP_EOL;
235
                unset($notes['state']);
236
                foreach ($notes as $k => $v) {
237
                    $out_text .= PHP_EOL . ucfirst($k) . ': ' . $v;
238
                }
239
240
                $data['photo']        = $notes['photo_id'];
241
                $data['reply_markup'] = Keyboard::hide(['selective' => true]);
242
                $data['caption']      = $out_text;
243
                $this->conversation->stop();
244
245
                $result = Request::sendPhoto($data);
246
                break;
247
        }
248
249
        return $result;
250
    }
251
}
252