Conversation::getCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 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;
12
13
/**
14
 * Class Conversation
15
 *
16
 * Only one conversation can be active at any one time.
17
 * A conversation is directly linked to a user, chat and the command that is managing the conversation.
18
 */
19
class Conversation
20
{
21
    /**
22
     * All information fetched from the database
23
     *
24
     * @var array|null
25
     */
26
    protected $conversation;
27
28
    /**
29
     * Notes stored inside the conversation
30
     *
31
     * @var mixed
32
     */
33
    protected $protected_notes;
34
35
    /**
36
     * Notes to be stored
37
     *
38
     * @var mixed
39
     */
40
    public $notes;
41
42
    /**
43
     * Telegram user id
44
     *
45
     * @var int
46
     */
47
    protected $user_id;
48
49
    /**
50
     * Telegram chat id
51
     *
52
     * @var int
53
     */
54
    protected $chat_id;
55
56
    /**
57
     * Command to be executed if the conversation is active
58
     *
59
     * @var string
60
     */
61
    protected $command;
62
63
    /**
64
     * Conversation contructor to initialize a new conversation
65
     *
66
     * @param int    $user_id
67
     * @param int    $chat_id
68
     * @param string $command
69
     *
70
     * @throws \Longman\TelegramBot\Exception\TelegramException
71
     */
72 9
    public function __construct($user_id, $chat_id, $command = null)
73
    {
74 9
        $this->user_id = $user_id;
75 9
        $this->chat_id = $chat_id;
76 9
        $this->command = $command;
77
78
        //Try to load an existing conversation if possible
79 9
        if (!$this->load() && $command !== null) {
80
            //A new conversation start
81 6
            $this->start();
82
        }
83 8
    }
84
85
    /**
86
     * Clear all conversation variables.
87
     *
88
     * @return bool Always return true, to allow this method in an if statement.
89
     */
90 2
    protected function clear()
91
    {
92 2
        $this->conversation    = null;
93 2
        $this->protected_notes = null;
94 2
        $this->notes           = null;
95
96 2
        return true;
97
    }
98
99
    /**
100
     * Load the conversation from the database
101
     *
102
     * @return bool
103
     * @throws \Longman\TelegramBot\Exception\TelegramException
104
     */
105 9
    protected function load()
106
    {
107
        //Select an active conversation
108 9
        $conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109 9
        if (isset($conversation[0])) {
110
            //Pick only the first element
111 5
            $this->conversation = $conversation[0];
112
113
            //Load the command from the conversation if it hasn't been passed
114 5
            $this->command = $this->command ?: $this->conversation['command'];
115
116 5
            if ($this->command !== $this->conversation['command']) {
117
                $this->cancel();
118
                return false;
119
            }
120
121
            //Load the conversation notes
122 5
            $this->protected_notes = json_decode($this->conversation['notes'], true);
123 5
            $this->notes           = $this->protected_notes;
124
        }
125
126 9
        return $this->exists();
127
    }
128
129
    /**
130
     * Check if the conversation already exists
131
     *
132
     * @return bool
133
     */
134 9
    public function exists()
135
    {
136 9
        return ($this->conversation !== null);
137
    }
138
139
    /**
140
     * Start a new conversation if the current command doesn't have one yet
141
     *
142
     * @return bool
143
     * @throws \Longman\TelegramBot\Exception\TelegramException
144
     */
145 6
    protected function start()
146
    {
147 6
        if ($this->command
148 6
            && !$this->exists()
149 6
            && ConversationDB::insertConversation(
150 6
                $this->user_id,
151 6
                $this->chat_id,
152 6
                $this->command
153
            )
154
        ) {
155 5
            return $this->load();
156
        }
157
158
        return false;
159
    }
160
161
    /**
162
     * Delete the current conversation
163
     *
164
     * Currently the Conversation is not deleted but just set to 'stopped'
165
     *
166
     * @return bool
167
     */
168 1
    public function stop()
169
    {
170 1
        return ($this->updateStatus('stopped') && $this->clear());
171
    }
172
173
    /**
174
     * Cancel the current conversation
175
     *
176
     * @return bool
177
     */
178 1
    public function cancel()
179
    {
180 1
        return ($this->updateStatus('cancelled') && $this->clear());
181
    }
182
183
    /**
184
     * Update the status of the current conversation
185
     *
186
     * @param string $status
187
     *
188
     * @return bool
189
     */
190 2
    protected function updateStatus($status)
191
    {
192 2
        if ($this->exists()) {
193 2
            $fields = ['status' => $status];
194
            $where  = [
195 2
                'id'      => $this->conversation['id'],
196 2
                'status'  => 'active',
197 2
                'user_id' => $this->user_id,
198 2
                'chat_id' => $this->chat_id,
199
            ];
200 2
            if (ConversationDB::updateConversation($fields, $where)) {
201 2
                return true;
202
            }
203
        }
204
205
        return false;
206
    }
207
208
    /**
209
     * Store the array/variable in the database with json_encode() function
210
     *
211
     * @return bool
212
     */
213 1
    public function update()
214
    {
215 1
        if ($this->exists()) {
216 1
            $fields = ['notes' => json_encode($this->notes)];
217
            //I can update a conversation whatever the state is
218 1
            $where = ['id' => $this->conversation['id']];
219 1
            if (ConversationDB::updateConversation($fields, $where)) {
220 1
                return true;
221
            }
222
        }
223
224
        return false;
225
    }
226
227
    /**
228
     * Retrieve the command to execute from the conversation
229
     *
230
     * @return string|null
231
     */
232 3
    public function getCommand()
233
    {
234 3
        return $this->command;
235
    }
236
}
237