Commander   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 503
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 321
dl 0
loc 503
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 3 1
A __construct() 0 4 1
B execute() 0 30 8
A setToken() 0 3 1
1
<?php namespace Slacky\Core;
2
3
use InvalidArgumentException;
4
use Slacky\Contracts\Http\Interactor;
5
6
class Commander
7
{
8
9
    /**
10
     * The default command headers.
11
     *
12
     * @var array
13
     */
14
    protected static $defaultHeaders = [];
15
16
    /**
17
     * The commands.
18
     *
19
     * @var array
20
     */
21
    protected static $commands = [
22
        'api.test'                => [
23
            'endpoint' => '/api.test',
24
            'token'    => false
25
        ],
26
        'auth.test'               => [
27
            'endpoint' => '/auth.test',
28
            'token'    => true
29
        ],
30
        'channels.archive'        => [
31
            'token'    => true,
32
            'endpoint' => '/channels.archive'
33
        ],
34
        'channels.create'         => [
35
            'token'    => true,
36
            'endpoint' => '/channels.create'
37
        ],
38
        'channels.history'        => [
39
            'token'    => true,
40
            'endpoint' => '/channels.history'
41
        ],
42
        'channels.info'           => [
43
            'token'    => true,
44
            'endpoint' => '/channels.info'
45
        ],
46
        'channels.invite'         => [
47
            'token'    => true,
48
            'endpoint' => '/channels.invite'
49
        ],
50
        'channels.join'           => [
51
            'token'    => true,
52
            'endpoint' => '/channels.join'
53
        ],
54
        'channels.kick'           => [
55
            'token'    => true,
56
            'endpoint' => '/channels.kick'
57
        ],
58
        'channels.leave'          => [
59
            'token'    => true,
60
            'endpoint' => '/channels.leave'
61
        ],
62
        'channels.list'           => [
63
            'token'    => true,
64
            'endpoint' => '/channels.list'
65
        ],
66
        'channels.mark'           => [
67
            'token'    => true,
68
            'endpoint' => '/channels.mark'
69
        ],
70
        'channels.rename'         => [
71
            'token'    => true,
72
            'endpoint' => '/channels.rename'
73
        ],
74
        'channels.setPurpose'     => [
75
            'token'    => true,
76
            'endpoint' => '/channels.setPurpose',
77
            'format'   => [
78
                'purpose'
79
            ]
80
        ],
81
        'channels.setTopic'       => [
82
            'token'    => true,
83
            'endpoint' => '/channels.setTopic',
84
            'format'   => [
85
                'topic'
86
            ]
87
        ],
88
        'channels.unarchive'      => [
89
            'token'    => true,
90
            'endpoint' => '/channels.unarchive'
91
        ],
92
        'chat.delete'             => [
93
            'token'    => true,
94
            'endpoint' => '/chat.delete'
95
        ],
96
        'chat.postMessage'        => [
97
            'token'    => true,
98
            'endpoint' => '/chat.postMessage',
99
            'format'   => [
100
                'text',
101
                'username'
102
            ]
103
        ],
104
        'chat.update'             => [
105
            'token'    => true,
106
            'endpoint' => '/chat.update',
107
            'format'   => [
108
                'text'
109
            ]
110
        ],
111
        'dnd.endDnd'              => [
112
            'token'    => true,
113
            'endpoint' => '/dnd.endDnd'
114
        ],
115
        'dnd.endSnooze'           => [
116
            'token'    => true,
117
            'endpoint' => '/dnd.endSnooze'
118
        ],
119
        'dnd.info'                => [
120
            'token'    => true,
121
            'endpoint' => '/dnd.info'
122
        ],
123
        'dnd.setSnooze'           => [
124
            'token'    => true,
125
            'endpoint' => '/dnd.setSnooze'
126
        ],
127
        'dnd.teamInfo'            => [
128
            'token'    => true,
129
            'endpoint' => '/dnd.teamInfo'
130
        ],
131
        'emoji.list'              => [
132
            'token'    => true,
133
            'endpoint' => '/emoji.list'
134
        ],
135
        'files.comments.add'      => [
136
            'token'    => true,
137
            'endpoint' => '/files.comments.add'
138
        ],
139
        'files.comments.delete'   => [
140
            'token'    => true,
141
            'endpoint' => '/files.comments.delete'
142
        ],
143
        'files.comments.edit'     => [
144
            'token'    => true,
145
            'endpoint' => '/files.comments.edit'
146
        ],
147
        'files.delete'            => [
148
            'token'    => true,
149
            'endpoint' => '/files.delete'
150
        ],
151
        'files.info'              => [
152
            'token'    => true,
153
            'endpoint' => '/files.info'
154
        ],
155
        'files.list'              => [
156
            'token'    => true,
157
            'endpoint' => '/files.list'
158
        ],
159
        'files.revokePublicURL'   => [
160
            'token'    => true,
161
            'endpoint' => '/files.revokePublicURL'
162
        ],
163
        'files.sharedPublcURL'    => [
164
            'token'    => true,
165
            'endpoint' => '/files.sharedPublcURL'
166
        ],
167
        'files.upload'            => [
168
            'token'    => true,
169
            'endpoint' => '/files.upload',
170
            'post'     => true,
171
            'headers'  => [
172
                'Content-Type' => 'multipart/form-data'
173
            ],
174
            'format'   => [
175
                'filename',
176
                'title',
177
                'initial_comment'
178
            ]
179
        ],
180
        'groups.archive'          => [
181
            'token'    => true,
182
            'endpoint' => '/groups.archive'
183
        ],
184
        'groups.close'            => [
185
            'token'    => true,
186
            'endpoint' => '/groups.close'
187
        ],
188
        'groups.create'           => [
189
            'token'    => true,
190
            'endpoint' => '/groups.create',
191
            'format'   => [
192
                'name'
193
            ]
194
        ],
195
        'groups.createChild'      => [
196
            'token'    => true,
197
            'endpoint' => '/groups.createChild'
198
        ],
199
        'groups.history'          => [
200
            'token'    => true,
201
            'endpoint' => '/groups.history'
202
        ],
203
        'groups.info'             => [
204
            'token'    => true,
205
            'endpoint' => '/groups.info'
206
        ],
207
        'groups.invite'           => [
208
            'token'    => true,
209
            'endpoint' => '/groups.invite'
210
        ],
211
        'groups.kick'             => [
212
            'token'    => true,
213
            'endpoint' => '/groups.kick'
214
        ],
215
        'groups.leave'            => [
216
            'token'    => true,
217
            'endpoint' => '/groups.leave'
218
        ],
219
        'groups.list'             => [
220
            'token'    => true,
221
            'endpoint' => '/groups.list'
222
        ],
223
        'groups.mark'             => [
224
            'token'    => true,
225
            'endpoint' => '/groups.mark'
226
        ],
227
        'groups.open'             => [
228
            'token'    => true,
229
            'endpoint' => '/groups.open'
230
        ],
231
        'groups.rename'           => [
232
            'token'    => true,
233
            'endpoint' => '/groups.rename'
234
        ],
235
        'groups.setPurpose'       => [
236
            'token'    => true,
237
            'endpoint' => '/groups.setPurpose',
238
            'format'   => [
239
                'purpose'
240
            ]
241
        ],
242
        'groups.setTopic'         => [
243
            'token'    => true,
244
            'endpoint' => '/groups.setTopic',
245
            'format'   => [
246
                'topic'
247
            ]
248
        ],
249
        'groups.unarchive'        => [
250
            'token'    => true,
251
            'endpoint' => '/groups.unarchive'
252
        ],
253
        'im.close'                => [
254
            'token'    => true,
255
            'endpoint' => '/im.close'
256
        ],
257
        'im.history'              => [
258
            'token'    => true,
259
            'endpoint' => '/im.history'
260
        ],
261
        'im.list'                 => [
262
            'token'    => true,
263
            'endpoint' => '/im.list'
264
        ],
265
        'im.mark'                 => [
266
            'token'    => true,
267
            'endpoint' => '/im.mark'
268
        ],
269
        'im.open'                 => [
270
            'token'    => true,
271
            'endpoint' => '/im.open'
272
        ],
273
        'mpim.close'              => [
274
            'token'    => true,
275
            'endpoint' => '/mpim.close'
276
        ],
277
        'mpmim.history'           => [
278
            'token'    => true,
279
            'endpoint' => '/mpmim.history'
280
        ],
281
        'mpim.list'               => [
282
            'token'    => true,
283
            'endpoint' => '/mpim.list'
284
        ],
285
        'mpim.mark'               => [
286
            'token'    => true,
287
            'endpoint' => '/mpim.mark'
288
        ],
289
        'mpim.open'               => [
290
            'token'    => true,
291
            'endpoint' => '/mpim.open'
292
        ],
293
        'oauth.access'            => [
294
            'token'    => false,
295
            'endpoint' => '/oauth.access'
296
        ],
297
        'pins.add'                => [
298
            'token'    => true,
299
            'endpoint' => '/pins.add'
300
        ],
301
        'pins.list'               => [
302
            'token'    => true,
303
            'endpoint' => '/pins.list'
304
        ],
305
        'pins.remove'             => [
306
            'token'    => true,
307
            'endpoint' => '/pins.remove'
308
        ],
309
        'reactions.add'           => [
310
            'token'    => true,
311
            'endpoint' => '/reactions.add'
312
        ],
313
        'reactions.get'           => [
314
            'token'    => true,
315
            'endpoint' => '/reactions.get'
316
        ],
317
        'reactions.list'          => [
318
            'token'    => true,
319
            'endpoint' => '/reactions.list'
320
        ],
321
        'reactions.remove'        => [
322
            'token'    => true,
323
            'endpoint' => '/reactions.remove'
324
        ],
325
        'rtm.start'               => [
326
            'token'    => true,
327
            'endpoint' => '/rtm.start'
328
        ],
329
        'search.all'              => [
330
            'token'    => true,
331
            'endpoint' => '/search.all'
332
        ],
333
        'search.files'            => [
334
            'token'    => true,
335
            'endpoint' => '/search.files'
336
        ],
337
        'search.messages'         => [
338
            'token'    => true,
339
            'endpoint' => '/search.messages'
340
        ],
341
        'stars.add'               => [
342
            'token'    => true,
343
            'endpoint' => '/stars.add'
344
        ],
345
        'stars.list'              => [
346
            'token'    => true,
347
            'endpoint' => '/stars.list'
348
        ],
349
        'stars.remove'            => [
350
            'token'    => true,
351
            'endpoint' => '/stars.remove'
352
        ],
353
        'team.accessLogs'         => [
354
            'token'    => true,
355
            'endpoint' => '/team.accessLogs'
356
        ],
357
        'team.info'               => [
358
            'token'    => true,
359
            'endpoint' => '/team.info'
360
        ],
361
        'team.integrationLogs'    => [
362
            'token'    => true,
363
            'endpoint' => '/team.integrationLogs'
364
        ],
365
        'usergroups.create'       => [
366
            'token'    => true,
367
            'endpoint' => '/usergroups.create'
368
        ],
369
        'usergroups.disable'      => [
370
            'token'    => true,
371
            'endpoint' => '/usergroups.disable'
372
        ],
373
        'usergroups.enable'       => [
374
            'token'    => true,
375
            'endpoint' => '/usergroups.enable'
376
        ],
377
        'usergroups.list'         => [
378
            'token'    => true,
379
            'endpoint' => '/usergroups.list'
380
        ],
381
        'usergroups.update'       => [
382
            'token'    => true,
383
            'endpoint' => '/usergroups.update'
384
        ],
385
        'usergroups.users.list'   => [
386
            'token'    => true,
387
            'endpoint' => '/usergroups.users.list'
388
        ],
389
        'usergroups.users.update' => [
390
            'token'    => true,
391
            'endpoint' => '/usergroups.users.update'
392
        ],
393
        'users.getPresence'       => [
394
            'token'    => true,
395
            'endpoint' => '/users.getPresence'
396
        ],
397
        'users.info'              => [
398
            'token'    => true,
399
            'endpoint' => '/users.info'
400
        ],
401
        'users.list'              => [
402
            'token'    => true,
403
            'endpoint' => '/users.list'
404
        ],
405
        'users.setActive'         => [
406
            'token'    => true,
407
            'endpoint' => '/users.setActive'
408
        ],
409
        'users.setPresence'       => [
410
            'token'    => true,
411
            'endpoint' => '/users.setPresence'
412
        ],
413
        'users.admin.invite'      => [
414
            'token'    => true,
415
            'endpoint' => '/users.admin.invite'
416
        ]
417
    ];
418
419
    /**
420
     * The base URL.
421
     *
422
     * @var string
423
     */
424
    protected static $baseUrl = 'https://slack.com/api';
425
426
    /**
427
     * The API token.
428
     *
429
     * @var string
430
     */
431
    protected $token;
432
433
    /**
434
     * The Http interactor.
435
     *
436
     * @var \Slacky\Contracts\Http\Interactor
437
     */
438
    protected $interactor;
439
440
    /**
441
     * @param string $token
442
     * @param \Slacky\Contracts\Http\Interactor $interactor
443
     */
444
    public function __construct($token, Interactor $interactor)
445
    {
446
        $this->token      = $token;
447
        $this->interactor = $interactor;
448
    }
449
450
    /**
451
     * Executes a command.
452
     *
453
     * @param  string $command
454
     * @param  array $parameters
455
     * @return \Slacky\Contracts\Http\Response
456
     * @throws \InvalidArgumentException
457
     */
458
    public function execute($command, array $parameters = [])
459
    {
460
        if (!isset(self::$commands[$command])) {
461
            throw new InvalidArgumentException("The command '{$command}' is not currently supported");
462
        }
463
464
        $command = self::$commands[$command];
465
        if (!empty($command['token'])) {
466
            $parameters = array_merge($parameters, ['token' => $this->token]);
467
        }
468
469
        if (isset($command['format'])) {
470
            foreach ($command['format'] as $format) {
471
                if (isset($parameters[$format])) {
472
                    $parameters[$format] = self::format($parameters[$format]);
473
                }
474
            }
475
        }
476
477
        $headers = [];
478
        if (isset($command['headers']))
479
            $headers = $command['headers'];
480
481
        $url = self::$baseUrl . $command['endpoint'];
482
483
        if (!empty($command['post'])) {
484
            return $this->interactor->post($url, [], $parameters, $headers);
0 ignored issues
show
Unused Code introduced by
The call to Slacky\Contracts\Http\Interactor::post() has too many arguments starting with $headers. ( Ignorable by Annotation )

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

484
            return $this->interactor->/** @scrutinizer ignore-call */ post($url, [], $parameters, $headers);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
485
        }
486
487
        return $this->interactor->get($url, $parameters, $headers);
488
    }
489
490
    /**
491
     * Sets the token.
492
     *
493
     * @param string $token
494
     */
495
    public function setToken(string $token) : void
496
    {
497
        $this->token = $token;
498
    }
499
500
    /**
501
     * Formats a string for Slack.
502
     *
503
     * @param  string $str
504
     * @return string
505
     */
506
    public static function format(string $str) : string
507
    {
508
        return str_replace(['&', '<', '>'], ['&amp;', '&lt;', '&gt;'], $str);
509
    }
510
511
}
512