Update   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 37
dl 0
loc 66
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUpdateType() 0 9 3
A getUpdateTypes() 0 3 1
A subEntities() 0 18 1
1
<?php
2
3
4
namespace TelegramBot\Entities;
5
6
use TelegramBot\Entities\Payments\PreCheckoutQuery;
7
use TelegramBot\Entities\Payments\ShippingQuery;
8
use TelegramBot\Entity;
9
10
/**
11
 * Class Update
12
 *
13
 * @link https://core.telegram.org/bots/api#update
14
 *
15
 * @method int                getUpdateId()             The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order.
16
 * @method Message            getMessage()              Optional. New incoming message of any kind — text, photo, sticker, etc.
17
 * @method Message            getEditedMessage()        Optional. New version of a message that is known to the bot and was edited
18
 * @method Message            getChannelPost()          Optional. New post in the channel, can be any kind — text, photo, sticker, etc.
19
 * @method Message            getEditedChannelPost()    Optional. New version of a post in the channel that is known to the bot and was edited
20
 * @method InlineQuery        getInlineQuery()          Optional. New incoming inline query
21
 * @method ChosenInlineResult getChosenInlineResult()   Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
22
 * @method CallbackQuery      getCallbackQuery()        Optional. New incoming callback query
23
 * @method ShippingQuery      getShippingQuery()        Optional. New incoming shipping query. Only for invoices with flexible price
24
 * @method PreCheckoutQuery   getPreCheckoutQuery()     Optional. New incoming pre-checkout query. Contains full information about checkout
25
 * @method Poll               getPoll()                 Optional. New poll state. Bots receive only updates about polls, which are sent or stopped by the bot
26
 * @method PollAnswer         getPollAnswer()           Optional. A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.
27
 * @method ChatMemberUpdated  getMyChatMember()         Optional. The bot's chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user.
28
 * @method ChatMemberUpdated  getChatMember()           Optional. A chat member's status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates.
29
 * @method ChatJoinRequest    getChatJoinRequest()      Optional. A request to join the chat has been sent. The bot must have the can_invite_users administrator right in the chat to receive these updates.
30
 * @method WebAppData         getWebAppData()           Optional. The data from WebApp
31
 */
32
class Update extends Entity
33
{
34
35
    public const TYPE_MESSAGE = 'message';
36
    public const TYPE_EDITED_MESSAGE = 'edited_message';
37
    public const TYPE_CHANNEL_POST = 'channel_post';
38
    public const TYPE_EDITED_CHANNEL_POST = 'edited_channel_post';
39
    public const TYPE_INLINE_QUERY = 'inline_query';
40
    public const TYPE_CHOSEN_INLINE_RESULT = 'chosen_inline_result';
41
    public const TYPE_CALLBACK_QUERY = 'callback_query';
42
    public const TYPE_SHIPPING_QUERY = 'shipping_query';
43
    public const TYPE_PRE_CHECKOUT_QUERY = 'pre_checkout_query';
44
    public const TYPE_POLL = 'poll';
45
    public const TYPE_POLL_ANSWER = 'poll_answer';
46
    public const TYPE_MY_CHAT_MEMBER = 'my_chat_member';
47
    public const TYPE_CHAT_MEMBER = 'chat_member';
48
    public const TYPE_CHAT_JOIN_REQUEST = 'chat_join_request';
49
    public const TYPE_WEB_DATA = 'web_app_data';
50
51
    /**
52
     * Get the update type based on the set properties
53
     *
54
     * @return string|null
55
     */
56
    public function getUpdateType(): ?string
57
    {
58
        foreach (self::getUpdateTypes() as $type) {
59
            if ($this->getProperty($type)) {
60
                return $type;
61
            }
62
        }
63
64
        return null;
65
    }
66
67
    /**
68
     * Get the list of all available update types
69
     *
70
     * @return string[]
71
     */
72
    public static function getUpdateTypes(): array
73
    {
74
        return array_keys((new self([]))->subEntities());
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    protected function subEntities(): array
81
    {
82
        return [
83
            self::TYPE_MESSAGE => Message::class,
84
            self::TYPE_EDITED_MESSAGE => EditedMessage::class,
85
            self::TYPE_CHANNEL_POST => ChannelPost::class,
86
            self::TYPE_EDITED_CHANNEL_POST => EditedChannelPost::class,
87
            self::TYPE_INLINE_QUERY => InlineQuery::class,
88
            self::TYPE_CHOSEN_INLINE_RESULT => ChosenInlineResult::class,
89
            self::TYPE_CALLBACK_QUERY => CallbackQuery::class,
90
            self::TYPE_SHIPPING_QUERY => ShippingQuery::class,
91
            self::TYPE_PRE_CHECKOUT_QUERY => PreCheckoutQuery::class,
92
            self::TYPE_POLL => Poll::class,
93
            self::TYPE_POLL_ANSWER => PollAnswer::class,
94
            self::TYPE_MY_CHAT_MEMBER => ChatMemberUpdated::class,
95
            self::TYPE_CHAT_MEMBER => ChatMemberUpdated::class,
96
            self::TYPE_CHAT_JOIN_REQUEST => ChatJoinRequest::class,
97
            self::TYPE_WEB_DATA => WebAppData::class,
98
        ];
99
    }
100
101
}
102