Skip to content

Correct typos and minor code styling. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions src/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Conversation
protected $user_id;

/**
* Telegram chat_id
* Telegram chat id
*
* @var int
*/
Expand All @@ -59,23 +59,23 @@ class Conversation
* Group name let you share the session among commands
* Call this as the same name of the command if you don't need to share the conversation
*
* @var strint
* @var string
*/
protected $group_name;

/**
* Command to be execute if the conversation is active
* Command to be executed if the conversation is active
*
* @var string
*/
protected $command;

/**
* Conversation contructor initialize a new conversation
* Conversation contructor to initialize a new conversation
*
* @param int $user_id
* @param int $chat_id
* @param string $name
* @param string $group_name
* @param string $command
*/
public function __construct($user_id, $chat_id, $group_name = null, $command = null)
Expand All @@ -91,7 +91,7 @@ public function __construct($user_id, $chat_id, $group_name = null, $command = n
}

/**
* Check if the conversation already exist
* Check if the conversation already exists
*
* @return bool
*/
Expand All @@ -101,7 +101,7 @@ protected function conversationExist()
if ($this->is_fetched) {
return true;
}
//select an active conversation
//Select an active conversation
$conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
$this->is_fetched = true;

Expand All @@ -114,13 +114,13 @@ protected function conversationExist()
return true;
}

//a track with the same name was already opened, store the data inside the class
//A conversation with the same name was already opened, store the data inside the class
if ($this->conversation['conversation_name'] == $this->group_name) {
$this->data = json_decode($this->conversation['data'], true);
return true;
}

//a conversation with a differet name has been opened unsetting the DB one and reacreatea a new one
//A conversation with a different name has been opened, unset the DB one and recreate a new one
ConversationDB::updateConversation(['status' => 'cancelled'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
return false;
}
Expand All @@ -130,9 +130,7 @@ protected function conversationExist()
}

/**
* Check if the Conversation already exist
*
* Check if a conversation has already been created in the database. If the conversation is not found, a new conversation is created. start fetch the data stored in the database
* Check if a conversation has already been created in the database. If the conversation is not found, a new conversation is created. Start fetches the data stored in the database.
*
* @return bool
*/
Expand All @@ -148,11 +146,13 @@ public function start()
/**
* Store the array/variable in the database with json_encode() function
*
* @todo Verify the query before assigning the $data member variable
*
* @param array $data
*/
public function update($data)
{
//conversation must exist!
//Conversation must exist!
if ($this->conversationExist()) {
$fields['data'] = json_encode($data);

Expand All @@ -165,11 +165,9 @@ public function update($data)
/**
* Delete the conversation from the database
*
* Currently the Convevrsation is not deleted but just unsetted
* Currently the Conversation is not deleted but just set to 'stopped'
*
* @TODO should return something
*
* @param array $data
* @todo should return something
*/
public function stop()
{
Expand All @@ -181,7 +179,7 @@ public function stop()
/**
* Retrieve the command to execute from the conversation
*
* @param string
* @return string|null
*/
public function getConversationCommand()
{
Expand All @@ -192,9 +190,9 @@ public function getConversationCommand()
}

/**
* Retrive the data store in the conversation
* Retrieve the data stored in the conversation
*
* @param array $data
* @return array
*/
public function getData()
{
Expand Down
38 changes: 19 additions & 19 deletions src/ConversationDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@ public static function initializeConversation()
define('TB_CONVERSATION', self::$table_prefix . 'conversation');
}
}

/**
* Conversation contructor initialize a new conversation
* Select a conversation from the DB
*
* @param int $user_id
* @param int $chat_id
* @param bool $limit
*
* @return array
* @return array|bool
*/
public static function selectConversation($user_id, $chat_id, $limit = null)
{
if (!self::isDbConnected()) {
return false;
}

try {
$query = 'SELECT * FROM `' . TB_CONVERSATION . '` ';
$query .= 'WHERE `status` = :status ';
$query .= 'AND `chat_id` = :chat_id ';
$query .= 'AND `user_id` = :user_id ';

$tokens = [':chat_id' => $chat_id, ':user_id' => $user_id];
if (!is_null($limit)) {
$query .=' LIMIT :limit';
$query .= ' LIMIT :limit';
}
$sth = self::$pdo->prepare($query);

Expand All @@ -73,7 +73,7 @@ public static function selectConversation($user_id, $chat_id, $limit = null)
* Insert the conversation in the database
*
* @param string $conversation_command
* @param string $conversation_name
* @param string $conversation_group_name
* @param int $user_id
* @param int $chat_id
*
Expand Down Expand Up @@ -127,13 +127,13 @@ public static function updateConversation(array $fields_values, array $where_fie
}

/**
* Insert the conversation in the database
* Update the conversation in the database
*
* @param string $table
* @param array $fields_values
* @param array $where_fields_values
* @param string $table
* @param array $fields_values
* @param array $where_fields_values
*
* @todo this function is generic should be moved in DB.php
* @todo This function is generic should be moved in DB.php
*
* @return bool
*/
Expand All @@ -156,26 +156,26 @@ public static function update($table, array $fields_values, array $where_fields_
}
++$a;
++$tokens_counter;
$update .= '`'.$field.'` = :'.$tokens_counter;
$tokens[':'.$tokens_counter] = $value;
$update .= '`' . $field . '` = :' . $tokens_counter;
$tokens[':' . $tokens_counter] = $value;
}

//Where
$a = 0;
$where = '';
foreach ($where_fields_values as $field => $value) {
if ($a) {
$where .= ' AND ';
$where .= ' AND ';
} else {
++$a;
$where .= 'WHERE ';
$where .= 'WHERE ';
}
++$tokens_counter;
$where .= '`'.$field .'`= :'.$tokens_counter ;
$tokens[':'.$tokens_counter] = $value;
$where .= '`' . $field .'`= :' . $tokens_counter ;
$tokens[':' . $tokens_counter] = $value;
}

$query = 'UPDATE `'.$table.'` SET '.$update.' '.$where;
$query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
try {
$sth = self::$pdo->prepare($query);
$status = $sth->execute($tokens);
Expand Down