feat: simplify cart restoration logic in ReleaseCheckoutService and SourceCartService
This commit is contained in:
@@ -14,22 +14,21 @@ class ReleaseCheckoutService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly StockReservationService $reservations,
|
||||
private readonly SourceCartService $sourceCart,
|
||||
) {}
|
||||
|
||||
public function cancel(Purchase $purchase): Purchase
|
||||
{
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED, restoreCart: true);
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED);
|
||||
}
|
||||
|
||||
public function cancelWithoutRestoringCart(Purchase $purchase): Purchase
|
||||
{
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED, restoreCart: false);
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED);
|
||||
}
|
||||
|
||||
public function expire(Purchase $purchase): Purchase
|
||||
{
|
||||
return $this->release($purchase, Purchase::STATUS_EXPIRED, restoreCart: true);
|
||||
return $this->release($purchase, Purchase::STATUS_EXPIRED);
|
||||
}
|
||||
|
||||
public function expireOverdue(): int
|
||||
@@ -66,9 +65,9 @@ class ReleaseCheckoutService
|
||||
return $expiredCount;
|
||||
}
|
||||
|
||||
private function release(Purchase $purchase, string $targetStatus, bool $restoreCart): Purchase
|
||||
private function release(Purchase $purchase, string $targetStatus): Purchase
|
||||
{
|
||||
return DB::transaction(function () use ($purchase, $targetStatus, $restoreCart): Purchase {
|
||||
return DB::transaction(function () use ($purchase, $targetStatus): Purchase {
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
|
||||
if ($purchase->status === Purchase::STATUS_PAID) {
|
||||
@@ -98,8 +97,7 @@ class ReleaseCheckoutService
|
||||
]);
|
||||
}
|
||||
|
||||
$reservationReturnedToCart = $restoreCart && $this->sourceCart->restore($purchase);
|
||||
$this->releaseCartReservations($purchase, $reservationReturnedToCart, $targetStatus);
|
||||
$this->releaseCartReservations($purchase, $targetStatus);
|
||||
|
||||
$purchase->update(['status' => $targetStatus]);
|
||||
|
||||
@@ -109,15 +107,8 @@ class ReleaseCheckoutService
|
||||
|
||||
private function releaseCartReservations(
|
||||
Purchase $purchase,
|
||||
bool $reservationReturnedToCart,
|
||||
string $targetStatus,
|
||||
): void {
|
||||
if ($reservationReturnedToCart) {
|
||||
$this->reservations->detachFromPurchase($purchase);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$cart = $purchase->cart()->withTrashed()->lockForUpdate()->first();
|
||||
if ($cart === null) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user