]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBotCommands/BaseCommand.php
basic twitch join/part commands
[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 'join':
14                                 return new JoinCommand($bot, $cmd);
15                         case 'part':
16                                 return new PartCommand($bot, $cmd);
17                         default:
18                                 throw new Exception('unrecognized command');
19                 }
20         }
21
22         public abstract function execute();
23
24         protected function __construct(TwitchBot $bot, TwitchBotCommand $cmd) {
25                 $this->bot = $bot;
26                 $this->command = $cmd;
27                 if ($cmd->tournament && $cmd->tournament->locale) {
28                         App::setLocale($cmd->tournament->locale);
29                 }
30         }
31
32         protected function getParameter($name) {
33                 return $this->command->parameters[$name];
34         }
35
36         protected function getUser() {
37                 if (!$this->hasParameter('user')) {
38                         throw new \Exception('no user in parameters');
39                 }
40                 return User::findOrFail($this->getParameter('user'));
41         }
42
43         protected function hasParameter($name) {
44                 return array_key_exists($name, $this->command->parameters);
45         }
46
47         protected $bot;
48         protected $command;
49
50 }