]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBotCommands/BaseCommand.php
8cdc0ee61f01020790f5a0aa89a630ced86781ac
[alttp.git] / app / TwitchBotCommands / BaseCommand.php
1 <?php
2
3 namespace App\TwitchBotCommands;
4
5 use App\Models\TwitchBotCommand;
6 use App\Models\User;
7 use App\TwitchBot\TwitchBot;
8
9 abstract class BaseCommand {
10
11         public static function resolve(TwitchBot $bot, TwitchBotCommand $cmd) {
12                 switch ($cmd->command) {
13                         case 'chat':
14                                 return new ChatCommand($bot, $cmd);
15                         case 'join':
16                                 return new JoinCommand($bot, $cmd);
17                         case 'part':
18                                 return new PartCommand($bot, $cmd);
19                         default:
20                                 throw new \Exception('unrecognized command');
21                 }
22         }
23
24         public abstract function execute();
25
26         protected function __construct(TwitchBot $bot, TwitchBotCommand $cmd) {
27                 $this->bot = $bot;
28                 $this->command = $cmd;
29                 if ($cmd->tournament && $cmd->tournament->locale) {
30                         App::setLocale($cmd->tournament->locale);
31                 }
32         }
33
34         protected function getParameter($name) {
35                 return $this->command->parameters[$name];
36         }
37
38         protected function getUser() {
39                 if (!$this->hasParameter('user')) {
40                         throw new \Exception('no user in parameters');
41                 }
42                 return User::findOrFail($this->getParameter('user'));
43         }
44
45         protected function hasParameter($name) {
46                 return array_key_exists($name, $this->command->parameters);
47         }
48
49         protected $bot;
50         protected $command;
51
52 }