Completed
Push — feature/refactor-app-design ( 84f9ff...b18d21 )
by Avtandil
04:09
created

Update   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 69
ccs 16
cts 21
cp 0.7619
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A subEntities() 0 14 1
A getUpdateType() 0 12 3
A getUpdateContent() 0 12 2
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Entities;
12
13
use Longman\TelegramBot\Entities\Payments\PreCheckoutQuery;
14
use Longman\TelegramBot\Entities\Payments\ShippingQuery;
15
16
/**
17
 * Class Update
18
 *
19
 * @link https://core.telegram.org/bots/api#update
20
 *
21
 * @method int                 getUpdateId()           The update's unique identifier. Update identifiers start from a certain positive number and increase
22
 *     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
23
 *     sequence, should they get out of order.
24
 * @method Message             getMessage()            Optional. New incoming message of any kind — text, photo, sticker, etc.
25
 * @method Message             getEditedMessage()      Optional. New version of a message that is known to the bot and was edited
26
 * @method Message             getChannelPost()        Optional. New post in the channel, can be any kind — text, photo, sticker, etc.
27
 * @method Message             getEditedChannelPost()  Optional. New version of a post in the channel that is known to the bot and was edited
28
 * @method InlineQuery         getInlineQuery()        Optional. New incoming inline query
29
 * @method ChosenInlineResult  getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
30
 * @method CallbackQuery       getCallbackQuery()      Optional. New incoming callback query
31
 * @method ShippingQuery       getShippingQuery()      Optional. New incoming shipping query. Only for invoices with flexible price
32
 * @method PreCheckoutQuery    getPreCheckoutQuery()   Optional. New incoming pre-checkout query. Contains full information about checkout
33
 */
34
class Update extends Entity
35
{
36
    const TYPE_MESSAGE = 'message';
37
    const TYPE_EDITED_MESSAGE = 'edited_message';
38
    const TYPE_CHANNEL_POST = 'channel_post';
39
    const TYPE_EDITED_CHANNEL_POST = 'edited_channel_post';
40
    const TYPE_INLINE_QUERY = 'inline_query';
41
    const TYPE_CHOSEN_INLINE_QUERY = 'chosen_inline_result';
42
    const TYPE_CALLBACK_QUERY = 'callback_query';
43
    const TYPE_SHIPPING_QUERY = 'shipping_query';
44
    const TYPE_PRE_CHECKOUT_QUERY = 'pre_checkout_query';
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 5
    protected function subEntities()
50
    {
51
        return [
52 5
            self::TYPE_MESSAGE             => Message::class,
53 5
            self::TYPE_EDITED_MESSAGE      => EditedMessage::class,
54 5
            self::TYPE_CHANNEL_POST        => ChannelPost::class,
55 5
            self::TYPE_EDITED_CHANNEL_POST => EditedChannelPost::class,
56 5
            self::TYPE_INLINE_QUERY        => InlineQuery::class,
57 5
            self::TYPE_CHOSEN_INLINE_QUERY => ChosenInlineResult::class,
58 5
            self::TYPE_CALLBACK_QUERY      => CallbackQuery::class,
59 5
            self::TYPE_SHIPPING_QUERY      => ShippingQuery::class,
60 5
            self::TYPE_PRE_CHECKOUT_QUERY  => PreCheckoutQuery::class,
61
        ];
62
    }
63
64
    /**
65
     * Get the update type based on the set properties
66
     *
67
     * @return string|null
68
     */
69 2
    public function getUpdateType()
70
    {
71 2
        $types = array_keys($this->subEntities());
72
73 2
        foreach ($types as $type) {
74 2
            if ($this->getProperty($type)) {
75 2
                return $type;
76
            }
77
        }
78
79 2
        return null;
80
    }
81
82
    /**
83
     * Get update content
84
     *
85
     * @return \Longman\TelegramBot\Entities\CallbackQuery
86
     *         |\Longman\TelegramBot\Entities\ChosenInlineResult
87
     *         |\Longman\TelegramBot\Entities\InlineQuery
88
     *         |\Longman\TelegramBot\Entities\Message
89
     */
90
    public function getUpdateContent()
91
    {
92
        if ($update_type = $this->getUpdateType()) {
93
            // Instead of just getting the property as an array,
94
            // use the __call method to get the correct Entity object.
95
            $method = 'get' . str_replace('_', '', ucwords($update_type, '_'));
96
97
            return $this->$method();
98
        }
99
100
        return null;
101
    }
102
}
103