<?php
declare(strict_types=1);
namespace WbfkExtensions\Subscriber;
use Enqueue\MessengerAdapter\EnvelopeItem\TransportConfiguration;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use WbfkExtensions\MessageQueue\Message\RecalculateProductSupplierDeliveryTimes;
class RecalculateProductSupplierDeliveryTimesOnCheckout implements EventSubscriberInterface
{
public function __construct(private readonly MessageBusInterface $messageBus)
{
}
public static function getSubscribedEvents(): array
{
return [
CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
];
}
public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
{
$lineItems = $event->getOrder()->getLineItems();
if ($lineItems === null || $lineItems->count() <= 0) {
return;
}
$productIds = $lineItems->fmap(static function (OrderLineItemEntity $orderLineItem) {
if ($orderLineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
return null;
}
return $orderLineItem->getReferencedId();
});
$this->messageBus->dispatch(
new RecalculateProductSupplierDeliveryTimes(
$productIds,
"checkout order placed. order id: {$event->getOrder()->getId()}"
),
[
new TransportConfiguration([
'metadata' => [
'priority' => -1000, // priority less than mails (0)
],
]),
]
);
}
}