Completed
Push — master ( 8fbc07...04de59 )
by Armando
03:05 queued 01:27
created

ConversationDB::updateConversation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
use Exception;
14
use Longman\TelegramBot\Exception\TelegramException;
15
use PDO;
16
17
class ConversationDB extends DB
18
{
19
    /**
20
     * Initialize conversation table
21
     */
22 9
    public static function initializeConversation()
23
    {
24 9
        if (!defined('TB_CONVERSATION')) {
25 1
            define('TB_CONVERSATION', self::$table_prefix . 'conversation');
26
        }
27 9
    }
28
29
    /**
30
     * Select a conversation from the DB
31
     *
32
     * @param string   $user_id
33
     * @param string   $chat_id
34
     * @param int|null $limit
35
     *
36
     * @return array|bool
37
     * @throws TelegramException
38
     */
39 9
    public static function selectConversation($user_id, $chat_id, $limit = null)
40
    {
41 9
        if (!self::isDbConnected()) {
42
            return false;
43
        }
44
45
        try {
46
            $sql = '
47
              SELECT *
48 9
              FROM `' . TB_CONVERSATION . '`
49
              WHERE `status` = :status
50
                AND `chat_id` = :chat_id
51
                AND `user_id` = :user_id
52
            ';
53
54 9
            if ($limit !== null) {
55 9
                $sql .= ' LIMIT :limit';
56
            }
57
58 9
            $sth = self::$pdo->prepare($sql);
59
60 9
            $sth->bindValue(':status', 'active');
61 9
            $sth->bindValue(':user_id', $user_id);
62 9
            $sth->bindValue(':chat_id', $chat_id);
63
64 9
            if ($limit !== null) {
65 9
                $sth->bindValue(':limit', $limit, PDO::PARAM_INT);
66
            }
67
68 9
            $sth->execute();
69
70 9
            return $sth->fetchAll(PDO::FETCH_ASSOC);
71
        } catch (Exception $e) {
72
            throw new TelegramException($e->getMessage());
73
        }
74
    }
75
76
    /**
77
     * Insert the conversation in the database
78
     *
79
     * @param string $user_id
80
     * @param string $chat_id
81
     * @param string $command
82
     *
83
     * @return bool
84
     * @throws TelegramException
85
     */
86 6
    public static function insertConversation($user_id, $chat_id, $command)
87
    {
88 6
        if (!self::isDbConnected()) {
89
            return false;
90
        }
91
92
        try {
93 6
            $sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
94
                (`status`, `user_id`, `chat_id`, `command`, `notes`, `created_at`, `updated_at`)
95
                VALUES
96
                (:status, :user_id, :chat_id, :command, :notes, :created_at, :updated_at)
97
            ');
98
99 6
            $date = self::getTimestamp();
100
101 6
            $sth->bindValue(':status', 'active');
102 6
            $sth->bindValue(':command', $command);
103 6
            $sth->bindValue(':user_id', $user_id);
104 6
            $sth->bindValue(':chat_id', $chat_id);
105 6
            $sth->bindValue(':notes', '[]');
106 6
            $sth->bindValue(':created_at', $date);
107 6
            $sth->bindValue(':updated_at', $date);
108
109 6
            return $sth->execute();
110 1
        } catch (Exception $e) {
111 1
            throw new TelegramException($e->getMessage());
112
        }
113
    }
114
115
    /**
116
     * Update a specific conversation
117
     *
118
     * @param array $fields_values
119
     * @param array $where_fields_values
120
     *
121
     * @return bool
122
     * @throws TelegramException
123
     */
124 3
    public static function updateConversation(array $fields_values, array $where_fields_values)
125
    {
126
        // Auto update the update_at field.
127 3
        $fields_values['updated_at'] = self::getTimestamp();
128
129 3
        return self::update(TB_CONVERSATION, $fields_values, $where_fields_values);
130
    }
131
}
132