feat(commands): add command for generating holidays in countries

This commit is contained in:
Ondrej Vlach 2024-08-07 13:01:53 +02:00
parent 1e8f4983be
commit b2de1fa6c2
No known key found for this signature in database
GPG Key ID: 7F141CDACEDEE2DE

View File

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Services\WorkingDays\PublicHolidays\PublicHolidaysGeneratorFactory;
use App\Services\WorkingDays\PublicHolidays\PublicHolidaysStateFactory;
use Illuminate\Console\Command;
class CreateHolidays extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:create-holidays {country-code} {year}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates public holidays for given year and country code';
/**
* Execute the console command.
*/
public function handle(PublicHolidaysGeneratorFactory $publicHolidaysGeneratorFactory, PublicHolidaysStateFactory $publicHolidaysStateFactory): int
{
$countryCodeStr = $this->argument('country-code');
$year = $this->argument('year');
if (!is_string($countryCodeStr) || strlen($countryCodeStr) !== 2) {
$this->error('Invalid country code. Must be two-letter code');
return 1;
}
if (!is_numeric($year)) {
$this->error('Year must be a numeric value');
return 1;
}
$countryCode = strtoupper($countryCodeStr);
$publicHolidaysStore = $publicHolidaysStateFactory->createStoreForCountry($countryCode);
$this->info('Creating public holidays for country code: ' . $countryCodeStr . ' and year: ' . $year);
try {
$publicHolidaysGenerator = $publicHolidaysGeneratorFactory->createPublicHolidaysGeneratorForCountry($countryCode);
} catch (\RuntimeException $e) {
$this->error($e->getMessage());
return 1;
}
$result = $publicHolidaysGenerator->generatePublicHolidaysForYear((int) $year);
foreach ($result as $nwdDate) {
$publicHolidaysStore->storePublicHoliday($nwdDate);
}
$this->info('[DONE] Creating public holidays for country code: ' . $countryCode . ' and year: ' . $year);
return 0;
}
}