Completed
Push — feature/refactor-app-design ( 84f9ff...b18d21 )
by Avtandil
04:09
created

BotanDB::selectShortUrl()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 0
cts 22
cp 0
rs 8.8571
cc 3
eloc 12
nc 7
nop 2
crap 12
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\Extensions\Botan;
12
13
use Exception;
14
use Longman\TelegramBot\DB;
15
use Longman\TelegramBot\Exception\TelegramException;
16
17
/**
18
 * Class BotanDB
19
 */
20
class BotanDB extends DB
21
{
22
    /**
23
     * Initialize 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 string $user_id
37
     *
38
     * @return array|bool
39
     * @throws 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`
50
                FROM `' . TB_BOTAN_SHORTENER . '`
51
                WHERE `user_id` = :user_id
52
                  AND `url` = :url
53
                ORDER BY `created_at` DESC
54
                LIMIT 1
55
            ');
56
57
            $sth->bindValue(':user_id', $user_id);
58
            $sth->bindValue(':url', $url);
59
            $sth->execute();
60
61
            return $sth->fetchColumn();
62
        } catch (Exception $e) {
63
            throw new TelegramException($e->getMessage());
64
        }
65
    }
66
67
    /**
68
     * Insert shortened URL into the database
69
     *
70
     * @param string $url
71
     * @param string $user_id
72
     * @param string $short_url
73
     *
74
     * @return bool
75
     * @throws TelegramException
76
     */
77
    public static function insertShortUrl($url, $user_id, $short_url)
78
    {
79
        if (! self::isDbConnected()) {
80
            return false;
81
        }
82
83
        try {
84
            $sth = self::$pdo->prepare('
85
                INSERT INTO `' . TB_BOTAN_SHORTENER . '`
86
                (`user_id`, `url`, `short_url`, `created_at`)
87
                VALUES
88
                (:user_id, :url, :short_url, :created_at)
89
            ');
90
91
            $sth->bindValue(':user_id', $user_id);
92
            $sth->bindValue(':url', $url);
93
            $sth->bindValue(':short_url', $short_url);
94
            $sth->bindValue(':created_at', self::getTimestamp());
95
96
            return $sth->execute();
97
        } catch (Exception $e) {
98
            throw new TelegramException($e->getMessage());
99
        }
100
    }
101
}
102