48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Enums\BookingStatus;
|
|
use App\Enums\ReviewStatus;
|
|
use App\Mail\NewReview;
|
|
use App\Models\Booking;
|
|
use App\Models\Review;
|
|
use App\Support\Settings;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* Stores a review written by a guest of a real, finished stay. It is published only after approval.
|
|
*/
|
|
class SubmitReview
|
|
{
|
|
/**
|
|
* @param array{rating: int, rating_cleanliness?: ?int, rating_equipment?: ?int, rating_location?: ?int, rating_host?: ?int, title?: ?string, body?: ?string, private_feedback?: ?string, display_name: string, publish_consent: bool} $data
|
|
*/
|
|
public function handle(Booking $booking, array $data): Review
|
|
{
|
|
if (! self::canReview($booking)) {
|
|
throw new RuntimeException('Für diese Buchung kann keine Bewertung abgegeben werden.');
|
|
}
|
|
|
|
$review = $booking->review()->create([
|
|
...$data,
|
|
'property_id' => $booking->property_id,
|
|
'status' => ReviewStatus::Pending,
|
|
'source' => 'website',
|
|
'stayed_on' => $booking->arrival,
|
|
]);
|
|
|
|
Mail::to(Settings::notificationRecipients())->send(new NewReview($review));
|
|
|
|
return $review;
|
|
}
|
|
|
|
public static function canReview(Booking $booking): bool
|
|
{
|
|
return in_array($booking->status, BookingStatus::occupying(), true)
|
|
&& $booking->departure->lessThanOrEqualTo(today())
|
|
&& ! $booking->review()->exists();
|
|
}
|
|
}
|