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. */ 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. * * @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded */ 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_street = $offer->address->parts->street ?? null; $offerModel->location_zip = $offer->address->parts->zip ?? null; $offerModel->location_city = $offer->address->parts->city ?? null; $offerModel->location_state = $offer->address->parts->state ?? null; $offerModel->location_country = $offer->address->parts->country ?? 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. */ 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(); } }