122 lines
4.9 KiB
PHP
122 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\Settings\SettingsType;
|
|
use App\Models\GeneralSetting;
|
|
use App\Models\Offer;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class FetchDigitalRecruiterOffers
|
|
{
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
* @throws \ErrorException If fetching data fails.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
$response = Http::get(config('services.digital_recruiter.url'));
|
|
if (
|
|
$response->clientError() or
|
|
$response->failed() or
|
|
!$response->json() or
|
|
!\is_array($response->json())
|
|
) {
|
|
throw new \ErrorException(sprintf(
|
|
'Failed to fetch data from %s',
|
|
config('services.digital_recruiter.url')
|
|
));
|
|
}
|
|
if (!isset($response->object()->ads)) {
|
|
throw new \ErrorException(sprintf(
|
|
'Object ads is missing in data from %s',
|
|
config('services.digital_recruiter.url')
|
|
));
|
|
}
|
|
$this->parseDigitalRecruiterOffers(\collect($response->object()->ads));
|
|
}
|
|
|
|
/**
|
|
* Parse digital recruiter offers.
|
|
*
|
|
* @param \Illuminate\Support\Collection $digitalRecruiterData
|
|
* @return void
|
|
*/
|
|
private function parseDigitalRecruiterOffers(Collection $digitalRecruiterData): void
|
|
{
|
|
// Remove all current offers.
|
|
Offer::query()->get()->map(function (Offer $offer) {
|
|
$offer->deleteQuietly();
|
|
});
|
|
// Get all public offers of the brand LE GAULOIS.
|
|
$offers = $digitalRecruiterData
|
|
->where('brand.name', "LE GAULOIS")
|
|
->whereNull('internal_apply_url')
|
|
->whereNotNull('apply_url');
|
|
|
|
$this->saveOffersCount($offers);
|
|
$this->saveOffers($offers);
|
|
}
|
|
|
|
/**
|
|
* Save all brand offers in cache and in database.
|
|
*
|
|
* @param \Illuminate\Support\Collection $offers
|
|
* @return void
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
private function saveOffers(Collection $offers): void
|
|
{
|
|
// phpcs:enable
|
|
// Save all brand offers.
|
|
$offers->splice(0, 6)->map(function (object $offer) {
|
|
$offerModel = new Offer();
|
|
$offerModel->title = $offer->title;
|
|
$offerModel->location = $offer->address->formatted ?? null;
|
|
$offerModel->location_details = $offer->entity->around
|
|
? strip_tags(clean($offer->entity->around))
|
|
: null;
|
|
$offerModel->location_latitude = $offer->address->position->lat ?? null;
|
|
$offerModel->location_longitude = $offer->address->position->lon ?? null;
|
|
$offerModel->contract = $offer->contract_type ?? null;
|
|
$offerModel->contract_period = $offer->contract_work_period ?? null;
|
|
$offerModel->experience_level = $offer->experience_level || $offer->education_level
|
|
? sprintf("%s - %s", $offer->experience_level ?? "", $offer->education_level ?? "")
|
|
: null;
|
|
$offerModel->qualifications = $offer->profile ? strip_tags(clean($offer->profile)) : null;
|
|
$offerModel->skills = $offer->skills ?? null;
|
|
$offerModel->function = $offer->service ?? null;
|
|
$offerModel->picture = $offer->pictures[0]->default ?? null;
|
|
$offerModel->picture_alt = $offer->title;
|
|
$offerModel->picture_title = sprintf("Image de l'offre %s", $offer->title);
|
|
$offerModel->link = $offer->apply_url ?? null;
|
|
$offerModel->description = $offer->profile ? strip_tags(clean($offer->profile)) : null;
|
|
$offerModel->recruiter_firstname = $offer->entity->manager->firstname ?? null;
|
|
$offerModel->recruiter_lastname = $offer->entity->manager->lastname ?? null;
|
|
$offerModel->recruiter_job = $offer->entity->manager->position ?? null;
|
|
$offerModel->recruiter_picture = $offer->entity->manager->picture_url ?? null;
|
|
$offerModel->published = true;
|
|
$offerModel->published_at = Carbon::parse($offer->published_at);
|
|
$offerModel->saveOrFail();
|
|
return $offerModel;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Save the count of all brand offers in cache and in database.
|
|
*
|
|
* @param \Illuminate\Support\Collection $offers
|
|
* @return void
|
|
*/
|
|
private function saveOffersCount(Collection $offers): void
|
|
{
|
|
// Save the count of all brand offers.
|
|
$generalSettingModel = GeneralSetting::query()->where('type', SettingsType::offersCount)->firstOrFail();
|
|
$generalSettingModel->value = ['count' => $offers->count()];
|
|
$generalSettingModel->saveOrFail();
|
|
}
|
|
}
|