Passed
Push — develop ( 16ae21...f7a059 )
by Septianata
04:44
created

Conversation::sayFallbackMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace App\Conversations;
4
5
use BotMan\BotMan\Interfaces\UserInterface;
6
use BotMan\BotMan\Messages\Conversations\Conversation as BaseConversation;
7
use Closure;
8
use Illuminate\Contracts\Support\Renderable;
9
use Symfony\Component\HttpFoundation\Response;
10
11
abstract class Conversation extends BaseConversation
12
{
13
    /**
14
     * Run `$this->say()` method with a renderable parameter.
15
     *
16
     * @param  \Illuminate\Contracts\Support\Renderable|string  $view
17
     * @param  array  $viewData
18
     * @param  array  $viewMergeData
19
     * @param  array  $additionalParameters
20
     * @return $this
21
     */
22
    protected function sayRenderable($view, array $viewData = [], array $viewMergeData = [], array $additionalParameters = [])
23
    {
24
        $view = $view instanceof Renderable ? $view : view($view, $viewData, $viewMergeData);
25
26
        $this->say($view->render(), $additionalParameters);
0 ignored issues
show
Bug introduced by
It seems like $view->render() can also be of type array; however, parameter $message of BotMan\BotMan\Messages\C...ons\Conversation::say() does only seem to accept BotMan\BotMan\Messages\Outgoing\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

26
        $this->say(/** @scrutinizer ignore-type */ $view->render(), $additionalParameters);
Loading history...
27
28
        return $this;
29
    }
30
31
    /**
32
     * Run `$this->ask()` method with a renderable parameter.
33
     *
34
     * @param  \Illuminate\Contracts\Support\Renderable|string  $view
35
     * @param  array|\Closure  $next
36
     * @param  array  $viewData
37
     * @param  array  $viewMergeData
38
     * @param  array  $additionalParameters
39
     * @return $this
40
     */
41
    protected function askRenderable($view, $next, array $viewData = [], array $viewMergeData = [], array $additionalParameters = [])
42
    {
43
        $view = $view instanceof Renderable ? $view : view($view, $viewData, $viewMergeData);
44
45
        $this->ask($view->render(), $next, $additionalParameters);
0 ignored issues
show
Bug introduced by
It seems like $view->render() can also be of type array; however, parameter $question of BotMan\BotMan\Messages\C...ons\Conversation::ask() does only seem to accept BotMan\BotMan\Messages\Outgoing\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

45
        $this->ask(/** @scrutinizer ignore-type */ $view->render(), $next, $additionalParameters);
Loading history...
46
47
        return $this;
48
    }
49
50
    /**
51
     * Return BotMan user data.
52
     *
53
     * @return \BotMan\BotMan\Interfaces\UserInterface
54
     */
55
    protected function getUser(): UserInterface
56
    {
57
        return $this->getBot()->getUser();
58
    }
59
60
    /**
61
     * Return BotMan user storage data.
62
     *
63
     * @param  mixed|null  $key
64
     * @param  mixed|null  $default
65
     * @return \Illuminate\Support\Collection|mixed
66
     */
67
    protected function getUserStorage($key = null, $default = null)
68
    {
69
        $storage = $this->getBot()->userStorage()->find(
70
            $this->getUser()->getId()
71
        );
72
73
        if (is_null($key)) {
74
            return $storage;
75
        }
76
77
        return $storage->get($key, $default);
78
    }
79
80
    /**
81
     * Set BotMan user storage data.
82
     *
83
     * @param  array  $data
84
     * @param  string|null  $key
85
     * @return void
86
     */
87
    protected function setUserStorage(array $data, string $key = null)
88
    {
89
        $this->getBot()->userStorage()->save($data,$key);
90
    }
91
92
    /**
93
     * Destroy BotMan user storage data.
94
     *
95
     * @param  string|null  $key
96
     * @param  array  $excepts
97
     * @param  bool  $forceDestroy
98
     * @return void
99
     */
100
    protected function destroyUserStorage(string $key = null, array $excepts = [], bool $forceDestroy = false)
101
    {
102
        if (!$forceDestroy) {
103
            $excepts[] = '_previous_conversation';
104
105
            /** @var \Illuminate\Support\Collection $storage */
106
            $storage = $this->getUserStorage();
107
        }
108
109
        $this->getBot()->userStorage()->delete($key);
110
111
        if (!$forceDestroy) {
112
            $this->setUserStorage($storage->only($excepts)->toArray());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $storage does not seem to be defined for all execution paths leading up to this point.
Loading history...
113
        }
114
    }
115
116
    /**
117
     * Return raw message payload from BotMan.
118
     *
119
     * @param  string|array|int|null  $key
120
     * @param  mixed  $default
121
     * @return array|mixed
122
     */
123
    protected function getMessagePayload($key = null, $default = null)
124
    {
125
        return data_get(
126
            json_decode($this->getBot()->getMessage()->getPayload(), true),
127
            $key, $default
128
        );
129
    }
130
131
    /**
132
     * Return reply response with the given validation error message.
133
     *
134
     * @param  string|null  $validationErrorMessage
135
     * @return $this
136
     */
137
    protected function displayValidationErrorMessage(string $validationErrorMessage = null)
138
    {
139
        if ($validationErrorMessage) {
140
            $this->say('⛔️ ' . $validationErrorMessage);
141
        }
142
143
        return $this;
144
    }
145
146
    /**
147
     * Create reply response format with the fallback message.
148
     *
149
     * @param  string  $text
150
     * @param  string  $view
151
     * @return string
152
     */
153
    protected function fallbackMessage(string $text, string $view = 'components.conversations.fallback'): string
154
    {
155
        return view($view, compact('text'))->render();
0 ignored issues
show
Bug Best Practice introduced by
The expression return view($view, compact('text'))->render() could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
156
    }
157
158
    /**
159
     * Return reply response with the fallback message.
160
     *
161
     * @param  string  $text
162
     * @param  string  $view
163
     * @return $this
164
     */
165
    protected function sayFallbackMessage(string $text, string $view = 'components.conversations.fallback')
166
    {
167
        return $this->say($this->fallbackMessage($text, $view));
168
    }
169
170
    /**
171
     * Return previous conversation value.
172
     *
173
     * @return \BotMan\BotMan\Messages\Conversations\Conversation|null
174
     */
175
    protected function getPreviousConversation(): ?BaseConversation
176
    {
177
        if (!$conversation = $this->getUserStorage('_previous_conversation')) {
178
            return null;
179
        }
180
181
        return new $conversation;
182
    }
183
184
    /**
185
     * Set previous conversation value.
186
     *
187
     * @param  string|\BotMan\BotMan\Messages\Conversations\Conversation  $conversation
188
     * @return $this
189
     */
190
    protected function setPreviousConversation($conversation)
191
    {
192
        if (is_a($conversation, BaseConversation::class)) {
193
            $conversation = is_object($conversation) ? get_class($conversation) : $conversation;
194
195
            $this->setUserStorage(['_previous_conversation' => $conversation]);
196
        }
197
198
        return $this;
199
    }
200
201
    /**
202
     * Start previous conversation.
203
     *
204
     * @return void
205
     */
206
    protected function startPreviousConversation()
207
    {
208
        $this->getBot()->startConversation($this->getPreviousConversation());
0 ignored issues
show
Bug introduced by
It seems like $this->getPreviousConversation() can also be of type null; however, parameter $instance of BotMan\BotMan\BotMan::startConversation() does only seem to accept BotMan\BotMan\Messages\Conversations\Conversation, 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

208
        $this->getBot()->startConversation(/** @scrutinizer ignore-type */ $this->getPreviousConversation());
Loading history...
209
    }
210
211
    /**
212
     * Start conversation.
213
     *
214
     * @param  string|\BotMan\BotMan\Messages\Conversations\Conversation  $conversation
215
     * @return void
216
     */
217
    protected function startConversation($conversation)
218
    {
219
        if (is_a($conversation, BaseConversation::class)) {
220
            $conversation = is_object($conversation) ? $conversation : new $conversation;
221
222
            $this->getBot()->startConversation($conversation);
223
        }
224
    }
225
226
    /**
227
     * Send reply message request and return the given response.
228
     *
229
     * @param  string|\BotMan\BotMan\Messages\Outgoing\OutgoingMessage|\BotMan\BotMan\Messages\Outgoing\Question  $message
230
     * @param  array  $additionalParameters
231
     * @param  \Closure|null  $callback
232
     * @return \Symfony\Component\HttpFoundation\Response
233
     */
234
    protected function reply($message, array $additionalParameters = [], Closure $callback = null)
235
    {
236
        $response = $this->getBot()->reply($message, $additionalParameters);
237
238
        if (is_callable($callback)) {
239
            $callback($response);
240
        }
241
242
        return $response;
243
    }
244
245
    /**
246
     * Send request to Telegram API for deleting specified message.
247
     *
248
     * @param  int|string  $chat_id
249
     * @param  int  $message
250
     * @return \Symfony\Component\HttpFoundation\Response
251
     *
252
     * @throws \BadMethodCallException
253
     */
254
    protected function deleteTelegramMessage($chat_id, $message_id)
255
    {
256
        return $this->getBot()->sendRequest('deleteMessage', compact('chat_id', 'message_id'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getBot()->...hat_id', 'message_id')) returns the type BotMan\BotMan\BotMan which is incompatible with the documented return type Symfony\Component\HttpFoundation\Response.
Loading history...
257
    }
258
259
    /**
260
     * Send request to Telegram API for deleting specified message based on the given response.
261
     *
262
     * @param  \Symfony\Component\HttpFoundation\Response  $response
263
     * @return \Symfony\Component\HttpFoundation\Response
264
     */
265
    protected function deleteTelegramMessageFromResponse(Response $response)
266
    {
267
        $responseBody = json_decode($response->getContent(), true);
268
269
        return $this->deleteTelegramMessage(
270
            data_get($responseBody, 'result.chat.id'),
0 ignored issues
show
Bug introduced by
It seems like data_get($responseBody, 'result.chat.id') can also be of type array and array and array and array<mixed,array|mixed|null>; however, parameter $chat_id of App\Conversations\Conver...deleteTelegramMessage() does only seem to accept integer|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

270
            /** @scrutinizer ignore-type */ data_get($responseBody, 'result.chat.id'),
Loading history...
271
            data_get($responseBody, 'result.message_id')
272
        );
273
    }
274
}
275