Files

89 lines
2.8 KiB
PHP

<?php
namespace App\Process\Tester;
use App\Actions\CreateReservation;
use App\Actions\PrepareStays;
use App\Actions\RequestReviews;
use App\Actions\SendBookingReminders;
use App\Enums\IntakeKind;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\Property;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Date;
/**
* What the events of a scenario work with inside a tester run: the tested channel and intake kind, the property, the
* case the scenario created, the landlord's inputs and the simulated clock.
*/
final class SandboxContext
{
public ?Booking $booking = null;
/**
* @param array<string, mixed> $inputs
*/
public function __construct(
public readonly Channel $channel,
public readonly IntakeKind $intake,
public readonly Property $property,
public readonly array $inputs,
) {}
public function arrival(): CarbonImmutable
{
$value = (string) ($this->inputs['arrival'] ?? '+21 days');
return CarbonImmutable::parse($value, now()->getTimezone())->startOfDay();
}
public function departure(): CarbonImmutable
{
return $this->arrival()->addDays(max(1, (int) ($this->inputs['nights'] ?? 4)));
}
public function guests(): int
{
return max(1, (int) ($this->inputs['guests'] ?? 2));
}
/**
* A request through the same action as the website's booking form.
*
* @param array<string, string> $guest
*/
public function request(array $guest): Booking
{
return $this->booking = app(CreateReservation::class)->handle(
$this->property,
new StayRequest($this->arrival(), $this->departure(), $this->guests()),
['first_name' => 'Testgast', 'last_name' => 'Sandbox', 'email' => 'gast@tester.invalid', ...$guest],
source: $this->channel->isWebsite() ? 'website' : 'channel',
originChannel: $this->channel->isWebsite() ? null : $this->channel,
);
}
/**
* Time passes: the clock moves on and the case-related schedulers run in the order of routes/console.php.
*/
public function travel(int $minutes): void
{
Date::setTestNow(now()->addMinutes($minutes));
foreach (['bookings:advance-waiting', 'bookings:expire-holds', 'bookings:auto-offers', 'cases:data-requests'] as $command) {
Artisan::call($command);
}
app(SendBookingReminders::class)->handle();
app(PrepareStays::class)->handle();
app(RequestReviews::class)->handle();
}
public function travelTo(CarbonImmutable $moment): void
{
$minutes = (int) max(0, now()->diffInMinutes($moment));
$this->travel($minutes);
}
}