56 lines
1.9 KiB
PHP
56 lines
1.9 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Modules\Core\Order\Services;
|
||
|
|
|
||
|
|
use Lunar\Models\Order;
|
||
|
|
use Modules\Core\Order\Events\OrderPaidChanged;
|
||
|
|
use Modules\Core\Order\Events\OrderStatusChanged;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The one place Order::status/paid actually get written — replaces the
|
||
|
|
* earlier per-axis Modules\Core\Order\Services\OrderAxisWriter now that
|
||
|
|
* there is a single status column plus one independent `paid` field (see
|
||
|
|
* Modules\Core\Order\Services\OrderStatusFlow's own docblock for why
|
||
|
|
* payment timing is not a status-sequence step).
|
||
|
|
*
|
||
|
|
* write() relies on Modules\Core\Order\Observers\OrderObserver to
|
||
|
|
* generically dispatch OrderStatusUpdated whenever `status` actually
|
||
|
|
* changes — there's no separate axis-changed event to dispatch here
|
||
|
|
* anymore, since there's only one column left to watch. markPaid() is
|
||
|
|
* genuinely independent: it dispatches its own OrderPaidChanged, since
|
||
|
|
* OrderObserver only watches `status`, not `paid`.
|
||
|
|
*
|
||
|
|
* Cause is passed explicitly through every call rather than smuggled
|
||
|
|
* through a runtime property on the model — Lunar\Models\Order has
|
||
|
|
* $guarded = [], so Eloquent treats ANY property assignment as a real
|
||
|
|
* column to persist; an earlier design that tried
|
||
|
|
* $order->statusTransitionCause = ... broke immediately with an
|
||
|
|
* "undefined column" error the moment ->update() ran.
|
||
|
|
*/
|
||
|
|
class OrderStatusWriter
|
||
|
|
{
|
||
|
|
public function write(Order $order, string $to, string $causeClass): void
|
||
|
|
{
|
||
|
|
$from = $order->status;
|
||
|
|
|
||
|
|
if ($from === $to) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$order->update(['status' => $to]);
|
||
|
|
|
||
|
|
OrderStatusChanged::dispatch($order, $from, $to, $causeClass);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function markPaid(Order $order, string $causeClass): void
|
||
|
|
{
|
||
|
|
if ($order->paid) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$order->update(['paid' => true, 'paid_at' => now()]);
|
||
|
|
|
||
|
|
OrderPaidChanged::dispatch($order, $causeClass);
|
||
|
|
}
|
||
|
|
}
|