Completed
Push — master ( 3845e2...a95f7d )
by Danilo
09:37
created

User::addUser()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 14
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Database;
20
21
use PhpBotFramework\Exceptions\BotException;
22
23
/**
24
 * \addtogroup Modules
25
 * @{
26
 */
27
28
/** \class User
29
 */
30
trait User
31
{
32
    /** @} */
33
34
    abstract public function getChat($chat_id);
35
36
    abstract public function setChatID($chat_id);
37
38
    abstract protected function sanitizeUserTable();
39
40
    /** PDO connection to the database. */
41
    public $pdo;
42
43
    /**
44
     * \addtogroup Bot Bot
45
     * @{
46
     */
47
48
    /**
49
     * \addtogroup Users-handle Users handling
50
     * \brief Handle bot users on the database.
51
     * @{
52
     */
53
54
    /** \brief Table contaning bot users data in the SQL database. */
55
    public $user_table = 'User';
56
57
    /** \brief Name of the column that represents the user id in the sql database */
58
    public $id_column = 'chat_id';
59
60
    /** \brief Add a user to the database.
61
     * \details Add a user to the database in Bot::$user_table table and Bot::$id_column column using Bot::$pdo connection.
62
     * @param string|int $chat_id chat ID of the user to add.
63
     * @return bool True on success.
64
     */
65
    public function addUser($chat_id) : bool
66
    {
67
        if (!isset($this->pdo)) {
68
            throw new BotException("Database connection not set");
69
        }
70
71
        $this->sanitizeUserTable();
72
73
        // Create insertion query and initialize variable
74
        $query = "INSERT INTO $this->user_table ($this->id_column) VALUES (:chat_id)";
75
76
        $sth = $this->pdo->prepare($query);
77
        $sth->bindParam(':chat_id', $chat_id);
78
79
        try {
80
            $sth->execute();
81
            $success = true;
82
        } catch (\PDOException $e) {
83
            echo $e->getMessage();
84
85
            $success = false;
86
        }
87
88
        $sth = null;
0 ignored issues
show
Unused Code introduced by
$sth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
        return $success;
90
    }
91
92
    /**
93
     * \brief Send a message to every user available on the database.
94
     * \details Send a message to all subscribed users, change Bot::$user_table and Bot::$id_column to match your database structure.
95
     * This method requires Bot::$pdo connection set.
96
     * All parameters are the same as CoreBot::sendMessage.
97
     * Because a limitation of Telegram Bot API the bot will have a delay after 20 messages sent in different chats.
98
     * @return int How many messages were sent.
99
     * @see CoreBot::sendMessage
100
     */
101
    public function broadcastMessage(
102
        string $text,
103
        string $reply_markup = null,
104
        string $parse_mode = 'HTML',
105
        bool $disable_web_preview = true,
106
        bool $disable_notification = false
107
    ) : int {
108
        if (!isset($this->pdo)) {
109
            throw new BotException("Database connection not set");
110
        }
111
112
        $this->sanitizeUserTable();
113
114
        $sth = $this->pdo->prepare("SELECT $this->id_column FROM $this->user_table");
115
116
        try {
117
            $sth->execute();
118
        } catch (\PDOException $e) {
119
            echo $e->getMessage();
120
        }
121
122
        // Iterate over all the row got
123
        while ($user = $sth->fetch()) {
124
            $user_data = $this->getChat($user[$this->id_column]);
125
126
            if ($user_data !== false) {
127
                // Change the chat_id for the next API method
128
                $this->setChatID($user[$this->id_column]);
129
                $this->sendMessage($text, $reply_markup, null, $parse_mode, $disable_web_preview, $disable_notification);
0 ignored issues
show
Bug introduced by
It seems like sendMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
130
            }
131
        }
132
133
        return $sth->rowCount();
134
    }
135
136
    /** @} */
137
138
    /** @} */
139
}
140