feat(commands): add command for generating holidays in countries
This commit is contained in:
parent
1e8f4983be
commit
b2de1fa6c2
67
app/Console/Commands/CreateHolidays.php
Normal file
67
app/Console/Commands/CreateHolidays.php
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user