*/ public function for(Property $property, int $count = 3, int $nights = 3, int $guests = 2): array { $key = "next-free-stays:{$property->id}:{$count}:{$nights}:{$guests}:".today()->toDateString(); $stays = Cache::remember($key, now()->addMinutes(10), fn (): array => $this->find($property, $count, $nights, min($guests, $property->max_guests))); return array_map(fn (array $stay): array => [ ...$stay, 'arrival' => CarbonImmutable::parse($stay['arrival']), 'departure' => CarbonImmutable::parse($stay['departure']), ], $stays); } /** * @return list */ private function find(Property $property, int $count, int $nights, int $guests): array { $from = CarbonImmutable::tomorrow(); $until = $from->addDays(self::HORIZON_DAYS + self::MAX_NIGHTS); $occupied = $this->availability->occupiedNights($property, $from, $until); $stays = []; for ($arrival = $from; count($stays) < $count && $arrival->lessThan($from->addDays(self::HORIZON_DAYS)); $arrival = $arrival->addDay()) { if (isset($occupied[$arrival->toDateString()])) { continue; } $quote = $this->firstPricedStay($property, $arrival, $nights, $guests, $occupied); if ($quote === null) { continue; } $stays[] = [ 'arrival' => $quote->arrival->toDateString(), 'departure' => $quote->departure->toDateString(), 'nights' => $quote->nights(), 'total' => $quote->total(), 'url' => route('booking.create', ['anreise' => $quote->arrival->toDateString(), 'abreise' => $quote->departure->toDateString(), 'personen' => $guests]), ]; $arrival = $quote->departure->subDay(); } return $stays; } /** * The shortest stay of at least $nights from $arrival that is free and priceable, if any. * * @param array $occupied */ private function firstPricedStay(Property $property, CarbonImmutable $arrival, int $nights, int $guests, array $occupied): ?PriceQuote { for ($length = 1; $length <= self::MAX_NIGHTS; $length++) { if (isset($occupied[$arrival->addDays($length - 1)->toDateString()])) { return null; } if ($length < $nights) { continue; } try { return $this->calculator->calculate($property, new StayRequest($arrival, $arrival->addDays($length), $guests)); } catch (QuoteException) { continue; } } return null; } }