Files
fewo-stommer/tests/Support/Process/BehaviourSnapshot.php
T

132 lines
6.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Support\Process;
use App\Actions\AutoOffer;
use App\Models\Booking;
use App\Models\BookingEvent;
use App\Models\BookingMessage;
use App\Models\BookingTask;
use App\Models\ChannelTask;
use App\Models\DataRequest;
use App\Process\Activity;
use App\Process\CaseActivities;
use App\Process\CaseAutomation;
use Carbon\CarbonInterface;
use Illuminate\Mail\Transport\ArrayTransport;
use Illuminate\Support\Facades\Mail;
/**
* The observable behaviour of one case at one moment, in the eight dimensions of the behaviour comparison
* (docs/Prozessarchitektur/Architektur/05 §4.1): activities, state transitions, automation, holds and deadlines, price,
* documents and communication, tasks and evidence, repetition. Pure reading – the snapshot never changes the case.
*/
final class BehaviourSnapshot
{
/** Number of mails of the array transport already reported, so each snapshot lists only the new ones. */
private static int $mailsSeen = 0;
public static function reset(): void
{
self::$mailsSeen = 0;
}
/**
* @return array<string, mixed>
*/
public static function of(Booking $booking): array
{
$booking = $booking->fresh(['guest', 'property', 'payments']) ?? $booking;
return [
'activities' => array_map(fn (Activity $activity): array => [
'key' => $activity->key,
'label' => $activity->label,
'state' => $activity->state->value,
'detail' => $activity->detail,
'action' => $activity->action,
], CaseActivities::for($booking)),
'next' => CaseActivities::next($booking)?->key,
'situation' => CaseActivities::situation($booking),
'state' => [
'status' => $booking->status->value,
'intake' => $booking->intake?->value,
'case_type' => $booking->case_type?->value,
'hold_purpose' => $booking->hold_purpose,
'hold_expires_at' => self::time($booking->hold_expires_at),
'offer_version' => $booking->offer_version,
'information_version' => $booking->information_version,
'offer_sent_at' => self::time($booking->offer_sent_at),
'confirmed_at' => self::time($booking->confirmed_at),
'accepted_at' => self::time($booking->accepted_at),
'confirmation_sent_at' => self::time($booking->confirmation_sent_at),
'wish_at' => self::time($booking->wish_at),
'completed_at' => self::time($booking->completed_at),
'settled_at' => self::time($booking->settled_at),
],
'automation' => [
'auto_offer_blockers' => $booking->intake?->value !== 'booking' && in_array($booking->status->value, ['reserved'], true)
? app(AutoOffer::class)->blockers($booking) : null,
'document_blockers' => app(CaseAutomation::class)->documentBlockers($booking),
],
'calendar' => [
'occupying' => Booking::query()->occupying()->whereKey($booking->id)->exists(),
'hold_active' => $booking->isHoldActive(),
'clarification' => $booking->clarificationState()['state'],
],
'price' => [
'total' => $booking->total,
'deposit' => $booking->deposit,
'lines' => array_map(fn (array $line): array => [$line['type'] ?? null, $line['amount'] ?? null], (array) ($booking->quote['lines'] ?? [])),
'deposit_due_on' => $booking->deposit_due_on?->toDateString(),
'balance_due_on' => $booking->balance_due_on?->toDateString(),
],
'documents' => [
'contract' => $booking->contract_path !== null,
'terms_template' => $booking->offer_terms['template']['name'] ?? null,
'cancellation_policy' => $booking->offer_terms['cancellation_policy']['name'] ?? null,
],
'communication' => [
'messages' => BookingMessage::query()->where('booking_id', $booking->id)->orderBy('id')->get()
->map(fn (BookingMessage $message): array => [$message->audience, $message->subject])->all(),
'mails' => self::newMails(),
],
'tasks' => BookingTask::query()->where('booking_id', $booking->id)->orderBy('id')->get()
->map(fn (BookingTask $task): array => [$task->kind->value, $task->status->value])->all(),
'channel_tasks' => ChannelTask::query()->whereHas('channel', fn ($query) => $query->where('property_id', $booking->property_id))->orderBy('id')->get()
->map(fn (ChannelTask $task): array => [$task->channel_id, $task->action->value, $task->status->value])->all(),
'data_requests' => DataRequest::query()->where('booking_id', $booking->id)->orderBy('id')->get()
->map(fn (DataRequest $request): array => [$request->status, $request->reminders_sent, $request->fields])->all(),
'events' => BookingEvent::query()->where('booking_id', $booking->id)->orderBy('id')->pluck('type')->all(),
];
}
/**
* Subjects and recipients of the mails sent since the last snapshot.
*
* @return list<array{0: string, 1: string}>
*/
private static function newMails(): array
{
$transport = Mail::mailer('array')->getSymfonyTransport();
if (! $transport instanceof ArrayTransport) {
return [];
}
$all = $transport->messages()->all();
$new = array_slice($all, self::$mailsSeen);
self::$mailsSeen = count($all);
return array_values(array_map(function ($sent): array {
$email = $sent->getOriginalMessage();
return [(string) $email->getSubject(), implode(',', array_map(fn ($address): string => $address->getAddress(), $email->getTo()))];
}, $new));
}
private static function time(?CarbonInterface $value): ?string
{
return $value?->format('Y-m-d H:i');
}
}