Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After work poll #284

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions app/Console/Commands/AfterworkCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Console\Commands;

use Carbon\CarbonImmutable;
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Illuminate\Console\Command;
use Spatie\SlackAlerts\Facades\SlackAlert;

class AfterworkCommand extends Command
{
protected $signature = 'spatie:after-work';

protected array $targetWeekdays = ['Friday'];

public function handle(): void
{
$options = collect($this->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
];
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down