Completed
Push — master ( 15677b...615879 )
by Vladimir
02:54
created

TelegramOutgoingMessage::compileInlineKeyboard()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 9.2
cc 4
eloc 11
nc 4
nop 0
crap 4.016
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Channels\Telegram;
6
7
use FondBot\Contracts\Channels\User;
8
use FondBot\Conversation\Buttons\UrlButton;
9
use FondBot\Contracts\Conversation\Keyboard;
10
use FondBot\Contracts\Channels\OutgoingMessage;
11
use FondBot\Conversation\Buttons\PayloadButton;
12
use FondBot\Channels\Telegram\Buttons\RequestContactButton;
13
14
class TelegramOutgoingMessage implements OutgoingMessage
15
{
16
    private const KEYBOARD_REPLY = 'keyboard';
17
    private const KEYBOARD_INLINE = 'inline_keyboard';
18
19
    private $recipient;
20
    private $text;
21
    private $keyboard;
22
23 9
    public function __construct(User $recipient, string $text, Keyboard $keyboard = null)
24
    {
25 9
        $this->recipient = $recipient;
26 9
        $this->text = $text;
27 9
        $this->keyboard = $keyboard;
28 9
    }
29
30
    /**
31
     * Get recipient.
32
     *
33
     * @return User
34
     */
35 2
    public function getRecipient(): User
36
    {
37 2
        return $this->recipient;
38
    }
39
40
    /**
41
     * Get message text.
42
     *
43
     * @return string
44
     */
45 2
    public function getText(): string
46
    {
47 2
        return $this->text;
48
    }
49
50
    /**
51
     * Get keyboard.
52
     *
53
     * @return Keyboard|null
54
     */
55 2
    public function getKeyboard(): ?Keyboard
56
    {
57 2
        return $this->keyboard;
58
    }
59
60
    /**
61
     * Get the instance as an array.
62
     *
63
     * @return array
64
     */
65 6
    public function toArray(): array
66
    {
67
        $payload = [
68 6
            'chat_id' => $this->recipient->getId(),
69 6
            'text' => $this->text,
70
        ];
71
72 6
        if ($replyMarkup = $this->getReplyMarkup()) {
0 ignored issues
show
Unused Code introduced by
$replyMarkup is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
73 3
            $payload['reply_markup'] = json_encode($this->getReplyMarkup());
74
        }
75
76 6
        return $payload;
77
    }
78
79 6
    private function getReplyMarkup(): ?array
80
    {
81 6
        if ($this->keyboard !== null) {
82 3
            $type = $this->detectKeyboardType();
83
84 3
            $keyboard = [];
85
            switch ($type) {
86 3
                case self::KEYBOARD_REPLY:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
87 2
                    $keyboard = $this->compileReplyKeyboard();
88 2
                    break;
89 1
                case self::KEYBOARD_INLINE:
90 1
                    $keyboard = $this->compileInlineKeyboard();
91 1
                    break;
92
            }
93
94 3
            return $keyboard;
95
        }
96
97 3
        return null;
98
    }
99
100
    /**
101
     * Compile reply keyboard markup.
102
     *
103
     * @return array
104
     */
105 2
    private function compileReplyKeyboard(): array
106
    {
107 2
        $buttons = [];
108 2
        foreach ($this->keyboard->getButtons() as $button) {
109 2
            $parameters = ['text' => $button->getLabel()];
110
111 2
            if ($button instanceof RequestContactButton) {
112 1
                $parameters['request_contact'] = true;
113
            }
114
115 2
            $buttons[] = $parameters;
116
        }
117
118
        return [
119 2
            'keyboard' => [$buttons],
120
            'one_time_keyboard' => true,
121
        ];
122
    }
123
124
    /**
125
     * Compile inline keyboard markup.
126
     *
127
     * @return array
128
     */
129 1
    private function compileInlineKeyboard(): array
130
    {
131 1
        $buttons = [];
132 1
        foreach ($this->keyboard->getButtons() as $button) {
133 1
            $parameters = ['text' => $button->getLabel()];
134
135 1
            if ($button instanceof UrlButton) {
136 1
                $parameters['url'] = $button->getUrl();
137
            } elseif ($button instanceof PayloadButton) {
138 1
                $parameters['callback_data'] = $button->getPayload();
139
            }
140
141 1
            $buttons[] = $parameters;
142
        }
143
144
        return [
145 1
            'inline_keyboard' => [$buttons],
146
        ];
147
    }
148
149 3
    private function detectKeyboardType(): string
150
    {
151 3
        $button = collect($this->keyboard->getButtons())->first();
152
153 3
        if ($button instanceof PayloadButton || $button instanceof UrlButton) {
154 1
            return self::KEYBOARD_INLINE;
155
        }
156
157 2
        return self::KEYBOARD_REPLY;
158
    }
159
}
160