diff --git a/app/Console/Commands/AfterworkCommand.php b/app/Console/Commands/AfterworkCommand.php new file mode 100644 index 00000000..fbc0b441 --- /dev/null +++ b/app/Console/Commands/AfterworkCommand.php @@ -0,0 +1,78 @@ +generatePollOptions()) + ->map(fn (string $option, int $index) => $this->addEmoji($option, $index)) + ->implode("\n"); + + SlackAlert::message("Who is in for an afterwork drink? :beer: :cup_with_straw:\n{$options}"); + + $this->info('Poll posted to Slack'); + } + + protected function generatePollOptions(): array + { + $options = []; + + foreach ($this->targetWeekdays as $weekday) { + $datesInMonth = $this->getDatesForWeekdayInMonth($weekday); + + foreach ($datesInMonth as $date) { + $options[$date->day] = "{$date->shortDayName} {$date->day}/{$date->month}"; + } + } + + ksort($options); + + return array_values($options); + } + + protected function addEmoji(string $option, int $index): string + { + $index++; + + $emoji = $this->numberWords()[$index]; + + return ":{$emoji}: {$option}"; + } + + protected function getDatesForWeekdayInMonth(string $dayName): CarbonPeriod + { + return new CarbonPeriod( + CarbonImmutable::parse("first {$dayName} of this month"), + CarbonInterval::week(), + CarbonImmutable::parse("first {$dayName} of next month") + ); + } + + protected function numberWords(): array + { + return [ + 1 => 'one', + 2 => 'two', + 3 => 'three', + 4 => 'four', + 5 => 'five', + 6 => 'six', + 7 => 'seven', + 8 => 'eight', + 9 => 'nine', + 10 => 'beer', // no 10 emoji in Slack + ]; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 6064ddd9..0c5d3fc3 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,6 +2,7 @@ namespace App\Console; +use App\Console\Commands\AfterworkCommand; use App\Console\Commands\ImportDocsFromRepositoriesCommand; use App\Console\Commands\ImportGitHubRepositoriesCommand; use App\Console\Commands\ImportGuideLinesCommand; @@ -43,6 +44,7 @@ protected function schedule(Schedule $schedule): void $schedule->command('geoip:update')->weekly(); $schedule->command(WishHappyBirthdayCommand::class)->runInBackground()->dailyAt('08:50'); + $schedule->command(AfterworkCommand::class)->runInBackground()->monthlyOn(1, '11:00'); } protected function commands(): void