Passed
Push — develop ( 62f833...95decf )
by Septianata
11:22
created

HomeConservation::conversations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 37
c 0
b 0
f 0
dl 0
loc 59
ccs 0
cts 0
cp 0
rs 9.328
cc 1
nc 1
nop 1
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Conversations;
4
5
use App\Enum\Gender;
6
use BotMan\BotMan\Messages\Incoming\Answer;
7
use BotMan\Drivers\Telegram\Extensions\Keyboard;
8
use BotMan\Drivers\Telegram\Extensions\KeyboardButton;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Collection;
11
use Illuminate\Support\Str;
12
13
class HomeConservation extends Conversation
14
{
15
    /**
16
     * Start the conversation.
17
     *
18
     * @return void
19
     */
20
    public function run()
21
    {
22
        $title = Str::ucfirst(Gender::title($this->getUserStorage('gender')));
23
        $name = Str::ucfirst($this->getUser()->getFirstName());
24
25
        $keyboard = Keyboard::create(Keyboard::TYPE_INLINE)->resizeKeyboard();
26
27
        foreach (static::conversations(withoutStartCommand: true) as $conversation) {
28
            $commands = Arr::wrap($conversation['command']);
29
30
            $keyboard->addRow(KeyboardButton::create(Arr::last($commands))
31
                ->callbackData(Arr::first($commands))
32
            );
33
        }
34
35
        $response = $this->reply(
36
            $question = view('conversations.home.confirm-menu', compact('title', 'name'))->render(),
0 ignored issues
show
Bug introduced by
It seems like $question = view('conver...le', 'name'))->render() can also be of type array; however, parameter $message of App\Conversations\Conversation::reply() does only seem to accept BotMan\BotMan\Messages\O...utgoing\Question|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
            /** @scrutinizer ignore-type */ $question = view('conversations.home.confirm-menu', compact('title', 'name'))->render(),
Loading history...
37
            $additionalParameters = $keyboard->toArray()
38
        );
39
40
        return $this->getBot()->storeConversation($this, next: function (Answer $answer) use ($response) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getBot()->storeCo... $additionalParameters) targeting BotMan\BotMan\BotMan::storeConversation() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
41
            if (!$answer->isInteractiveMessageReply()) {
42
                return;
43
            }
44
45
            $this->deleteTelegramMessageFromResponse($response);
46
47
            if (!$conversation = $this->getConversation($answer->getValue())) {
48
                return $this->sayFallbackMessage($answer->getText());
49
            }
50
51
            return $this->startConversation($conversation);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->startConversation($conversation) targeting App\Conversations\Conver...on::startConversation() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
        }, question: $question, additionalParameters: $additionalParameters);
53
    }
54
55
    /**
56
     * Return specific conversation instance based on the given name.
57
     *
58
     * @param  string  $name
59
     * @return \BotMan\BotMan\Messages\Conversations\Conversation|null
60
     */
61
    protected function getConversation(string $name): ?Conversation
62
    {
63
        if (!$conversation = static::conversations(withoutStartCommand: true)->first(fn ($conversation) =>
64
            in_array($name, Arr::wrap($conversation['command']))
65
        )) {
66
            return null;
67
        }
68
69
        return value($conversation['handler']);
70
    }
71
72
    /**
73
     * Return list of command and its conversation handler class.
74
     *
75
     * @param  bool  $withoutStartCommand
76
     * @return \Illuminate\Support\Collection<array>
77
     */
78
    public static function conversations(bool $withoutStartCommand = false): Collection
79
    {
80
        return collect([
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect(array(arr...ion(...) { /* ... */ }) could return the type Tightenco\Collect\Support\Collection which is incompatible with the type-hinted return Illuminate\Support\Collection. Consider adding an additional type-check to rule them out.
Loading history...
81
            [
82
                'command' => 'start',
83
                'handler' => fn () => new StartConservation,
84
                'description' => 'Memulai percakapan',
85
            ],
86
            [
87
                'command' => [
88
                    'exchange',
89
                    view('conversations.home.reply-menu-exchange')->render(),
90
                ],
91
                'handler' => fn () => new ExchangeConversation,
92
                'description' => 'Melakukan transaksi penukaran uang',
93
            ],
94
            [
95
                'command' => [
96
                    'help',
97
                    view('conversations.home.reply-menu-help')->render(),
98
                ],
99
                'handler' => fn () => new HelpConversation,
100
                'description' => 'Panduan cara menggunakan chatbot ini',
101
            ],
102
            [
103
                'command' => [
104
                    'login',
105
                    view('conversations.home.reply-menu-login')->render(),
106
                ],
107
                'handler' => fn () => new LoginConversation,
108
                'description' => 'Mendaftarkan Chat ID Telegram pada akun (khusus admin dan staf)',
109
            ],
110
            [
111
                'command' => [
112
                    'logout',
113
                    view('conversations.home.reply-menu-logout')->render(),
114
                ],
115
                'handler' => fn () => new LogoutConversation,
116
                'description' => 'Menghapus Chat ID Telegram pada akun (khusus admin dan staf)',
117
            ],
118
            [
119
                'command' => [
120
                    'ask_telegram_chat_id',
121
                    view('conversations.home.reply-menu-ask_telegram_chat_id')->render(),
122
                ],
123
                'handler' => fn () => new AskTelegramChatIdConversation,
124
                'description' => 'Mengetahui Chat ID Telegram anda',
125
            ],
126
            [
127
                'command' => [
128
                    'check_order_status',
129
                    view('conversations.home.reply-menu-check_order_status')->render(),
130
                ],
131
                'handler' => fn () => new CheckOrderStatusConversation,
132
                'description' => 'Mengetahui status transaksi penukaran uang anda',
133
            ],
134
        ])->when($withoutStartCommand, fn (Collection $conversations) =>
135
            $conversations->reject(fn ($conversation) =>
136
                in_array('start', Arr::wrap($conversation['command']))
137
            )
138
        );
139
    }
140
}
141