File: D:/HostingSpaces/SBogers10/ste.komma.pro/app/Console/Commands/GetTrainingsFromC4.php
<?php
namespace App\Console\Commands;
use App\C4\C4;
use App\Locations\LocationService;
use App\SteLanguages\SteLanguageService;
use App\Trainings\Resources\Training;
use App\Trainings\TrainingService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
class GetTrainingsFromC4 extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'training:import';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Get the trainings from the c4 API';
/**
* Simple API hook for C4
*
* @var C4
*/
private C4 $c4;
private SteLanguageService $steLanguageService;
private TrainingService $trainingService;
private LocationService $locationService;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->c4 = app(C4::class);
$this->steLanguageService = app(SteLanguageService::class);
$this->trainingService = app(TrainingService::class);
$this->locationService = app(LocationService::class);
}
/**
* Execute the console command.
*
* @param bool $runFromCommand
*/
public function handle(bool $runFromCommand = true)
{
$apiTrainings = $this->c4->getTrainings();
if($runFromCommand) $this->info('C4 import: ' . sizeof($apiTrainings) . ' trainings have been found.');
$trainings = collect();
foreach ($apiTrainings as $apiTraining)
{
if($apiTraining->status == 'Annuleren (instituut)') continue;
if($apiTraining->niveau == '') continue;
// Grab the language out of the STE language. If not present throw warning error and continue
if(!$language = $this->steLanguageService->getSteLanguageByC4Code($apiTraining->taal_code)) {
$warningMessage = 'C4 import: Language "' . $apiTraining->taal_code . '" provided by C4 has not been defined to the available STE Languages in the site or we should remap them to one of these..';
\Log::warning($warningMessage);
if($runFromCommand) $this->warn($warningMessage);
continue;
}
// Validate the location is previous is mapped to one of our locations
if(!$location = $this->locationService->getLocationByC4Code($apiTraining->locatie)){
$warningMessage = 'C4 import: Location "' . $apiTraining->locatie . '" provided by C4 has not been mapped to one of our locations.';
\Log::warning($warningMessage);
if($runFromCommand) $this->warn($warningMessage);
continue;
}
// $training = $this->trainingService->firstOrNew($language, strtoupper($trainingLevel));
$training = new Training();
$training->enrich($apiTraining);
$training->location = $location;
$training->location_id = $location->id;
$training->language = $language;
$training->language_id = $language->id;
$trainings->push($training);
}
$message = 'C4 import: ' . $trainings->count() . ' added to the cache.';
if($runFromCommand) $this->info($message);
\Log::info($message);
Cache::put('trainings', $trainings, Carbon::now()->addMinutes(60));
}
}