Passed
Push — develop ( e34a97...d968c2 )
by Septianata
14:57
created

UpdateCustomerConversation::askGender()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.9
cc 2
nc 1
nop 0
crap 6
1
<?php
2
3
namespace App\Conversations;
4
5
use App\Http\Requests\Customer\UpdateRequest as CustomerUpdateRequest;
6
use BotMan\BotMan\Messages\Incoming\Answer;
7
use BotMan\Drivers\Telegram\Extensions\Keyboard;
8
use BotMan\Drivers\Telegram\Extensions\KeyboardButton;
9
10
class UpdateCustomerConversation extends RegisterCustomerConversation
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15
    public function run()
16
    {
17
        return $this->askGender();
18
    }
19
20
    /**
21
     * Ask the customer gender.
22
     *
23
     * @return $this
24
     */
25
    protected function askGender()
26
    {
27
        return $this->askRenderable('conversations.update-customer.confirm-gender', next: function (Answer $answer) {
28
            if (!$answer->isInteractiveMessageReply()) {
29
                return;
30
            }
31
32
            $this->setUserStorage(['gender' => $answer->getValue()]);
33
34
            return $this->askEmail();
35
        }, additionalParameters: Keyboard::create(Keyboard::TYPE_INLINE)->resizeKeyboard()->addRow(
36
            KeyboardButton::create(view('conversations.start.reply-gender-male')->render())->callbackData('male'),
37
            KeyboardButton::create(view('conversations.start.reply-gender-female')->render())->callbackData('female')
38
        )->addRow(
39
            KeyboardButton::create(view('conversations.start.reply-gender-undefined')->render())->callbackData('unknown')
40
        )->toArray());
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    protected function askEmail(string $validationErrorMessage = null)
47
    {
48
        $this->displayValidationErrorMessage($validationErrorMessage);
49
50
        return $this->askRenderable('conversations.exchange.ask-email', function (Answer $answer) {
51
            $value = $answer->getText();
52
            $validator = CustomerUpdateRequest::createValidator($value, 'email');
53
54
            if ($validator->fails()) {
55
                return $this->askEmail($validator->errors()->first('email'));
56
            }
57
58
            $this->setUserStorage(['email' => $validator->validated()['email']]);
59
60
            return $this->askFullName();
61
        });
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    protected function askPhone(string $validationErrorMessage = null)
68
    {
69
        $this->displayValidationErrorMessage($validationErrorMessage);
70
71
        return $this->askRenderable('conversations.register-customer.ask-phone', function () {
72
            $value = $this->getMessagePayload('contact.phone_number');
73
            $validator = CustomerUpdateRequest::createValidator($value, 'phone');
74
75
            if ($validator->fails()) {
76
                return $this->askPhone($validator->errors()->first('phone'));
77
            }
78
79
            $this->setUserStorage(['phone' => $validator->validated()['phone']]);
80
81
            return $this->askWhatsappPhone();
82
        }, additionalParameters: ['reply_markup' => json_encode([
83
            'keyboard' => [[['text' => '☎️ ' . trans('Send My Phone Number'), 'request_contact' => true]]],
0 ignored issues
show
Bug introduced by
Are you sure trans('Send My Phone Number') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

83
            'keyboard' => [[['text' => '☎️ ' . /** @scrutinizer ignore-type */ trans('Send My Phone Number'), 'request_contact' => true]]],
Loading history...
84
            'resize_keyboard' => true,
85
            'one_time_keyboard' => true,
86
            'remove_keyboard' => true,
87
        ])]);
88
    }
89
}
90