Completed
Push — master ( e7303e...8fbc07 )
by Armando
01:58
created

ConversationDB::update()   C

Complexity

Conditions 7
Paths 28

Size

Total Lines 47
Code Lines 33

Duplication

Lines 20
Ratio 42.55 %

Code Coverage

Tests 28
CRAP Score 7.0444

Importance

Changes 0
Metric Value
dl 20
loc 47
ccs 28
cts 31
cp 0.9032
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 33
nc 28
nop 3
crap 7.0444
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
     * Initilize 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 int  $user_id
33
     * @param int  $chat_id
34
     * @param bool $limit
35
     *
36
     * @return array|bool
37
     * @throws \Longman\TelegramBot\Exception\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 9
            $query = 'SELECT * FROM `' . TB_CONVERSATION . '` ';
47 9
            $query .= 'WHERE `status` = :status ';
48 9
            $query .= 'AND `chat_id` = :chat_id ';
49 9
            $query .= 'AND `user_id` = :user_id ';
50
51 9
            if ($limit !== null) {
52 9
                $query .= ' LIMIT :limit';
53
            }
54 9
            $sth = self::$pdo->prepare($query);
55
56 9
            $status = 'active';
57 9
            $sth->bindParam(':status', $status);
58 9
            $sth->bindParam(':user_id', $user_id);
59 9
            $sth->bindParam(':chat_id', $chat_id);
60 9
            $sth->bindParam(':limit', $limit, PDO::PARAM_INT);
61 9
            $sth->execute();
62
63 9
            $results = $sth->fetchAll(PDO::FETCH_ASSOC);
64
        } catch (Exception $e) {
65
            throw new TelegramException($e->getMessage());
66
        }
67 9
        return $results;
68
    }
69
70
    /**
71
     * Insert the conversation in the database
72
     *
73
     * @param int    $user_id
74
     * @param int    $chat_id
75
     * @param string $command
76
     *
77
     * @return bool
78
     * @throws \Longman\TelegramBot\Exception\TelegramException
79
     */
80 6
    public static function insertConversation($user_id, $chat_id, $command)
81
    {
82 6
        if (!self::isDbConnected()) {
83
            return false;
84
        }
85
86
        try {
87 6
            $sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
88
                (
89
                `status`, `user_id`, `chat_id`, `command`, `notes`, `created_at`, `updated_at`
90
                )
91
                VALUES (
92
                :status, :user_id, :chat_id, :command, :notes, :created_at, :updated_at
93
                )
94
            ');
95
96 6
            $status = 'active';
97 6
            $notes  = '[]';
98 6
            $date   = self::getTimestamp();
99
100 6
            $sth->bindParam(':status', $status);
101 6
            $sth->bindParam(':command', $command);
102 6
            $sth->bindParam(':user_id', $user_id);
103 6
            $sth->bindParam(':chat_id', $chat_id);
104 6
            $sth->bindParam(':notes', $notes);
105 6
            $sth->bindParam(':created_at', $date);
106 6
            $sth->bindParam(':updated_at', $date);
107
108 6
            $status = $sth->execute();
109 1
        } catch (Exception $e) {
110 1
            throw new TelegramException($e->getMessage());
111
        }
112 5
        return $status;
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
     */
123 3
    public static function updateConversation(array $fields_values, array $where_fields_values)
124
    {
125 3
        return self::update(TB_CONVERSATION, $fields_values, $where_fields_values);
126
    }
127
128
    /**
129
     * Update the conversation in the database
130
     *
131
     * @param string $table
132
     * @param array  $fields_values
133
     * @param array  $where_fields_values
134
     *
135
     * @todo This function is generic should be moved in DB.php
136
     *
137
     * @return bool
138
     * @throws \Longman\TelegramBot\Exception\TelegramException
139
     */
140 3
    public static function update($table, array $fields_values, array $where_fields_values)
141
    {
142 3
        if (!self::isDbConnected()) {
143
            return false;
144
        }
145
        //Auto update the field update_at
146 3
        $fields_values['updated_at'] = self::getTimestamp();
147
148
        //Values
149 3
        $update         = '';
150 3
        $tokens         = [];
151 3
        $tokens_counter = 0;
152 3
        $a              = 0;
153 3 View Code Duplication
        foreach ($fields_values as $field => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154 3
            if ($a) {
155 3
                $update .= ', ';
156
            }
157 3
            ++$a;
158 3
            ++$tokens_counter;
159 3
            $update .= '`' . $field . '` = :' . $tokens_counter;
160 3
            $tokens[':' . $tokens_counter] = $value;
161
        }
162
163
        //Where
164 3
        $a     = 0;
165 3
        $where = '';
166 3 View Code Duplication
        foreach ($where_fields_values as $field => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167 3
            if ($a) {
168 2
                $where .= ' AND ';
169
            } else {
170 3
                ++$a;
171 3
                $where .= 'WHERE ';
172
            }
173 3
            ++$tokens_counter;
174 3
            $where .= '`' . $field . '`= :' . $tokens_counter;
175 3
            $tokens[':' . $tokens_counter] = $value;
176
        }
177
178 3
        $query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
179
        try {
180 3
            $sth    = self::$pdo->prepare($query);
181 3
            $status = $sth->execute($tokens);
182
        } catch (Exception $e) {
183
            throw new TelegramException($e->getMessage());
184
        }
185 3
        return $status;
186
    }
187
}
188