115 lines
4 KiB
PHP
115 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\Settings\SettingsType;
|
|
use App\Models\GeneralSetting;
|
|
use App\Models\Offer;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Cache;
|
|
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.
|
|
$offerModels = $offers->splice(0, 6)->map(function (object $offer) {
|
|
$offerModel = new Offer();
|
|
$offerModel->title = $offer->title ?? "";
|
|
$offerModel->location = sprintf(
|
|
"%s %s",
|
|
$offer->address->parts->zip ?? "",
|
|
$offer->address->parts->city ?? ""
|
|
);
|
|
$offerModel->contract = $offer->contract_type ?? "";
|
|
$offerModel->function = $offer->service ?? "";
|
|
$offerModel->picture = $offer->pictures[0]->default ?? "";
|
|
$offerModel->picture_alt = $offer->title ?? "";
|
|
$offerModel->picture_title = sprintf("Image de l'offre %s", $offer->title ?? "");
|
|
$offerModel->link = $offer->apply_url ?? "";
|
|
$offerModel->description = strip_tags(clean($offer->description ?? ""));
|
|
$offerModel->published = true;
|
|
$offerModel->publish_at = $offer->published_at;
|
|
$offerModel->saveOrFail();
|
|
return $offerModel;
|
|
});
|
|
// Cache all brand offers.
|
|
Cache::put(config('services.digital_recruiter.url'), $offerModels);
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
// Cache the count of all brand offers.
|
|
Cache::put(sprintf('%s-count', config('services.digital_recruiter.url')), $offers->count());
|
|
}
|
|
}
|