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

68 lines
2.2 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\OrderDispatched;
use Modules\Core\Order\Support\OrderReferenceDisplay;
use Modules\Core\Shipping\Models\Shipment;
/**
* Fills a real, previously-unfilled customer-communication gap — before
* this redesign nothing notified a customer when their carrier order left
* the building at all.
*/
class OrderDispatchedNotification extends BaseNotification
{
public function __construct(private readonly OrderDispatched $event) {}
public static function getKey(): string
{
return 'order.dispatched.customer.mail';
}
public static function listensTo(): string
{
return OrderDispatched::class;
}
public function via(object $notifiable): array
{
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 is on its way', ['reference' => $reference]))
->view('core::order.notifications.dispatched', [
'order' => $order,
'reference' => $reference,
// Active, numbered, non-return shipments: the voucher numbers
// to show, plus the courier's link for manual carriers
// (Shipment::trackingUrl()). Integrated carriers' history is
// on the customer's order page.
'shipments' => $order->shipments()->get()
->reject(fn (Shipment $shipment) => $shipment->isCancelled() || $shipment->isReturn() || blank($shipment->tracking_reference))
->values(),
]);
}
}