ogsoft-example/app/Console/Commands/CreateHolidays.php

68 lines
2.1 KiB
PHP
Raw Normal View History

<?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;
}
}