GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ConversationDB::initializeConversation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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