64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\ExceptionDayType;
|
|
use App\Models\Magasin;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class GetPublicHolidayForNextYear implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* Chose a specific year to fetch data
|
|
* @var integer $year
|
|
*/
|
|
protected $year;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param integer|null $year
|
|
*/
|
|
public function __construct($year = null)
|
|
{
|
|
$this->year = is_numeric($year) ? (int)$year : null;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$targetYear = $this->year ?? now()->year + 1;
|
|
$url = "https://calendrier.api.gouv.fr/jours-feries/metropole/{$targetYear}.json";
|
|
$response = Http::get($url);
|
|
if ($response->successful()) {
|
|
$publicHolidays = $response->json();
|
|
$magasins = Magasin::with('collecteExceptions')->get();
|
|
foreach ($magasins as $magasin) {
|
|
foreach ($publicHolidays as $date => $holidayName) {
|
|
$existingException = $magasin->collecteExceptions()->where('date', $date)->first();
|
|
if (!$existingException) {
|
|
$magasin->collecteExceptions()->create([
|
|
'date' => $date,
|
|
'comment' => $holidayName,
|
|
'type' => ExceptionDayType::ferie()
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|