InlineKeyboardButton   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B couldBe() 0 10 6
B validate() 0 18 5
A __call() 0 9 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\Exception\TelegramException;
14
15
/**
16
 * Class InlineKeyboardButton
17
 *
18
 * @link https://core.telegram.org/bots/api#inlinekeyboardbutton
19
 *
20
 * @method string getText()                         Label text on the button
21
 * @method string getUrl()                          Optional. HTTP url to be opened when button is pressed
22
 * @method string getCallbackData()                 Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
23
 * @method string getSwitchInlineQuery()            Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted.
24
 * @method string getSwitchInlineQueryCurrentChat() Optional. If set, pressing the button will insert the bot‘s username and the specified inline query in the current chat's input field. Can be empty, in which case only the bot’s username will be inserted.
25
 *
26
 * @method $this setText(string $text)                                                     Label text on the button
27
 * @method $this setUrl(string $url)                                                       Optional. HTTP url to be opened when button is pressed
28
 * @method $this setCallbackData(string $callback_data)                                    Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
29
 * @method $this setSwitchInlineQuery(string $switch_inline_query)                         Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted.
30
 * @method $this setSwitchInlineQueryCurrentChat(string $switch_inline_query_current_chat) Optional. If set, pressing the button will insert the bot‘s username and the specified inline query in the current chat's input field. Can be empty, in which case only the bot’s username will be inserted.
31
 */
32
class InlineKeyboardButton extends KeyboardButton
33
{
34
    /**
35
     * Check if the passed data array could be an InlineKeyboardButton.
36
     *
37
     * @param array $data
38
     *
39
     * @return bool
40
     */
41 1
    public static function couldBe($data)
42
    {
43 1
        return is_array($data) &&
44 1
               array_key_exists('text', $data) && (
45 1
                   array_key_exists('url', $data) ||
46 1
                   array_key_exists('callback_data', $data) ||
47 1
                   array_key_exists('switch_inline_query', $data) ||
48 1
                   array_key_exists('switch_inline_query_current_chat', $data)
49
               );
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 10
    protected function validate()
56
    {
57 10
        if ($this->getProperty('text', '') === '') {
58 1
            throw new TelegramException('You must add some text to the button!');
59
        }
60
61 9
        $num_params = 0;
62
63 9
        foreach (['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat'] as $param) {
64 9
            if (!empty($this->getProperty($param))) {
65 9
                $num_params++;
66
            }
67
        }
68
69 9
        if ($num_params !== 1) {
70 2
            throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat!');
71
        }
72 7
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 6
    public function __call($method, $args)
78
    {
79
        // Only 1 of these can be set, so clear the others when setting a new one.
80 6
        if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat'], true)) {
81 1
            unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat);
82
        }
83
84 6
        return parent::__call($method, $args);
85
    }
86
}
87