SuperSeller3000/src/Infrastructure/Messenger/Handler/TrackingPushHandler.php

46 lines
1.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Infrastructure\Messenger\Handler;
use App\Application\Channel\ChannelAdapterRegistry;
use App\Domain\Order\Repository\OrderRepositoryInterface;
use App\Infrastructure\Messenger\Message\TrackingPushMessage;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Uid\Uuid;
#[AsMessageHandler]
final class TrackingPushHandler
{
public function __construct(
private readonly OrderRepositoryInterface $orders,
private readonly ChannelAdapterRegistry $channelAdapters,
private readonly LoggerInterface $logger,
) {
}
public function __invoke(TrackingPushMessage $message): void
{
$order = $this->orders->findById(Uuid::fromString($message->orderId));
if (null === $order) {
throw new UnrecoverableMessageHandlingException("Order {$message->orderId} not found");
}
$platformType = $order->getPlatform()->getType();
$adapter = $this->channelAdapters->get($platformType);
$adapter->pushTracking($order);
$order->markTrackingPushedToEbay();
$this->orders->save($order);
$this->logger->info('Tracking pushed to channel', [
'orderId' => $message->orderId,
'platform' => $platformType,
'trackingNumber' => $message->trackingNumber,
]);
}
}