Completed
Push — master ( 52afdb...112a61 )
by Armando
03:05
created

ConversationDB::insertConversation()   B

Complexity

Conditions 3
Paths 11

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
ccs 17
cts 17
cp 1
rs 8.8571
cc 3
eloc 18
nc 11
nop 3
crap 3
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 (!is_null($limit)) {
52 9
                $query .= ' LIMIT :limit';
53
            }
54 9
            $sth = self::$pdo->prepare($query);
55
56 9
            $active = 'active';
57 9
            $sth->bindParam(':status', $active, PDO::PARAM_STR);
58 9
            $sth->bindParam(':user_id', $user_id, PDO::PARAM_INT);
59 9
            $sth->bindParam(':chat_id', $chat_id, PDO::PARAM_INT);
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, :date, :date
93
                )
94 6
               ');
95 6
            $active = 'active';
96
            //$notes = json_encode('');
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97 6
            $notes      = '""';
98 6
            $created_at = self::getTimestamp();
99
100 6
            $sth->bindParam(':status', $active);
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(':date', $created_at);
106
107 6
            $status = $sth->execute();
108 1
        } catch (Exception $e) {
109 1
            throw new TelegramException($e->getMessage());
110
        }
111 5
        return $status;
112
    }
113
114
    /**
115
     * Update a specific conversation
116
     *
117
     * @param array $fields_values
118
     * @param array $where_fields_values
119
     *
120
     * @return bool
121
     */
122 3
    public static function updateConversation(array $fields_values, array $where_fields_values)
123
    {
124 3
        return self::update(TB_CONVERSATION, $fields_values, $where_fields_values);
125
    }
126
127
    /**
128
     * Update the conversation in the database
129
     *
130
     * @param string $table
131
     * @param array  $fields_values
132
     * @param array  $where_fields_values
133
     *
134
     * @todo This function is generic should be moved in DB.php
135
     *
136
     * @return bool
137
     * @throws \Longman\TelegramBot\Exception\TelegramException
138
     */
139 3
    public static function update($table, array $fields_values, array $where_fields_values)
140
    {
141 3
        if (!self::isDbConnected()) {
142
            return false;
143
        }
144
        //Auto update the field update_at
145 3
        $fields_values['updated_at'] = self::getTimestamp();
146
147
        //Values
148 3
        $update         = '';
149 3
        $tokens         = [];
150 3
        $tokens_counter = 0;
151 3
        $a              = 0;
152 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...
153 3
            if ($a) {
154 3
                $update .= ', ';
155
            }
156 3
            ++$a;
157 3
            ++$tokens_counter;
158 3
            $update .= '`' . $field . '` = :' . $tokens_counter;
159 3
            $tokens[':' . $tokens_counter] = $value;
160
        }
161
162
        //Where
163 3
        $a     = 0;
164 3
        $where = '';
165 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...
166 3
            if ($a) {
167 2
                $where .= ' AND ';
168
            } else {
169 3
                ++$a;
170 3
                $where .= 'WHERE ';
171
            }
172 3
            ++$tokens_counter;
173 3
            $where .= '`' . $field . '`= :' . $tokens_counter;
174 3
            $tokens[':' . $tokens_counter] = $value;
175
        }
176
177 3
        $query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
178
        try {
179 3
            $sth    = self::$pdo->prepare($query);
180 3
            $status = $sth->execute($tokens);
181
        } catch (Exception $e) {
182
            throw new TelegramException($e->getMessage());
183
        }
184 3
        return $status;
185
    }
186
}
187