*/ protected $fillable = [ 'title', 'location_street', 'location_zip', 'location_city', 'location_state', 'location_country', 'location_details', 'location_latitude', 'location_longitude', 'contract', 'contract_period', 'function', 'experience_level', 'qualifications', 'skills', 'picture', 'picture_alt', 'picture_title', 'link', 'description', 'recruiter_firstname', 'recruiter_lastname', 'recruiter_job', 'recruiter_picture', 'published', 'published_at', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'skills' => 'array', 'published' => 'boolean', 'published_at' => 'datetime', ]; /** * Perform any actions required after the model boots. */ protected static function booted(): void { static::creating(function (self $offer) { $offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true); $offer->recruiter_picture = FileStorageHelper::storeFile($offer, $offer->recruiter_picture, true); }); static::created(function () { self::resetCache(); }); static::updating(function (self $offer) { $offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true); $offer->recruiter_picture = FileStorageHelper::storeFile($offer, $offer->recruiter_picture, true); }); static::updated(function (self $offer) { FileStorageHelper::removeOldFile($offer, 'picture'); FileStorageHelper::removeOldFile($offer, 'recruiter_picture'); self::resetCache(); }); static::deleted(function (self $offer) { FileStorageHelper::removeFile($offer, 'picture'); FileStorageHelper::removeFile($offer, 'recruiter_picture'); self::resetCache(); }); } /** * Reset all cache elements related to this model. */ private static function resetCache(): void { Cache::forget('offersCached'); Cache::forget('offersCountCached'); self::getOffersCached(); self::getOffersCountCached(); } /** * Get all offers. */ public static function getOffersCached(): Collection { return Cache::remember('offersCached', 86400, function () { return self::query()->where('published', true)->get(); }); } /** * Get offers count. */ public static function getOffersCountCached(): int { return Cache::remember('offersCountCached', 86400, function () { return GeneralSetting::query()->where('type', SettingsType::offersCount)->firstOrFail()->value['count']; }); } /** * Get formatted address location. */ public function getFormattedAddressAttribute(): string { return trim(sprintf( '%s %s %s %s', $this->location_street ?? '', $this->location_zip ?? '', $this->location_city ?? '', $this->location_country ?? '' )); } /** * Create a SchemaOrg. * * @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh */ public function toSchemaOrg(): JobPosting { $schemaJob = Schema::jobPosting() ->title($this->title) ->hiringOrganization($this->getOrganizationSchema()); // Recruiter. $recruiter = Schema::contactPoint(); if (! empty($this->recruiter_firstname) || ! empty($this->recruiter_lastname)) { $recruiter->name(sprintf( '%s %s', $this->recruiter_firstname ?? '', $this->recruiter_lastname ?? '', )); } if (! empty($this->recruiter_job)) { $recruiter->contactType($this->recruiter_job); } if (! empty($this->recruiter_picture)) { $recruiter->image( Schema::imageObject() ->url(asset($this->recruiter_picture)) ->headline(sprintf( 'Photo du recruteur %s %s', $this->recruiter_firstname ?? '', $this->recruiter_lastname ?? '', )) ); $schemaJob->applicationContact($recruiter); } // Location. $location = Schema::place() ->isAccessibleForFree(true) ->publicAccess(true); if (! empty($this->location_longitude)) { $location->longitude($this->location_longitude); } if (! empty($this->location_latitude)) { $location->latitude($this->location_latitude); } // Address location. $fields = [ 'streetAddress' => $this->location_street, 'postalCode' => $this->location_zip, 'addressLocality' => $this->location_city, 'addressRegion' => $this->location_state, 'addressCountry' => $this->location_country, ]; $hasAddress = array_filter($fields); if (! empty($hasAddress)) { $postalAddress = Schema::postalAddress(); foreach ($hasAddress as $method => $value) { $postalAddress->$method($value); } $location->address($postalAddress); } if (! empty($this->location_details)) { $location->description($this->location_details); } $schemaJob->jobLocation($location); // Other. if (! empty($this->description)) { $schemaJob->description($this->description); } if (! empty($this->contract)) { $schemaJob->employmentType($this->contract); } if (! empty($this->created_at)) { $schemaJob->datePosted($this->created_at); } return $schemaJob; } }