Completed
Push — master ( 5fbbd7...e7cf63 )
by Danilo
03:29
created

User::addUser()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace PhpBotFramework\Database;
4
5
trait User {
6
7
    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...
8
9
    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...
10
11
    /**
12
     * \addtogroup Bot Bot
13
     * @{
14
     */
15
16
    /**
17
     * \addtogroup Users-handle Users handling
18
     * \brief Handle bot users on the database.
19
     * @{
20
     */
21
22
    /** Pdo connection to the database. */
23
    public $pdo;
24
25
    /** \brief Table contaning bot users data in the sql database. */
26
    public $user_table = '"User"';
27
28
    /** \brief Name of the column that represents the user id in the sql database */
29
    public $id_column = 'chat_id';
30
31
    /** \brief Add a user to the database.
32
     * \details Add a user to the database in Bot::$user_table table and Bot::$id_column column using Bot::$pdo connection.
33
     * @param int $chat_id chat_id of the user to add.
34
     * @return bool True on success.
35
     */
36
    public function addUser($chat_id) : bool {
37
38
        // Is there database connection?
39
        if (!isset($this->pdo)) {
40
41
            throw new BotException("Database connection not set");
42
43
        }
44
45
        // Create insertion query and initialize variable
46
        $query = "INSERT INTO $this->user_table ($this->id_column) VALUES (:chat_id)";
47
48
        // Prepare the query
49
        $sth = $this->pdo->prepare($query);
50
51
        // Add the chat_id to the query
52
        $sth->bindParam(':chat_id', $chat_id);
53
54
        try {
55
56
            $sth->execute();
57
            $success = true;
58
59
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class PhpBotFramework\Database\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
60
61
            echo $e->getMessage();
62
63
            $success = false;
64
65
        }
66
67
        // Close statement
68
        $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...
69
70
        // Return result
71
        return $success;
72
73
    }
74
75
    /**
76
     * \brief Broadcast a message to all user registred on the database.
77
     * \details Send a message to all users subscribed, change Bot::$user_table and Bot::$id_column to match your database structure is.
78
     * This method requires Bot::$pdo connection set.
79
     * All parameters are the same as CoreBot::sendMessage.
80
     * Because a limitation of Telegram Bot API the bot will have a delay after 20 messages sent in different chats.
81
     * @see CoreBot::sendMessage
82
     */
83
    public function broadcastMessage(string $text, string $reply_markup = null, string $parse_mode = 'HTML', bool $disable_web_preview = true, bool $disable_notification = false) {
84
85
        // Is there database connection?
86
        if (!isset($this->pdo)) {
87
88
            throw new BotException("Database connection not set");
89
90
        }
91
92
        // Prepare the query to get all chat_id from the database
93
        $sth = $this->pdo->prepare("SELECT $this->id_column FROM $this->user_table");
94
95
        try {
96
97
            $sth->execute();
98
99
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class PhpBotFramework\Database\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
100
101
            echo $e->getMessage();
102
103
        }
104
105
        // Iterate over all the row got
106
        while ($user = $sth->fetch()) {
107
108
            // Call getChat to know that this users haven't blocked the bot
109
            $user_data = $this->getChat($user[$this->id_column]);
110
111
            // Did they block it?
112
            if ($user_data !== false) {
113
114
                // Change the chat_id for the next API method
115
                $this->setChatID($user[$this->id_column]);
116
117
                // Send the message
118
                $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...
119
120
            }
121
122
        }
123
124
        // Close statement
125
        $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...
126
127
    }
128
129
    /** @} */
130
131
    /** @} */
132
133
}
134