candidates($site, $appearance) as $notice) { $banner = $this->banner($notice); if ($banner !== null) { return $banner; } } return null; } /** * Visible notices for the website and the appearance, in the order they compete. * * @return Collection */ public function candidates(Site $site, ?Appearance $appearance): Collection { return Notice::query()->visible() ->where(fn ($query) => $query->whereNull('site_id')->when($site->exists, fn ($query) => $query->orWhere('site_id', $site->id))) ->with('specialOffer.property') ->orderByDesc('priority') ->latest() ->orderByDesc('id') ->get() ->filter(fn (Notice $notice): bool => $notice->fitsSeason($appearance)) ->values(); } private function banner(Notice $notice): ?Banner { if ($notice->special_offer_id === null) { return filled($notice->text) ? new Banner($notice, (string) $notice->text, $notice->link_label, $notice->link_label ? ($notice->link_url ?: route('booking.create')) : null, $notice->tone) : null; } $offer = $notice->specialOffer; $advertised = $offer?->property ? $this->showcase->current($offer->property)->first(fn (AdvertisedOffer $advertised): bool => $advertised->offer->is($offer)) : null; if ($advertised === null) { return null; } return new Banner( $notice, filled($notice->text) ? (string) $notice->text : self::offerText($advertised), $notice->link_label ?: 'Zum Angebot', $notice->link_url ?: $advertised->bookingUrl(), $notice->tone, ); } /** * "Kurzfristig frei: 12.11.–15.11. · 15 % günstiger". */ public static function offerText(AdvertisedOffer $advertised): string { $dates = $advertised->arrival && $advertised->departure ? ': '.$advertised->arrival->format('d.m.').'–'.$advertised->departure->format('d.m.') : ''; return $advertised->title.$dates.' · '.$advertised->advantage; } }