<?php declare(strict_types=1);
namespace FourtwosixThemeExtension\Core\Content\Flow\Dispatching\Action;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Event\FlowEvent;
use Shopware\Core\Framework\Event\OrderAware;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class SendOrderDataAction extends FlowAction
{
private SystemConfigService $systemConfigService;
protected EntityRepositoryInterface $customerGroupRepository;
protected EntityRepositoryInterface $orderAddressRepository;
protected EntityRepositoryInterface $salesChannelRepository;
protected EntityRepositoryInterface $countryRepository;
protected EntityRepositoryInterface $paymentMethodRepository;
protected LoggerInterface $logger;
public function __construct(
SystemConfigService $systemConfigService,
EntityRepositoryInterface $customerGroupRepository,
EntityRepositoryInterface $orderAddressRepository,
EntityRepositoryInterface $salesChannelRepository,
EntityRepositoryInterface $countryRepository,
EntityRepositoryInterface $paymentMethodRepository,
LoggerInterface $logger
) {
// you would need this repository to create a tag
$this->systemConfigService = $systemConfigService;
$this->customerGroupRepository = $customerGroupRepository;
$this->orderAddressRepository = $orderAddressRepository;
$this->salesChannelRepository = $salesChannelRepository;
$this->countryRepository = $countryRepository;
$this->paymentMethodRepository = $paymentMethodRepository;
$this->logger = $logger;
}
public static function getName(): string
{
// your own action name
return 'action.send.order.data';
}
public static function getSubscribedEvents(): array
{
return [
self::getName() => 'handle',
];
}
public function requirements(): array
{
return [OrderAware::class];
}
/**
* @param FlowEvent $event
* @return void
*/
public function handle(FlowEvent $event): void
{
$this->logger->info("FTS - Started flow for order",[]);
$orderEvent = $event->getEvent();
if ( !$orderEvent instanceof OrderAware) {
return;
}
// @fourtwosix-edit: wait on state change because it may happen together with order placed
if ($orderEvent instanceof OrderStateMachineStateChangeEvent) {
sleep(10);
}
/** @var OrderEntity $order */
$order = $orderEvent->getOrder();
$client = new Client();
$headers = [
'Content-Type' => 'application/json',
];
$host = rtrim($this->systemConfigService->get('FourtwosixThemeExtension.config.hubUrl'), '/');
$secret = $this->systemConfigService->get('FourtwosixThemeExtension.config.hubSecret');
$endpoint = sprintf("/forward-orders/%s", $order->getId());
$completeUrl = sprintf("%s%s?secret=%s", $host, $endpoint, urlencode($secret));
$body = '';
$guzzleRequest = new GuzzleRequest('POST', $completeUrl, $headers, $body);
$client->sendAsync($guzzleRequest)->wait();
$this->logger->info("Forward order with id {$order->getId()}",[]);
}
}