<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
meius / laravel-transaction-orchestrator example snippets
return [
// Other service providers...
Meius\LaravelTransactionOrchestrator\Providers\TransactionOrchestratorServiceProvider::class,
];
use App\Models\Order;
use App\Repositories\OrderRepository;
use Meius\LaravelTransactionOrchestrator\Attributes\Locks\LockForUpdate;
use Meius\LaravelTransactionOrchestrator\Attributes\Transactional;
use Meius\LaravelTransactionOrchestrator\Enums\HttpRollbackPolicy;
use Symfony\Component\HttpFoundation\Response;
class OrderController extends Controller
{
public function __construct(
private readonly OrderRepository $orderRepository,
) {
//
}
#[Transactional(
connection: 'mysql',
retries: 3,
backoff: [50, 100, 200], // milliseconds
noRollbackOn: [QueryException::class],
rollbackOnHttpError: HttpRollbackPolicy::ROLLBACK_ON_5XX,
)]
public function destroy(#[LockForUpdate] Order $order): Response
{
try {
$this->orderRepository->delete($order);
} catch (\Throwable) {
return response()->json([
'message' => 'Unable to delete the order.',
], Response::HTTP_INTERNAL_SERVER_ERROR); // 5xx → rollback (per policy)
}
return response()->noContent(); // 204 → commit
}
}
use App\Exceptions\Products\CannotRemoveProductException;
use App\Exceptions\Products\ProductNotInOrderException;
use App\Http\Resources\OrderResource;
use App\Models\Order;
use App\Models\Product;
use App\Services\OrderService;
use Meius\LaravelTransactionOrchestrator\Attributes\Locks\LockForUpdate;
use Meius\LaravelTransactionOrchestrator\Attributes\Locks\SharedLock;
use Meius\LaravelTransactionOrchestrator\Attributes\Transactional;
use Symfony\Component\HttpFoundation\Response;
class OrderProductController extends Controller
{
public function __construct(
private readonly OrderService $orderService,
) {
//
}
/**
* Removes a product from the order.
*/
#[Transactional]
public function destroy(
#[LockForUpdate] Order $order,
#[SharedLock] Product $product
): Response {
try {
$order = $this->orderService->recalculate($order, $product);
} catch (ProductNotInOrderException|CannotRemoveProductException $exception) {
return response()->json([
'error' => $exception->getMessage(),
], Response::HTTP_UNPROCESSABLE_ENTITY);
} catch (\Throwable) {
return response()->json([
'error' => 'Unable to remove product from order.',
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
return OrderResource::make($order)->response();
}
}
#[Transactional(rollbackOnHttpError: [409, 422])]
#[Transactional(connection: ['mysql', 'pgsql'])]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.