Completed
Push — master ( 7bf2c4...ed6650 )
by Danilo
03:26
created

LongPolling::getUpdateOffsetDatabase()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 2
dl 0
loc 34
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace PhpBotFramework\Database;
4
5
trait LongPolling {
6
7
    abstract public function getUpdates(int $offset = 0, int $limit = 100, int $timeout = 60);
8
9
    abstract protected function initCommands();
10
11
    /**
12
     * \addtogroup Bot
13
     * @{
14
     */
15
16
    /**
17
     * \addtogroup LongPollingDatabase Long polling With Database
18
     * \brief Use getUpdates saving and getting offset in redis/sql-database.
19
     * @{
20
     */
21
22
    /**
23
     * \brief (<i>Internal</i>)Get first update offset in redis.
24
     * \details Called by getUpdatesRedis to get the offset saved in redis or to get it from telegram and save it in redis.
25
     * @param $offset_key Name of the variable where the offset is saved on Redis
26
     * @return Id of the first update to process.
27
     */
28
    protected function getUpdateOffsetRedis(string $offset_key) : int {
29
30
        // If offset is already set in redis
31
        if ($this->redis->exists($offset_key)) {
32
33
            // return the value saved
34
            return $this->redis->get($offset_key);
0 ignored issues
show
Bug introduced by
The property redis 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...
35
36
            // Else get the offset from the id from the first update received
37
        } else {
38
39
            do {
40
41
                $update = $this->getUpdates(0, 1);
42
43
            } while (empty($update));
44
45
            $offset = $update[0]['update_id'];
46
47
            $this->redis->set($offset_key, $offset);
48
49
            return $offset_key;
50
51
        }
52
53
    }
54
55
    /**
56
     * \brief Get updates received by the bot, using redis to save and get the last offset.
57
     * \details It check if an offset exists on redis, then get it, or call getUpdates to set it.
58
     * Then it start an infinite loop where it process updates and update the offset on redis.
59
     * Each update is surrounded by a try/catch.
60
     * @see getUpdates
61
     * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted.
62
     * @param $timeout <i>Optional</i>. Timeout in seconds for long polling.
63
     * @param $offset_key <i>Optional</i>. Name of the variable where the offset is saved on Redis
64
     */
65
    public function getUpdatesRedis(int $limit = 100, int $timeout = 60, string $offset_key = 'offset') {
0 ignored issues
show
Unused Code introduced by
The parameter $offset_key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
67
        // Check redis connection
68
        if (!isset($this->redis)) {
69
70
            throw new BotException("Redis connection is not set");
71
72
        }
73
74
        $offset = $this->getUpdateOffsetRedis();
0 ignored issues
show
Bug introduced by
The call to getUpdateOffsetRedis() misses a required argument $offset_key.

This check looks for function calls that miss required arguments.

Loading history...
75
76
        $this->initCommands();
77
78
        // Prepare the query for updating the offset in the database
79
        $sth = $this->pdo->prepare('UPDATE "' . $table_name . '" SET "' . $column_name . '" = :new_offset');
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...
Bug introduced by
The variable $table_name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $column_name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
80
81
        while (true) {
82
83
            $updates = $this->getUpdates($offset, $limit, $timeout);
84
85
            foreach ($updates as $key => $update) {
86
87
                try {
88
89
                    $this->processUpdate($update);
0 ignored issues
show
Bug introduced by
It seems like processUpdate() 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...
90
91
                } catch (BotException $e) {
0 ignored issues
show
Bug introduced by
The class PhpBotFramework\Database\BotException 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...
92
93
                    echo $e->getMessage();
94
95
                }
96
97
            }
98
99
            // Update the offset on the database
100
            $sth->bindParam(':new_offset', $offset + sizeof($updates));
101
            $sth->execute();
102
103
        }
104
105
    }
106
107
    /** @} */
108
109
    /** @} */
110
111
}
112