BotanDB   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 2
dl 0
loc 82
ccs 0
cts 48
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeBotanDb() 0 6 2
A selectShortUrl() 0 23 3
B insertShortUrl() 0 26 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
/**
18
 * Class BotanDB
19
 */
20
class BotanDB extends DB
21
{
22
    /**
23
     * Initilize botan shortener table
24
     */
25
    public static function initializeBotanDb()
26
    {
27
        if (!defined('TB_BOTAN_SHORTENER')) {
28
            define('TB_BOTAN_SHORTENER', self::$table_prefix . 'botan_shortener');
29
        }
30
    }
31
32
    /**
33
     * Select cached shortened URL from the database
34
     *
35
     * @param string  $url
36
     * @param integer $user_id
37
     *
38
     * @return array|bool
39
     * @throws \Longman\TelegramBot\Exception\TelegramException
40
     */
41
    public static function selectShortUrl($url, $user_id)
42
    {
43
        if (!self::isDbConnected()) {
44
            return false;
45
        }
46
47
        try {
48
            $sth = self::$pdo->prepare('
49
                SELECT `short_url` FROM `' . TB_BOTAN_SHORTENER . '`
50
                WHERE `user_id` = :user_id AND `url` = :url
51
                ORDER BY `created_at` DESC
52
                LIMIT 1
53
            ');
54
55
            $sth->bindParam(':user_id', $user_id, PDO::PARAM_INT);
56
            $sth->bindParam(':url', $url, PDO::PARAM_STR);
57
            $sth->execute();
58
59
            return $sth->fetchColumn();
60
        } catch (Exception $e) {
61
            throw new TelegramException($e->getMessage());
62
        }
63
    }
64
65
    /**
66
     * Insert shortened URL into the database
67
     *
68
     * @param string  $url
69
     * @param integer $user_id
70
     * @param string  $short_url
71
     *
72
     * @return bool
73
     * @throws \Longman\TelegramBot\Exception\TelegramException
74
     */
75
    public static function insertShortUrl($url, $user_id, $short_url)
76
    {
77
        if (!self::isDbConnected()) {
78
            return false;
79
        }
80
81
        try {
82
            $sth = self::$pdo->prepare('
83
                INSERT INTO `' . TB_BOTAN_SHORTENER . '`
84
                (`user_id`, `url`, `short_url`, `created_at`)
85
                VALUES
86
                (:user_id, :url, :short_url, :date)
87
            ');
88
89
            $created_at = self::getTimestamp();
90
91
            $sth->bindParam(':user_id', $user_id, PDO::PARAM_INT);
92
            $sth->bindParam(':url', $url, PDO::PARAM_STR);
93
            $sth->bindParam(':short_url', $short_url, PDO::PARAM_STR);
94
            $sth->bindParam(':date', $created_at, PDO::PARAM_STR);
95
96
            return $sth->execute();
97
        } catch (Exception $e) {
98
            throw new TelegramException($e->getMessage());
99
        }
100
    }
101
}
102