Skip to content

Commit 2858ff5

Browse files
committed
Merge pull request #9 from noplanman/small_cleanup_fixes
Small cleanup fixes
2 parents 8a5a5c5 + b8e3b4b commit 2858ff5

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

src/Commands/AdminCommands/SendtochannelCommand.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,20 @@ public function execute()
215215
$this->conversation->notes['last_message_id'] = $message->getMessageId();
216216
// no break
217217
case 5:
218-
$this->conversation->stop();
219218
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
220219

221220
if ($this->conversation->notes['post_message']) {
222-
$data['text'] = $this->publish(new Message($this->conversation->notes['message'], 'anystring'), $this->conversation->notes['channel'], $this->conversation->notes['caption']);
223-
$result = Request::sendMessage($data);
224-
break;
221+
$data['text'] = $this->publish(
222+
new Message($this->conversation->notes['message'], 'anystring'),
223+
$this->conversation->notes['channel'],
224+
$this->conversation->notes['caption']
225+
);
226+
} else {
227+
$data['text'] = 'Abort by user, message not sent..';
225228
}
226229

227-
$data['text'] = 'Abort by user, message not sent..';
230+
$this->conversation->stop();
228231
$result = Request::sendMessage($data);
229-
break;
230232
}
231233
return $result;
232234
}

src/Conversation.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ class Conversation
6060
*/
6161
protected $command;
6262

63-
/**
64-
* Command has been provided
65-
*
66-
* @var string
67-
*/
68-
protected $command_is_provided;
69-
7063
/**
7164
* Conversation contructor to initialize a new conversation
7265
*
@@ -80,26 +73,35 @@ public function __construct($user_id, $chat_id, $command = null)
8073
$this->chat_id = $chat_id;
8174
$this->command = $command;
8275

83-
$this->command_is_provided = ($command !== null);
84-
8576
//Try to load an existing conversation if possible
86-
if (!$this->load() && $this->command_is_provided) {
77+
if (!$this->load() && $command !== null) {
8778
//A new conversation start
8879
$this->start();
8980
}
9081
}
9182

9283
/**
93-
* Load the conversation from the database
84+
* Clear all conversation variables.
9485
*
95-
* @return bool
86+
* @return bool Always return true, to allow this method in an if statement.
9687
*/
97-
protected function load()
88+
protected function clear()
9889
{
9990
$this->conversation = null;
10091
$this->protected_notes = null;
10192
$this->notes = null;
10293

94+
return true;
95+
}
96+
97+
/**
98+
* Load the conversation from the database
99+
*
100+
* @return bool
101+
*/
102+
protected function load()
103+
{
104+
103105
//Select an active conversation
104106
$conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
105107
if (isset($conversation[0])) {
@@ -111,7 +113,6 @@ protected function load()
111113

112114
if ($this->command !== $this->conversation['command']) {
113115
$this->cancel();
114-
$this->conversation = null;
115116
return false;
116117
}
117118

@@ -162,7 +163,7 @@ protected function start()
162163
*/
163164
public function stop()
164165
{
165-
return $this->updateStatus('stopped');
166+
return ($this->updateStatus('stopped') && $this->clear());
166167
}
167168

168169
/**
@@ -172,7 +173,7 @@ public function stop()
172173
*/
173174
public function cancel()
174175
{
175-
return $this->updateStatus('cancelled');
176+
return ($this->updateStatus('cancelled') && $this->clear());
176177
}
177178

178179
/**

src/ConversationDB.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Longman\TelegramBot;
1212

1313
use Longman\TelegramBot\DB;
14+
use Longman\TelegramBot\Exception\TelegramException;
1415

1516
/**
1617
* Class ConversationDB
@@ -63,7 +64,7 @@ public static function selectConversation($user_id, $chat_id, $limit = null)
6364

6465
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
6566

66-
} catch (PDOException $e) {
67+
} catch (\Exception $e) {
6768
throw new TelegramException($e->getMessage());
6869
}
6970
return $results;
@@ -106,7 +107,7 @@ public static function insertConversation($user_id, $chat_id, $command)
106107
$sth->bindParam(':date', $created_at);
107108

108109
$status = $sth->execute();
109-
} catch (PDOException $e) {
110+
} catch (\Exception $e) {
110111
throw new TelegramException($e->getMessage());
111112
}
112113
return $status;
@@ -178,7 +179,7 @@ public static function update($table, array $fields_values, array $where_fields_
178179
try {
179180
$sth = self::$pdo->prepare($query);
180181
$status = $sth->execute($tokens);
181-
} catch (PDOException $e) {
182+
} catch (\Exception $e) {
182183
throw new TelegramException($e->getMessage());
183184
}
184185
return $status;

0 commit comments

Comments
 (0)