Completed
Push — master ( 87c85d...4ed6b9 )
by Danilo
03:41
created

User::addUser()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 13
cp 0
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 14
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 function getChat($chat_id);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
36
    abstract function setChatID($chat_id);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
38
    /** PDO connection to the database. */
39
    public $pdo;
40
41
    /**
42
     * \addtogroup Bot Bot
43
     * @{
44
     */
45
46
    /**
47
     * \addtogroup Users-handle Users handling
48
     * \brief Handle bot users on the database.
49
     * @{
50
     */
51
52
    /** \brief Table contaning bot users data in the SQL database. */
53
    public $user_table = '"User"';
54
55
    /** \brief Name of the column that represents the user id in the sql database */
56
    public $id_column = 'chat_id';
57
58
    /** \brief Add a user to the database.
59
     * \details Add a user to the database in Bot::$user_table table and Bot::$id_column column using Bot::$pdo connection.
60
     * @param string|int $chat_id chat ID of the user to add.
61
     * @return bool True on success.
62
     */
63
    public function addUser($chat_id) : bool
64
    {
65
        if (!isset($this->pdo)) {
66
            throw new BotException("Database connection not set");
67
        }
68
69
        // Create insertion query and initialize variable
70
        $query = "INSERT INTO $this->user_table ($this->id_column) VALUES (:chat_id)";
71
72
        $sth = $this->pdo->prepare($query);
73
        $sth->bindParam(':chat_id', $chat_id);
74
75
        try {
76
            $sth->execute();
77
            $success = true;
78
        } catch (\PDOException $e) {
79
            echo $e->getMessage();
80
81
            $success = false;
82
        }
83
84
        $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...
85
        return $success;
86
    }
87
88
    /**
89
     * \brief Send a message to every user available on the database.
90
     * \details Send a message to all subscribed users, change Bot::$user_table and Bot::$id_column to match your database structure.
91
     * This method requires Bot::$pdo connection set.
92
     * All parameters are the same as CoreBot::sendMessage.
93
     * Because a limitation of Telegram Bot API the bot will have a delay after 20 messages sent in different chats.
94
     * @return int How many messages were sent.
95
     * @see CoreBot::sendMessage
96
     */
97 1
    public function broadcastMessage(
98
        string $text,
99
        string $reply_markup = null,
100
        string $parse_mode = 'HTML',
101
        bool $disable_web_preview = true,
102
        bool $disable_notification = false
103
    ) : int {
104 1
        if (!isset($this->pdo)) {
105
            throw new BotException("Database connection not set");
106
        }
107
108 1
        $sth = $this->pdo->prepare("SELECT $this->id_column FROM $this->user_table");
109
110
        try {
111 1
            $sth->execute();
112 1
        } catch (\PDOException $e) {
113 1
            echo $e->getMessage();
114
        }
115
116
        // Iterate over all the row got
117 1
        while ($user = $sth->fetch()) {
118
            $user_data = $this->getChat($user[$this->id_column]);
119
120
            if ($user_data !== false) {
121
                // Change the chat_id for the next API method
122
                $this->setChatID($user[$this->id_column]);
123
                $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...
124
            }
125
        }
126
127 1
        return $sth->rowCount();
128
    }
129
130
    /** @} */
131
132
    /** @} */
133
}
134