Completed
Push — feature/refactor-app-design ( d959a3 )
by Avtandil
03:51
created

Update   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 61.53%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 67
ccs 8
cts 13
cp 0.6153
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A subEntities() 0 14 1
A getUpdateContent() 0 11 2
A getUpdateType() 0 21 3
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 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.
22
 * @method Message             getMessage()            Optional. New incoming message of any kind — text, photo, sticker, etc.
23
 * @method Message             getEditedMessage()      Optional. New version of a message that is known to the bot and was edited
24
 * @method Message             getChannelPost()        Optional. New post in the channel, can be any kind — text, photo, sticker, etc.
25
 * @method Message             getEditedChannelPost()  Optional. New version of a post in the channel that is known to the bot and was edited
26
 * @method InlineQuery         getInlineQuery()        Optional. New incoming inline query
27
 * @method ChosenInlineResult  getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
28
 * @method CallbackQuery       getCallbackQuery()      Optional. New incoming callback query
29
 * @method ShippingQuery       getShippingQuery()      Optional. New incoming shipping query. Only for invoices with flexible price
30
 * @method PreCheckoutQuery    getPreCheckoutQuery()   Optional. New incoming pre-checkout query. Contains full information about checkout
31
 */
32
class Update extends Entity
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37 3
    protected function subEntities()
38
    {
39
        return [
40 3
            'message'              => Message::class,
41
            'edited_message'       => EditedMessage::class,
42
            'channel_post'         => ChannelPost::class,
43
            'edited_channel_post'  => EditedChannelPost::class,
44
            'inline_query'         => InlineQuery::class,
45
            'chosen_inline_result' => ChosenInlineResult::class,
46
            'callback_query'       => CallbackQuery::class,
47
            'shipping_query'       => ShippingQuery::class,
48
            'pre_checkout_query'   => PreCheckoutQuery::class,
49
        ];
50
    }
51
52
    /**
53
     * Get the update type based on the set properties
54
     *
55
     * @return string|null
56
     */
57 1
    public function getUpdateType()
58
    {
59
        $types = [
60 1
            'message',
61
            'edited_message',
62
            'channel_post',
63
            'edited_channel_post',
64
            'inline_query',
65
            'chosen_inline_result',
66
            'callback_query',
67
            'shipping_query',
68
            'pre_checkout_query',
69
        ];
70 1
        foreach ($types as $type) {
71 1
            if ($this->getProperty($type)) {
72 1
                return $type;
73
            }
74
        }
75
76 1
        return null;
77
    }
78
79
    /**
80
     * Get update content
81
     *
82
     * @return \Longman\TelegramBot\Entities\CallbackQuery
83
     *         |\Longman\TelegramBot\Entities\ChosenInlineResult
84
     *         |\Longman\TelegramBot\Entities\InlineQuery
85
     *         |\Longman\TelegramBot\Entities\Message
86
     */
87
    public function getUpdateContent()
88
    {
89
        if ($update_type = $this->getUpdateType()) {
90
            // Instead of just getting the property as an array,
91
            // use the __call method to get the correct Entity object.
92
            $method = 'get' . str_replace('_', '', ucwords($update_type, '_'));
93
            return $this->$method();
94
        }
95
96
        return null;
97
    }
98
}
99