|
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 |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$question = Question::create(view('conversations.exchange.confirm-customer_data', compact('customer'))->render()) |
|
|
|
|
|
|
60
|
|
|
->callbackId('exchange_confirm_customer_data') |
|
61
|
|
|
->addButtons([ |
|
62
|
|
|
Button::create(view('conversations.register-customer.reply-customer_data-yes')->render())->value('yes'), |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
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.