diff --git a/app/Console/Commands/exportCsv.php b/app/Console/Commands/csvExport.php similarity index 53% rename from app/Console/Commands/exportCsv.php rename to app/Console/Commands/csvExport.php index d4e8bc1..26b76dd 100644 --- a/app/Console/Commands/exportCsv.php +++ b/app/Console/Commands/csvExport.php @@ -3,16 +3,18 @@ namespace App\Console\Commands; use App\Http\Controllers\GiteaApiController\GiteaExport; +use DateTime; use Illuminate\Console\Command; +use InvalidArgumentException; -class exportCsv extends Command +class csvExport extends Command { /** * The name and signature of the console command. * * @var string */ - protected $signature = 'csvExport:generate'; + protected $signature = 'csvExport:generate {firstDay?} {lastDay?}'; /** * The console command description. @@ -31,6 +33,32 @@ class exportCsv extends Command parent::__construct(); } + function checkDateValid($stringDate){ + $d = DateTime::createFromFormat('Y-m-d', $stringDate); + if($d){ + $d = $d->format('Y-m-d'); + return $d && $d == $stringDate; + } + return false; + } + + function checkInputDates(){ + $firstDay = $this->argument('firstDay'); + $lastDay = $this->argument('lastDay'); + try{ + if( $firstDay !== null && $lastDay !== null){ + $this->info('Rilevate date richieste, controllo la correttezza del formato.'); + if(!$this->checkDateValid($firstDay)) throw new InvalidArgumentException('Formato della prima data errata o data inesistente, formato richiesto Y-m-t, data fornita '. $firstDay); + if(!$this->checkDateValid($lastDay)) throw new InvalidArgumentException('Formato della seconda data errata o data inesistente, formato richiesto Y-m-t, data fornita '. $lastDay); + $this->info('Le date richieste sono correte.'); + return true; + } + } catch (\InvalidArgumentException $e){ + throw new InvalidArgumentException($e->getMessage()); + } + $this->info('Non sono state rilevate date custom'); + return true; + } /** * Execute the console command. * @@ -40,8 +68,10 @@ class exportCsv extends Command { // Write your script logic here $this->info('Inizio esecuzione.'); - $firstDay = date('Y-m-01'); // First day of the current month - $lastDay = date('Y-m-t'); // Last day of the current month + $this->info('Controllo se sono state inserite date custom.'); + $this->checkInputDates(); + $firstDay = $this->argument('firstDay') ? $this->argument('firstDay') : date('Y-m-01'); // First day of the current month; + $lastDay = $this->argument('lastDay') ? $this->argument('lastDay') : date('Y-m-t'); // Last day of the current month; $this->info('Export da ' . $firstDay .' a '. $lastDay ); $calculate_agent_cost = true; $third_party_integrations_allow = true; diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 42c7f9d..c62f4d2 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,8 +2,7 @@ namespace App\Console; -use App\Console\Commands\exportCsv; -use App\Console\Commands\exportCsvCli; +use App\Console\Commands\csvExport; use App\Http\Controllers\GiteaApiController\GiteaExport; use Dotenv\Dotenv; use Illuminate\Console\Scheduling\Schedule; @@ -17,7 +16,7 @@ class Kernel extends ConsoleKernel * @var array */ protected $commands = [ - exportCsv::class, + csvExport::class, ]; /** @@ -28,43 +27,5 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - $schedule->call(function () { - $firstDay = date('Y-m-01'); // First day of the current month - $lastDay = date('Y-m-t'); // Last day of the current month - - $calculate_agent_cost = true; - $third_party_integrations_allow = true; - $third_party_integrations_nextcloud = true; - $third_party_integrations_open_project = false; - $email_send_allow = true; - - $gitea_export = new GiteaExport( - $calculate_agent_cost, - $third_party_integrations_allow, - $third_party_integrations_nextcloud, - $third_party_integrations_open_project, - $email_send_allow, - ); - $gitea_export->export_issues($firstDay, $lastDay, ['state' => 'closed']); - })->monthly(); - $schedule->call(function () { - $firstDay = date('Y-m-01'); // First day of the current month - $lastDay = date('Y-m-t'); // Last day of the current month - - $calculate_agent_cost = true; - $third_party_integrations_allow = true; - $third_party_integrations_nextcloud = false; - $third_party_integrations_open_project = false; - $email_send_allow = true; - - $gitea_export = new GiteaExport( - $calculate_agent_cost, - $third_party_integrations_allow, - $third_party_integrations_nextcloud, - $third_party_integrations_open_project, - $email_send_allow, - ); - $gitea_export->export_issues($firstDay, $lastDay, ['state' => 'closed']); - })->everyFiveMinutes(); } }