Completed
Push — master ( 185853...b7deb5 )
by Marcel
01:56
created

src/Commands/Command.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BotMan\BotMan\Commands;
4
5
use BotMan\BotMan\Closure;
6
use Illuminate\Support\Collection;
7
use BotMan\BotMan\Interfaces\DriverInterface;
8
use BotMan\BotMan\Interfaces\MiddlewareInterface;
9
10
class Command
11
{
12
    /** @var string */
13
    protected $pattern;
14
15
    /** @var Closure|string */
16
    protected $callback;
17
18
    /** @var string */
19
    protected $in;
20
21
    /** @var string */
22
    protected $driver;
23
24
    /** @var array */
25
    protected $recipients;
26
27
    /** @var array */
28
    protected $middleware = [];
29
30
    /** @var bool */
31
    protected $stopsConversation = false;
32
33
    /** @var bool */
34
    protected $skipsConversation = false;
35
36
    /**
37
     * Command constructor.
38
     *
39
     * @param string $pattern
40
     * @param Closure|string $callback
41
     * @param array|null $recipients
42
     * @param string|null $driver
43
     */
44
    public function __construct($pattern, $callback, $recipients = null, $driver = null)
45
    {
46
        $this->pattern = $pattern;
47
        $this->callback = $callback;
48
        $this->driver = $driver;
49
        $this->recipients = $recipients;
0 ignored issues
show
Documentation Bug introduced by
It seems like $recipients can be null. However, the property $recipients is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
50
    }
51
52
    /**
53
     * Apply possible group attributes.
54
     *
55
     * @param  array $attributes
56
     */
57
    public function applyGroupAttributes(array $attributes)
58
    {
59
        if (isset($attributes['middleware'])) {
60
            $this->middleware($attributes['middleware']);
61
        }
62
63
        if (isset($attributes['driver'])) {
64
            $this->driver($attributes['driver']);
65
        }
66
67
        if (isset($attributes['recipient'])) {
68
            $this->recipient($attributes['recipient']);
69
        }
70
71
        if (isset($attributes['stop_conversation']) && $attributes['stop_conversation'] === true) {
72
            $this->stopsConversation();
73
        }
74
75
        if (isset($attributes['skip_conversation']) && $attributes['skip_conversation'] === true) {
76
            $this->skipsConversation();
77
        }
78
    }
79
80
    /**
81
     * @param $driver
82
     * @return $this
83
     */
84
    public function driver($driver)
85
    {
86
        $this->driver = Collection::make($driver)->transform(function ($driver) {
87
            if (class_exists($driver) && is_subclass_of($driver, DriverInterface::class)) {
88
                $driver = basename(str_replace('\\', '/', $driver));
89
                $driver = preg_replace('/(.*)(Driver)$/', '$1', $driver);
90
            }
91
92
            return $driver;
93
        });
94
95
        return $this;
96
    }
97
98
    /**
99
     * With this command a current conversation should be stopped.
100
     */
101
    public function stopsConversation()
102
    {
103
        $this->stopsConversation = true;
104
    }
105
106
    /**
107
     * Tells if a current conversation should be stopped through this command.
108
     *
109
     * @return bool
110
     */
111
    public function shouldStopConversation()
112
    {
113
        return $this->stopsConversation;
114
    }
115
116
    /**
117
     * With this command a current conversation should be skipped.
118
     */
119
    public function skipsConversation()
120
    {
121
        $this->skipsConversation = true;
122
    }
123
124
    /**
125
     * Tells if a current conversation should be skipped through this command.
126
     *
127
     * @return bool
128
     */
129
    public function shouldSkipConversation()
130
    {
131
        return $this->skipsConversation;
132
    }
133
134
    /**
135
     * @param $recipients
136
     * @return $this
137
     */
138
    public function recipient($recipients)
139
    {
140
        $this->recipients = is_array($recipients) ? $recipients : [$recipients];
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param array|MiddlewareInterface $middleware
147
     * @return $this
148
     */
149
    public function middleware($middleware)
150
    {
151
        if (! is_array($middleware)) {
152
            $middleware = [$middleware];
153
        }
154
155
        $this->middleware = Collection::make($middleware)->filter(function ($item) {
156
            return $item instanceof MiddlewareInterface;
157
        })->merge($this->middleware)->toArray();
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public function toArray()
166
    {
167
        return [
168
            'pattern' => $this->pattern,
169
            'callback' => $this->callback,
170
            'driver' => $this->driver,
171
            'middleware' => $this->middleware,
172
            'recipient' => $this->recipients,
173
        ];
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getPattern(): string
180
    {
181
        return $this->pattern;
182
    }
183
184
    /**
185
     * @return Closure|string
186
     */
187
    public function getCallback()
188
    {
189
        return $this->callback;
190
    }
191
192
    /**
193
     * @return array
194
     */
195
    public function getMiddleware(): array
196
    {
197
        return $this->middleware;
198
    }
199
200
    /**
201
     * @return string
202
     */
203
    public function getDriver()
204
    {
205
        return $this->driver;
206
    }
207
208
    /**
209
     * @return array
210
     */
211
    public function getRecipients()
212
    {
213
        return $this->recipients;
214
    }
215
}