Completed
Pull Request — develop (#365)
by
unknown
02:35
created

BotanDB::selectShortUrl()   B

Complexity

Conditions 4
Paths 9

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 0
cts 23
cp 0
rs 8.5806
cc 4
eloc 14
nc 9
nop 2
crap 20
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  $user_id
36
     * @param  $url
37
     *
38
     * @return array|bool
39
     * @throws \Longman\TelegramBot\Exception\TelegramException
40
     */
41
    public static function selectShortUrl($user_id, $url)
42
    {
43
        if (!self::isDbConnected()) {
44
            return false;
45
        }
46
47
        try {
48
            $sth = self::$pdo->prepare('SELECT * FROM `' . TB_BOTAN_SHORTENER . '`
49
                WHERE `user_id` = :user_id AND `url` = :url
50
                ORDER BY `created_at` DESC
51
                LIMIT 1
52
            ');
53
54
            $sth->bindParam(':user_id', $user_id, PDO::PARAM_INT);
55
            $sth->bindParam(':url', $url, PDO::PARAM_STR);
56
            $sth->execute();
57
58
            $result = $sth->fetchAll(PDO::FETCH_ASSOC);
59
60
            if (isset($result[0]['short_url'])) {
61
                return $result[0]['short_url'];
62
            }
63
64
            return $result;
65
        } catch (Exception $e) {
66
            throw new TelegramException($e->getMessage());
67
        }
68
    }
69
70
    /**
71
     * Insert shortened URL into the database
72
     *
73
     * @param $user_id
74
     * @param $url
75
     * @param $short_url
76
     *
77
     * @return bool
78
     * @throws \Longman\TelegramBot\Exception\TelegramException
79
     */
80
    public static function insertShortUrl($user_id, $url, $short_url)
81
    {
82
        if (!self::isDbConnected()) {
83
            return false;
84
        }
85
86
        try {
87
            $sth = self::$pdo->prepare('INSERT INTO `' . TB_BOTAN_SHORTENER . '`
88
                (
89
                `user_id`, `url`, `short_url`, `created_at`
90
                )
91
                VALUES (
92
                :user_id, :url, :short_url, :date
93
                )
94
            ');
95
96
            $created_at = self::getTimestamp();
97
98
            $sth->bindParam(':user_id', $user_id);
99
            $sth->bindParam(':url', $url);
100
            $sth->bindParam(':short_url', $short_url);
101
            $sth->bindParam(':date', $created_at);
102
103
            return $sth->execute();
104
        } catch (Exception $e) {
105
            throw new TelegramException($e->getMessage());
106
        }
107
    }
108
}
109