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

ExchangeConversation::askEmail()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 23
ccs 0
cts 14
cp 0
rs 9.8333
cc 3
nc 1
nop 1
crap 12
1
<?php
2
3
namespace App\Conversations;
4
5
use App\Models\Customer;
6
use BotMan\BotMan\Messages\Incoming\Answer;
7
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
8
use BotMan\BotMan\Messages\Outgoing\Question;
9
10
class ExchangeConversation extends Conversation
11
{
12
    /**
13
     * Start the conversation.
14
     *
15
     * @return $this
16
     */
17
    public function run()
18
    {
19
        return $this
20
            ->sayRenderable('conversations.exchange.index')
21
            ->verifyCustomer();
22
    }
23
24
    /**
25
     * Verify if the current customer has been registered or not.
26
     *
27
     * @return $this
28
     */
29
    protected function verifyCustomer()
30
    {
31
        if (!$customer = Customer::retrieveByBotManUser($this->getUser())) {
32
            $username = $this->getUser()->getUsername();
33
            $email = $this->getUserStorage('email');
34
35
            if (!$customer = Customer::retrieveByUsernameAndEmail(compact('username', 'email'))) {
36
                return $this
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setPreviousConver...CustomerConversation()) 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...
Bug Best Practice introduced by
The expression return $this->setPreviou...CustomerConversation()) returns the type void which is incompatible with the documented return type App\Conversations\ExchangeConversation.
Loading history...
37
                    ->setPreviousConversation($this)
38
                    ->startConversation(new RegisterCustomerConversation);
39
            }
40
        }
41
42
        return $this->displayCustomerData($customer);
43
    }
44
45
    /**
46
     * Reply with customer data.
47
     *
48
     * @param  \App\Models\Customer  $customer
49
     * @return $this
50
     */
51
    protected function displayCustomerData(Customer $customer)
52
    {
53
        if (!$this->getPreviousConversation() instanceof static) {
54
            $this->say('<em>Data anda sebelumnya sudah pernah terekam di database kami.</em>');
55
        }
56
57
        $this->destroyUserStorage(forceDestroy: true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type null|string expected by parameter $key of App\Conversations\Conver...n::destroyUserStorage(). ( Ignorable by Annotation )

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

57
        $this->destroyUserStorage(/** @scrutinizer ignore-type */ forceDestroy: true);
Loading history...
58
59
        $question = Question::create(view('conversations.exchange.confirm-customer_data', compact('customer'))->render())
0 ignored issues
show
Bug introduced by
It seems like view('conversations.exch...('customer'))->render() can also be of type array; however, parameter $text of BotMan\BotMan\Messages\Outgoing\Question::create() does only seem to accept 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

59
        $question = Question::create(/** @scrutinizer ignore-type */ view('conversations.exchange.confirm-customer_data', compact('customer'))->render())
Loading history...
60
            ->callbackId('exchange_confirm_customer_data')
61
            ->addButtons([
62
                Button::create(view('conversations.register-customer.reply-customer_data-yes')->render())->value('yes'),
0 ignored issues
show
Bug introduced by
It seems like view('conversations.regi...er_data-yes')->render() can also be of type array; however, parameter $text of BotMan\BotMan\Messages\O...ctions\Button::create() does only seem to accept 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

62
                Button::create(/** @scrutinizer ignore-type */ view('conversations.register-customer.reply-customer_data-yes')->render())->value('yes'),
Loading history...
63
                Button::create(view('conversations.register-customer.reply-customer_data-no')->render())->value('no'),
64
            ]);
65
66
        return $this->ask($question, next: function (Answer $answer) use ($customer) {
0 ignored issues
show
Unused Code introduced by
The import $customer is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
67
            if (!$answer->isInteractiveMessageReply()) {
68
                return;
69
            }
70
71
            if (!in_array($value = $answer->getValue(), ['yes', 'no'])) {
72
                return $this->displayFallback($answer->getText());
73
            }
74
75
            if ($value === 'no') {
76
                $this->destroyUserStorage();
77
78
                return $this
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setPreviousConver...CustomerConversation()) 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...
79
                    ->setPreviousConversation($this)
80
                    ->startConversation(new UpdateCustomerConversation);
81
            }
82
83
            return $this->recordOrder();
84
        });
85
    }
86
87
    /**
88
     * Record customer order data.
89
     *
90
     * @return $this
91
     */
92
    protected function recordOrder()
93
    {
94
        return $this->say('terima kasih :)');
95
    }
96
}
97