Passed
Push — master ( 9ca615...b6ea35 )
by Armando
02:47
created

Update::getUpdateContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the TelegramBot package.
5
 *
6
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Longman\TelegramBot\Entities;
13
14
use Longman\TelegramBot\Entities\Payments\PreCheckoutQuery;
15
use Longman\TelegramBot\Entities\Payments\ShippingQuery;
16
17
/**
18
 * Class Update
19
 *
20
 * @link https://core.telegram.org/bots/api#update
21
 *
22
 * @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.
23
 * @method Message             getMessage()            Optional. New incoming message of any kind — text, photo, sticker, etc.
24
 * @method Message             getEditedMessage()      Optional. New version of a message that is known to the bot and was edited
25
 * @method Message             getChannelPost()        Optional. New post in the channel, can be any kind — text, photo, sticker, etc.
26
 * @method Message             getEditedChannelPost()  Optional. New version of a post in the channel that is known to the bot and was edited
27
 * @method InlineQuery         getInlineQuery()        Optional. New incoming inline query
28
 * @method ChosenInlineResult  getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
29
 * @method CallbackQuery       getCallbackQuery()      Optional. New incoming callback query
30
 * @method ShippingQuery       getShippingQuery()      Optional. New incoming shipping query. Only for invoices with flexible price
31
 * @method PreCheckoutQuery    getPreCheckoutQuery()   Optional. New incoming pre-checkout query. Contains full information about checkout
32
 * @method Poll                getPoll()               Optional. New poll state. Bots receive only updates about polls, which are sent or stopped by the bot
33
 * @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.
34
 * @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.
35
 * @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.
36
 */
37
class Update extends Entity
38
{
39
    public const TYPE_MESSAGE              = 'message';
40
    public const TYPE_EDITED_MESSAGE       = 'edited_message';
41
    public const TYPE_CHANNEL_POST         = 'channel_post';
42
    public const TYPE_EDITED_CHANNEL_POST  = 'edited_channel_post';
43
    public const TYPE_INLINE_QUERY         = 'inline_query';
44
    public const TYPE_CHOSEN_INLINE_RESULT = 'chosen_inline_result';
45
    public const TYPE_CALLBACK_QUERY       = 'callback_query';
46
    public const TYPE_SHIPPING_QUERY       = 'shipping_query';
47
    public const TYPE_PRE_CHECKOUT_QUERY   = 'pre_checkout_query';
48
    public const TYPE_POLL                 = 'poll';
49
    public const TYPE_POLL_ANSWER          = 'poll_answer';
50
    public const TYPE_MY_CHAT_MEMBER       = 'my_chat_member';
51
    public const TYPE_CHAT_MEMBER          = 'chat_member';
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 4
    protected function subEntities(): array
57
    {
58
        return [
59 4
            self::TYPE_MESSAGE              => Message::class,
60 4
            self::TYPE_EDITED_MESSAGE       => EditedMessage::class,
61 4
            self::TYPE_CHANNEL_POST         => ChannelPost::class,
62 4
            self::TYPE_EDITED_CHANNEL_POST  => EditedChannelPost::class,
63 4
            self::TYPE_INLINE_QUERY         => InlineQuery::class,
64 4
            self::TYPE_CHOSEN_INLINE_RESULT => ChosenInlineResult::class,
65 4
            self::TYPE_CALLBACK_QUERY       => CallbackQuery::class,
66 4
            self::TYPE_SHIPPING_QUERY       => ShippingQuery::class,
67 4
            self::TYPE_PRE_CHECKOUT_QUERY   => PreCheckoutQuery::class,
68 4
            self::TYPE_POLL                 => Poll::class,
69 4
            self::TYPE_POLL_ANSWER          => PollAnswer::class,
70 4
            self::TYPE_MY_CHAT_MEMBER       => ChatMemberUpdated::class,
71 4
            self::TYPE_CHAT_MEMBER          => ChatMemberUpdated::class,
72
        ];
73
    }
74
75
    /**
76
     * Get the list of all available update types
77
     *
78
     * @return string[]
79
     */
80
    public static function getUpdateTypes(): array
81
    {
82
        return array_keys((new self([]))->subEntities());
83
    }
84
85
    /**
86
     * Get the update type based on the set properties
87
     *
88
     * @return string|null
89
     */
90
    public function getUpdateType(): ?string
91
    {
92
        foreach (self::getUpdateTypes() as $type) {
93
            if ($this->getProperty($type)) {
94
                return $type;
95
            }
96
        }
97
98
        return null;
99
    }
100
101
    /**
102
     * Get update content
103
     *
104
     * @return CallbackQuery|ChatMemberUpdated|ChosenInlineResult|InlineQuery|Message|PollAnswer|Poll|PreCheckoutQuery|ShippingQuery
105
     */
106
    public function getUpdateContent()
107
    {
108
        if ($update_type = $this->getUpdateType()) {
109
            // Instead of just getting the property as an array,
110
            // use the __call method to get the correct Entity object.
111
            $method = 'get' . str_replace('_', '', ucwords($update_type, '_'));
112
            return $this->$method();
113
        }
114
115
        return null;
116
    }
117
}
118