|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* T3Bot. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Frank Nägler <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @link http://www.t3bot.de |
|
8
|
|
|
* @link http://wiki.typo3.org/T3Bot |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace T3Bot\Commands; |
|
11
|
|
|
|
|
12
|
|
|
use Slack\Payload; |
|
13
|
|
|
use Slack\RealTimeClient; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class UtilCommand. |
|
17
|
|
|
* |
|
18
|
|
|
* @property string commandName |
|
19
|
|
|
* @property array helpCommands |
|
20
|
|
|
*/ |
|
21
|
|
|
class UtilCommand extends AbstractCommand |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* AbstractCommand constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Payload $payload |
|
27
|
|
|
* @param RealTimeClient $client |
|
28
|
|
|
*/ |
|
29
|
|
View Code Duplication |
public function __construct(Payload $payload, RealTimeClient $client) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$this->commandName = 'util'; |
|
32
|
|
|
$this->helpCommands = [ |
|
33
|
|
|
'help' => 'shows this help', |
|
34
|
|
|
'coin [options]' => 'coin toss with [options] (separate by comma)', |
|
35
|
|
|
]; |
|
36
|
|
|
parent::__construct($payload, $client); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function processCoin() : string |
|
43
|
|
|
{ |
|
44
|
|
|
$params = $this->params; |
|
45
|
|
|
array_shift($params); |
|
46
|
|
|
$params = implode(' ', $params); |
|
47
|
|
|
$options = array_map('trim', explode(',', $params)); |
|
48
|
|
|
if (count($options) === 1) { |
|
49
|
|
|
return '*Botty says:* _A complicated decision ..._'; |
|
50
|
|
|
} |
|
51
|
|
|
if (count(array_count_values($options)) === 1) { |
|
52
|
|
|
return '*Botty says:* _it is undecidable ..._'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$option = $options[random_int(0, count($options) - 1)]; |
|
56
|
|
|
|
|
57
|
|
|
return '*Botty says:* _' . $option . '_'; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
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.