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

BotanDB   A

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 49
cp 0
rs 10

3 Methods

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