baks-dev/orders-order
Laravel/Symfony модуль системных заказов: установка через Composer, интеграция с Centrifugo и Messenger (воркер orders-order), установка ассетов, расширение статусов через сервисы с тегом baks.order.status, поддержка миграций и тестов.
Install Dependencies:
composer require baks-dev/orders-order baks-dev/payment baks-dev/users-address baks-dev/contacts-region baks-dev/centrifugo baks-dev/products-stocks baks-dev/delivery phpoffice/phpspreadsheet
Run Asset Installation:
php bin/console baks:assets:install
Set Up Database Migrations:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Start Messenger Worker:
php bin/console messenger:consume orders-order
Create a Basic Order:
use BaksDev\Orders\Order\Order;
use BaksDev\Orders\Order\OrderRepository;
$order = new Order();
$order->setUserId(1);
$order->setStatus('pending'); // Default status
$orderRepository = app(OrderRepository::class);
$orderRepository->save($order);
Verify Real-Time Updates (if using Centrifugo):
.env.// Create a custom status (e.g., 'processing')
$order = new Order();
$order->setUserId(1);
$order->setStatus('pending');
// Transition to 'processing' (assuming you've implemented OrderStatusCustom)
$order->transitionTo('processing');
$orderRepository->save($order);
OrderRepository to persist orders with initial status (e.g., pending).OrderStatusInterface for custom statuses (e.g., processing, shipped, cancelled).
#[AutoconfigureTag('baks.order.status')]
class ProcessingStatus implements OrderStatusInterface {
public function canTransitionFrom(Order $order, string $toStatus): bool {
return $order->getStatus() === 'pending' && $toStatus === 'processing';
}
}
validateTransition() in your custom status class to enforce business rules.use Symfony\Component\Messenger\MessageBusInterface;
$bus = app(MessageBusInterface::class);
$bus->dispatch(new UpdateOrderStatusMessage($order->getId(), 'shipped'));
orders-order:
php bin/console messenger:consume orders-order
use BaksDev\Orders\Order\Event\OrderStatusChanged;
$event = new OrderStatusChanged($order);
$dispatcher->dispatch($event);
const channel = client.subscribe('orders.order.1'); // Order ID
channel.on('order_status_changed', (data) => {
console.log('Status updated:', data.status);
});
products-stocks to deduct inventory when orders are confirmed.
$order->transitionTo('confirmed'); // Triggers stock deduction via Messenger
delivery package for shipping labels and tracking.
$deliveryService = app(\BaksDev\Delivery\DeliveryService::class);
$deliveryService->createShipment($order);
OrderController@store, OrderController@updateStatus).
public function updateStatus(Request $request, Order $order) {
$order->transitionTo($request->input('status'));
return response()->json(['status' => $order->getStatus()]);
}
public function rules() {
return [
'status' => ['required', 'string', Rule::in(['pending', 'processing', 'shipped', 'cancelled'])],
];
}
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CancelPendingOrdersCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output) {
$orders = $this->orderRepository->findBy(['status' => 'pending']);
foreach ($orders as $order) {
$order->transitionTo('cancelled');
$this->orderRepository->save($order);
}
$output->writeln('Cancelled ' . count($orders) . ' orders.');
}
}
public function testCannotTransitionFromShippedToCancelled() {
$order = new Order();
$order->setStatus('shipped');
$this->assertFalse($order->canTransitionTo('cancelled'));
}
public function testOrderWorkflow() {
$response = $this->post('/orders', ['user_id' => 1]);
$response->assertStatus(201);
$response = $this->patch('/orders/1/status', ['status' => 'processing']);
$response->assertStatus(200);
}
Status Transition Logic Errors:
pending → shipped without processing).canTransitionFrom() in all custom status implementations. Use PHPUnit to verify edge cases.validateTransition() method to your Order entity to centralize validation logic.Messenger Worker Stalls:
orders-order worker crashes or is overloaded.php bin/console messenger:failed-messages-list orders-order.Centrifugo Connection Issues:
CENTRIFUGO_URL and CENTRIFUGO_API_KEY in .env.Database Lock Contention:
orders table can cause timeouts during status transitions.status and user_id.@Version) for high-contention scenarios.Dependency Conflicts:
baks-dev/orders-order and other packages (e.g., laravel/framework).composer why-not <package> to diagnose conflicts.composer.lock to pin versions if necessary.Log Order Events:
Order entity to log all status changes to a separate table.protected function logStatusChange(string $from, string $to) {
\Log::info("Order {$this->id} status changed from {$from} to {$to}");
// Optionally store in a database table for auditing
}
Enable Messenger Debugging:
# config/packages/messenger.yaml
framework:
messenger:
failure_transport: failed
transports:
failed: 'doctrine://default?queue_name=failed'
Centrifugo Debugging:
# centrifugo/config.json
"debug": true,
"log_level
How can I help you explore Laravel packages today?