Completed
Push — master ( 0f03ab...a6f21d )
by Marcel
02:12
created

Conversation::getBot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace BotMan\BotMan\Messages\Conversations;
4
5
use Closure;
6
use BotMan\BotMan\BotMan;
7
use Spatie\Macroable\Macroable;
8
use Illuminate\Support\Collection;
9
use BotMan\BotMan\Interfaces\ShouldQueue;
10
use BotMan\BotMan\Messages\Attachments\Audio;
11
use BotMan\BotMan\Messages\Attachments\Image;
12
use BotMan\BotMan\Messages\Attachments\Video;
13
use BotMan\BotMan\Messages\Outgoing\Question;
14
use BotMan\BotMan\Messages\Attachments\Location;
15
use BotMan\BotMan\Messages\Incoming\IncomingMessage;
16
17
/**
18
 * Class Conversation.
19
 */
20
abstract class Conversation
21
{
22
    use Macroable;
23
24
    /**
25
     * @var BotMan
26
     */
27
    protected $bot;
28
29
    /**
30
     * @var string
31
     */
32
    protected $token;
33
34
    /**
35
     * Number of minutes this specific conversation should be cached.
36
     * @var int
37
     */
38
    protected $cacheTime;
39
40
    /**
41
     * @param BotMan $bot
42
     */
43
    public function setBot(BotMan $bot)
44
    {
45
        $this->bot = $bot;
46
    }
47
48
    /**
49
     * @return BotMan
50
     */
51
    public function getBot()
52
    {
53
        return $this->bot;
54
    }
55
56
    /**
57
     * @param string|Question $question
58
     * @param array|Closure $next
59
     * @param array $additionalParameters
60
     * @return $this
61
     */
62
    public function ask($question, $next, $additionalParameters = [])
63
    {
64
        $this->bot->reply($question, $additionalParameters);
65
        $this->bot->storeConversation($this, $next, $question, $additionalParameters);
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param string|\BotMan\BotMan\Messages\Outgoing\Question $question
72
     * @param array|Closure $next
73
     * @param array|Closure $repeat
74
     * @param array $additionalParameters
75
     * @return $this
76
     */
77 View Code Duplication
    public function askForImages($question, $next, $repeat = null, $additionalParameters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $additionalParameters['__getter'] = 'getImages';
80
        $additionalParameters['__pattern'] = Image::PATTERN;
81
        $additionalParameters['__repeat'] = ! is_null($repeat) ? $this->bot->serializeClosure($repeat) : $repeat;
0 ignored issues
show
Bug introduced by
It seems like $repeat defined by parameter $repeat on line 77 can also be of type array; however, BotMan\BotMan\Traits\Han...ons::serializeClosure() does only seem to accept object<Closure>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
82
83
        return $this->ask($question, $next, $additionalParameters);
84
    }
85
86
    /**
87
     * @param string|\BotMan\BotMan\Messages\Outgoing\Question $question
88
     * @param array|Closure $next
89
     * @param array|Closure $repeat
90
     * @param array $additionalParameters
91
     * @return $this
92
     */
93 View Code Duplication
    public function askForVideos($question, $next, $repeat = null, $additionalParameters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $additionalParameters['__getter'] = 'getVideos';
96
        $additionalParameters['__pattern'] = Video::PATTERN;
97
        $additionalParameters['__repeat'] = ! is_null($repeat) ? $this->bot->serializeClosure($repeat) : $repeat;
0 ignored issues
show
Bug introduced by
It seems like $repeat defined by parameter $repeat on line 93 can also be of type array; however, BotMan\BotMan\Traits\Han...ons::serializeClosure() does only seem to accept object<Closure>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
98
99
        return $this->ask($question, $next, $additionalParameters);
100
    }
101
102
    /**
103
     * @param string|\BotMan\BotMan\Messages\Outgoing\Question $question
104
     * @param array|Closure $next
105
     * @param array|Closure $repeat
106
     * @param array $additionalParameters
107
     * @return $this
108
     */
109 View Code Duplication
    public function askForAudio($question, $next, $repeat = null, $additionalParameters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $additionalParameters['__getter'] = 'getAudio';
112
        $additionalParameters['__pattern'] = Audio::PATTERN;
113
        $additionalParameters['__repeat'] = ! is_null($repeat) ? $this->bot->serializeClosure($repeat) : $repeat;
0 ignored issues
show
Bug introduced by
It seems like $repeat defined by parameter $repeat on line 109 can also be of type array; however, BotMan\BotMan\Traits\Han...ons::serializeClosure() does only seem to accept object<Closure>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
114
115
        return $this->ask($question, $next, $additionalParameters);
116
    }
117
118
    /**
119
     * @param string|\BotMan\BotMan\Messages\Outgoing\Question $question
120
     * @param array|Closure $next
121
     * @param array|Closure $repeat
122
     * @param array $additionalParameters
123
     * @return $this
124
     */
125 View Code Duplication
    public function askForLocation($question, $next, $repeat = null, $additionalParameters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $additionalParameters['__getter'] = 'getLocation';
128
        $additionalParameters['__pattern'] = Location::PATTERN;
129
        $additionalParameters['__repeat'] = ! is_null($repeat) ? $this->bot->serializeClosure($repeat) : $repeat;
0 ignored issues
show
Bug introduced by
It seems like $repeat defined by parameter $repeat on line 125 can also be of type array; however, BotMan\BotMan\Traits\Han...ons::serializeClosure() does only seem to accept object<Closure>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
130
131
        return $this->ask($question, $next, $additionalParameters);
132
    }
133
134
    /**
135
     * Repeat the previously asked question.
136
     * @param string|Question $question
137
     */
138
    public function repeat($question = '')
139
    {
140
        $conversation = $this->bot->getStoredConversation();
141
142
        if (! $question instanceof Question && ! $question) {
143
            $question = unserialize($conversation['question']);
144
        }
145
146
        $next = $conversation['next'];
147
        $additionalParameters = unserialize($conversation['additionalParameters']);
148
149
        if (is_string($next)) {
150
            $next = unserialize($next)->getClosure();
151
        } elseif (is_array($next)) {
152
            $next = Collection::make($next)->map(function ($callback) {
153
                if ($this->bot->getDriver()->serializesCallbacks() && ! $this->bot->runsOnSocket()) {
154
                    $callback['callback'] = unserialize($callback['callback'])->getClosure();
155
                }
156
157
                return $callback;
158
            })->toArray();
159
        }
160
        $this->ask($question, $next, $additionalParameters);
161
    }
162
163
    /**
164
     * @param string|\BotMan\BotMan\Messages\Outgoing\Question $message
165
     * @param array $additionalParameters
166
     * @return $this
167
     */
168
    public function say($message, $additionalParameters = [])
169
    {
170
        $this->bot->reply($message, $additionalParameters);
171
172
        return $this;
173
    }
174
175
    /**
176
     * Should the conversation be skipped (temporarily).
177
     * @param  IncomingMessage $message
178
     * @return bool
179
     */
180
    public function skipsConversation(IncomingMessage $message)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
181
    {
182
        //
183
    }
184
185
    /**
186
     * Should the conversation be removed and stopped (permanently).
187
     * @param  IncomingMessage $message
188
     * @return bool
189
     */
190
    public function stopsConversation(IncomingMessage $message)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
191
    {
192
        //
193
    }
194
195
    /**
196
     * Override default conversation cache time (only for this conversation).
197
     * @return mixed
198
     */
199
    public function getConversationCacheTime()
200
    {
201
        return $this->cacheTime ?? null;
202
    }
203
204
    /**
205
     * @return mixed
206
     */
207
    abstract public function run();
208
209
    /**
210
     * @return array
211
     */
212
    public function __sleep()
213
    {
214
        $properties = get_object_vars($this);
215
        if (! $this instanceof ShouldQueue) {
216
            unset($properties['bot']);
217
        }
218
219
        return array_keys($properties);
220
    }
221
}
222