Update::getChat()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Telegram\Bot\Objects;
4
5
/**
6
 * Class Update.
7
 *
8
 *
9
 * @method int                  getUpdateId()               The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially.
10
 * @method Message              getMessage()                (Optional). New incoming message of any kind - text, photo, sticker, etc.
11
 * @method EditedMessage        getEditedMessage()          (Optional). New version of a message that is known to the bot and was edited.
12
 * @method InlineQuery          getInlineQuery()            (Optional). New incoming inline query.
13
 * @method ChosenInlineResult   getChosenInlineResult()     (Optional). A result of an inline query that was chosen by the user and sent to their chat partner.
14
 * @method CallbackQuery        getCallbackQuery()          (Optional). Incoming callback query.
15
 *
16
 * @link https://core.telegram.org/bots/api#update
17
 */
18
class Update extends BaseObject
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 6
    public function relations()
24
    {
25
        return [
26 6
            'message'              => Message::class,
27 6
            'edited_message'       => EditedMessage::class,
28 6
            'inline_query'         => InlineQuery::class,
29 6
            'chosen_inline_result' => ChosenInlineResult::class,
30 6
            'callback_query'       => CallbackQuery::class,
31 6
        ];
32
    }
33
34
    /**
35
     * Get recent message.
36
     *
37
     * @return Update
38
     */
39
    public function recentMessage()
40
    {
41
        return new static($this->last());
42
    }
43
    
44
    /**
45
     * Determine if the update is of given type
46
     *
47
     * @param string         $type
48
     *
49
     * @return bool
50
     */
51
    public function isType($type)
52
    {
53
        if ($this->has(strtolower($type))) {
54
            return true;
55
        }
56
    
57
        return $this->detectType() === $type;
58
    }
59
    
60
    /**
61
     * Detect type based on properties.
62
     *
63
     * @return string|null
64
     */
65
    public function detectType()
66
    {
67
        $types = [
68
            'message',
69
            'edited_message',
70
            'inline_query',
71
            'chosen_inline_result',
72
            'callback_query',
73
        ];
74
75
        return $this->keys()
76
            ->intersect($types)
77
            ->pop();
78
    }
79
    
80
    /**
81
     * Get message object (if exists)
82
     *
83
     * @return null|Chat
84
     */
85
    public function getChat()
86
    {
87
        switch ($this->detectType())
88
        {
89
            case 'message':
90
                return $this->getMessage()->getChat();
91
            case 'callback_query':
92
                return $this->getCallbackQuery()->getMessage()->getChat();
93
            default:
94
                // nothing to return
95
                return null;
96
        }
97
    }
98
}
99