Completed
Push — remove_monolog ( 2c1bce )
by Armando
02:53
created

Update::subEntities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
ccs 2
cts 2
cp 1
crap 1
rs 9.9
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
 */
34
class Update extends Entity
35
{
36
    /**
37
     * {@inheritdoc}
38
     */
39 3
    protected function subEntities()
40
    {
41
        return [
42 3
            'message'              => Message::class,
43
            'edited_message'       => EditedMessage::class,
44
            'channel_post'         => ChannelPost::class,
45
            'edited_channel_post'  => EditedChannelPost::class,
46
            'inline_query'         => InlineQuery::class,
47
            'chosen_inline_result' => ChosenInlineResult::class,
48
            'callback_query'       => CallbackQuery::class,
49
            'shipping_query'       => ShippingQuery::class,
50
            'pre_checkout_query'   => PreCheckoutQuery::class,
51
            'poll'                 => Poll::class,
52
        ];
53
    }
54
55
    /**
56
     * Get the update type based on the set properties
57
     *
58
     * @return string|null
59
     */
60
    public function getUpdateType()
61
    {
62
        $types = array_keys($this->subEntities());
63
        foreach ($types as $type) {
64
            if ($this->getProperty($type)) {
65
                return $type;
66
            }
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * Get update content
74
     *
75
     * @return CallbackQuery|ChosenInlineResult|InlineQuery|Message
76
     */
77
    public function getUpdateContent()
78
    {
79
        if ($update_type = $this->getUpdateType()) {
80
            // Instead of just getting the property as an array,
81
            // use the __call method to get the correct Entity object.
82
            $method = 'get' . str_replace('_', '', ucwords($update_type, '_'));
83
            return $this->$method();
84
        }
85
86
        return null;
87
    }
88
}
89