Completed
Pull Request — master (#20)
by
unknown
03:12
created

DatabaseHandler::addUser()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace PhpBotFramework\Utilities;
4
5
trait DatabaseHandler {
6
7
    /**
8
     * \addtogroup Users-handle Users handling
9
     * \brief Handle bot users on the database.
10
     * @{
11
     */
12
13
    /** \brief Table contaning bot users data in the sql database. */
14
    public $user_table = '"User"';
15
16
    /** \brief Name of the column that represents the user id in the sql database */
17
    public $id_column = 'chat_id';
18
19
    /** \brief Add a user to the database.
20
     * \details Add a user to the database in Bot::$user_table table and Bot::$id_column column using Bot::$pdo connection.
21
     * @param $chat_id chat_id of the user to add.
22
     * @return True on success.
23
     */
24
    public function addUser($chat_id) : bool {
25
26
        // Is there database connection?
27
        if (!isset($this->pdo)) {
28
29
            throw new BotException("Database connection not set");
30
31
        }
32
33
        // Create insertion query and initialize variable
34
        $query = "INSERT INTO $this->user_table ($this->id_column) VALUES (:chat_id)";
35
36
        // Prepare the query
37
        $sth = $this->pdo->prepare($query);
0 ignored issues
show
Bug introduced by
The property pdo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
39
        // Add the chat_id to the query
40
        $sth->bindParam(':chat_id', $chat_id);
41
42
        try {
43
44
            $sth->execute();
45
            $success = true;
46
47
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class PhpBotFramework\Utilities\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...
48
49
            echo $e->getMessage();
50
51
            $success = false;
52
53
        }
54
55
        // Close statement
56
        $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...
57
58
        // Return result
59
        return $success;
60
61
    }
62
63
    /**
64
     * \brief Broadcast a message to all user registred on the database.
65
     * \details Send a message to all users subscribed, change Bot::$user_table and Bot::$id_column to match your database structure is.
66
     * This method requires Bot::$pdo connection set.
67
     * All parameters are the same as CoreBot::sendMessage.
68
     * Because a limitation of Telegram Bot API the bot will have a delay after 20 messages sent in different chats.
69
     * @see CoreBot::sendMessage
70
     */
71
    public function broadcastMessage($text, string $reply_markup = null, string $parse_mode = 'HTML', bool $disable_web_preview = true, bool $disable_notification = false) {
72
73
        // Is there database connection?
74
        if (!isset($this->pdo)) {
75
76
            throw new BotException("Database connection not set");
77
78
        }
79
80
        // Prepare the query to get all chat_id from the database
81
        $sth = $this->pdo->prepare("SELECT $this->id_column FROM $this->user_table");
82
83
        try {
84
85
            $sth->execute();
86
87
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class PhpBotFramework\Utilities\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...
88
89
            echo $e->getMessage();
90
91
        }
92
93
        // Iterate over all the row got
94
        while ($user = $sth->fetch()) {
95
96
            // Call getChat to know that this users haven't blocked the bot
97
            $user_data = $this->getChat($user[$this->id_column]);
0 ignored issues
show
Bug introduced by
It seems like getChat() 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...
98
99
            // Did they block it?
100
            if ($user_data !== false) {
101
102
                // Change the chat_id for the next API method
103
                $this->setChatID($user[$this->id_column]);
0 ignored issues
show
Bug introduced by
It seems like setChatID() 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...
104
105
                // Send the message
106
                $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...
107
108
            }
109
110
        }
111
112
        // Close statement
113
        $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...
114
115
    }
116
117
    /** @} */
118
119
}
120