Files
core/src/Order/Notifications/OrderStatusUpdatedNotification.php
T

66 lines
2.1 KiB
PHP

<?php
namespace Modules\Core\Order\Notifications;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Notification as NotificationFacade;
use Modules\Core\Notification\BaseNotification;
use Modules\Core\Order\Events\OrderStatusUpdated;
use Modules\Core\Order\Support\OrderReferenceDisplay;
class OrderStatusUpdatedNotification extends BaseNotification
{
public function __construct(private readonly OrderStatusUpdated $event) {}
public static function getKey(): string
{
return 'order.status_updated.customer.mail';
}
public static function listensTo(): string
{
return OrderStatusUpdated::class;
}
/**
* 'ready-for-pickup' has its own, richer notification
* (Modules\Core\Order\Notifications\OrderPickupReadyNotification) —
* both listen to the same OrderStatusUpdated event via
* NotificationRegistry, so without this the customer would get two
* emails for that one transition. Returning no channels is the
* standard Laravel way to suppress a notification outright.
*/
public function via(object $notifiable): array
{
if ($this->event->newStatus === 'ready-for-pickup') {
return [];
}
return ['mail'];
}
public function notifiable(): AnonymousNotifiable
{
$order = $this->event->order;
$email = $order->billingAddress?->contact_email ?? $order->shippingAddress?->contact_email;
return NotificationFacade::route('mail', $email);
}
public function toMail(object $notifiable): MailMessage
{
$order = $this->event->order;
$reference = OrderReferenceDisplay::resolve($order);
return (new MailMessage)
->subject(__('Your order :reference has been updated', ['reference' => $reference]))
->view('core::order.notifications.status-updated', [
'reference' => $reference,
'statusLabel' => config("lunar.orders.statuses.{$order->status}.label", $order->status),
]);
}
}