Completed
Push — master ( 9d4d02...0bdd7a )
by Armando
06:45
created

Update   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 63
ccs 2
cts 13
cp 0.1538
rs 10

3 Methods

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