Completed
Push — master ( ef9e39...904d42 )
by
unknown
08:21 queued 06:18
created

CancelCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 81
ccs 0
cts 16
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 17 2
A removeKeyboard() 0 10 1
A executeNoDb() 0 4 1
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\Request;
17
18
/**
19
 * User "/cancel" command
20
 *
21
 * This command cancels the currently active conversation and
22
 * returns a message to let the user know which conversation it was.
23
 * If no conversation is active, the returned message says so.
24
 */
25
class CancelCommand extends UserCommand
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $name = 'cancel';
31
32
    /**
33
     * @var string
34
     */
35
    protected $description = 'Cancel the currently active conversation';
36
37
    /**
38
     * @var string
39
     */
40
    protected $usage = '/cancel';
41
42
    /**
43
     * @var string
44
     */
45
    protected $version = '0.2.0';
46
47
    /**
48
     * @var bool
49
     */
50
    protected $need_mysql = true;
51
52
    /**
53
     * Command execute method
54
     *
55
     * @return \Longman\TelegramBot\Entities\ServerResponse
56
     * @throws \Longman\TelegramBot\Exception\TelegramException
57
     */
58
    public function execute()
59
    {
60
        $text = 'No active conversation!';
61
62
        //Cancel current conversation if any
63
        $conversation = new Conversation(
64
            $this->getMessage()->getFrom()->getId(),
65
            $this->getMessage()->getChat()->getId()
66
        );
67
68
        if ($conversation_command = $conversation->getCommand()) {
69
            $conversation->cancel();
70
            $text = 'Conversation "' . $conversation_command . '" cancelled!';
71
        }
72
73
        return $this->removeKeyboard($text);
74
    }
75
76
    /**
77
     * Remove the keyboard and output a text
78
     *
79
     * @param string $text
80
     *
81
     * @return \Longman\TelegramBot\Entities\ServerResponse
82
     * @throws \Longman\TelegramBot\Exception\TelegramException
83
     */
84
    private function removeKeyboard($text)
85
    {
86
        return Request::sendMessage(
87
            [
88
                'reply_markup' => Keyboard::remove(['selective' => true]),
89
                'chat_id'      => $this->getMessage()->getChat()->getId(),
90
                'text'         => $text,
91
            ]
92
        );
93
    }
94
95
    /**
96
     * Execute no db
97
     *
98
     * @return \Longman\TelegramBot\Entities\ServerResponse
99
     * @throws \Longman\TelegramBot\Exception\TelegramException
100
     */
101
    public function executeNoDb()
102
    {
103
        return $this->removeKeyboard('Nothing to cancel.');
104
    }
105
}
106