Completed
Push — master ( 23095d...54d687 )
by Danilo
02:12
created

Updates   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setWebhook() 0 5 1
A getWebhookInfo() 0 5 1
A deleteWebhook() 0 5 1
A getUpdates() 0 11 1
B setUpdateReturned() 0 42 3
1
<?php
2
3
namespace PhpBotFramework\Core;
4
5
trait Updates {
6
7
    /**
8
     * \addtogroup Api Api Methods
9
     * @{
10
     */
11
12
    /**
13
     * \brief Set bot's webhook.
14
     * \details Set a webhook for the current bot in order to receive incoming
15
     * updates via an outgoing webhook.
16
     * @param $params See [Telegram API](https://core.telegram.org/bots/api#setwebhook)
17
     * for more information about the available parameters.
18
     */
19
    public function setWebhook(array $params) {
20
21
        return $this->exec_curl_request('setWebhook?' . http_build_query($params));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() 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...
22
23
    }
24
25
    /**
26
     * \brief Get information about bot's webhook.
27
     * \details Returns an hash which contains information about bot's webhook.
28
     */
29
    public function getWebhookInfo() {
30
31
        return $this->exec_curl_request('getWebhookInfo');
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() 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...
32
33
    }
34
35
    /**
36
     * \brief Delete bot's webhook.
37
     * \details Delete bot's webhook if it exists.
38
     */
39
    public function deleteWebhook() {
40
41
        return $this->exec_curl_request('deleteWebhook');
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() 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...
42
43
    }
44
45
    /**
46
     * \brief Request bot updates.
47
     * \details Request updates received by the bot using method getUpdates of Telegram API. [Api reference](https://core.telegram.org/bots/api#getupdates)
48
     * @param $offset <i>Optional</i>. Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update from the end of the updates queue. All previous updates will forgotten.
49
     * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted.
50
     * @param $timeout <i>Optional</i>. Timeout in seconds for long polling.
51
     * @return Array of updates (can be empty).
52
     */
53
    public function getUpdates(int $offset = 0, int $limit = 100, int $timeout = 60) {
54
55
        $parameters = [
56
            'offset' => $offset,
57
            'limit' => $limit,
58
            'timeout' => $timeout,
59
        ];
60
61
        return $this->exec_curl_request('getUpdates?' . http_build_query($parameters));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() 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...
62
63
    }
64
65
    /**
66
     * \brief Set updates received by the bot for getUpdates handling.
67
     * \details List the types of updates you want your bot to receive. For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. Specify an empty list to receive all updates regardless of type.
68
     * Set it one time and it won't change until next setUpdateReturned call.
69
     * @param $allowed_updates <i>Optional</i>. List of updates allowed.
70
     */
71
    public function setUpdateReturned(array $allowed_updates = []) {
72
73
        // Parameter for getUpdates
74
        $parameters = [
75
            'offset' => 0,
76
            'limit' => 1,
77
            'timeout' => 0,
78
        ];
79
80
        // Start the list
81
        $updates_string = '[';
82
83
        // Flag to skip adding ", " to the string
84
        $first_string = true;
85
86
        // Iterate over the list
87
        foreach ($allowed_updates as $index => $update) {
88
89
            // Is it the first update added?
90
            if (!$first_string) {
91
92
                $updates_string .= ', "' . $update . '"';
93
94
            } else {
95
96
                $updates_string .= '"' . $update . '"';
97
98
                // Set the flag to false cause we added an item
99
                $first_string = false;
100
101
            }
102
103
        }
104
105
        // Close string with the marker
106
        $updates_string .= ']';
107
108
        // Exec getUpdates
109
        $this->exec_curl_request('getUpdates?' . http_build_query($parameters)
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() 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...
110
                                               . '&allowed_updates=' . $updates_string);
111
112
    }
113
114
    /** @} */
115
116
}
117