custom/plugins/FourtwosixThemeExtension/src/Core/Content/Flow/Dispatching/Action/SendOrderDataAction.php line 71

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixThemeExtension\Core\Content\Flow\Dispatching\Action;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Psr7\Request as GuzzleRequest;
  5. use Psr\Log\LoggerInterface;
  6. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  7. use Shopware\Core\Checkout\Order\OrderEntity;
  8. use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\Event\FlowEvent;
  11. use Shopware\Core\Framework\Event\OrderAware;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. class SendOrderDataAction extends FlowAction
  14. {
  15.     private SystemConfigService $systemConfigService;
  16.     protected EntityRepositoryInterface $customerGroupRepository;
  17.     protected EntityRepositoryInterface $orderAddressRepository;
  18.     protected EntityRepositoryInterface $salesChannelRepository;
  19.     protected EntityRepositoryInterface $countryRepository;
  20.     protected EntityRepositoryInterface $paymentMethodRepository;
  21.     protected LoggerInterface $logger;
  22.     public function __construct(
  23.         SystemConfigService $systemConfigService,
  24.         EntityRepositoryInterface $customerGroupRepository,
  25.         EntityRepositoryInterface $orderAddressRepository,
  26.         EntityRepositoryInterface $salesChannelRepository,
  27.         EntityRepositoryInterface $countryRepository,
  28.         EntityRepositoryInterface $paymentMethodRepository,
  29.         LoggerInterface $logger
  30.     ) {
  31.         // you would need this repository to create a tag
  32.         $this->systemConfigService $systemConfigService;
  33.         $this->customerGroupRepository $customerGroupRepository;
  34.         $this->orderAddressRepository $orderAddressRepository;
  35.         $this->salesChannelRepository $salesChannelRepository;
  36.         $this->countryRepository $countryRepository;
  37.         $this->paymentMethodRepository $paymentMethodRepository;
  38.         $this->logger $logger;
  39.     }
  40.     public static function getName(): string
  41.     {
  42.         // your own action name
  43.         return 'action.send.order.data';
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             self::getName() => 'handle',
  49.         ];
  50.     }
  51.     public function requirements(): array
  52.     {
  53.         return [OrderAware::class];
  54.     }
  55.     /**
  56.      * @param FlowEvent $event
  57.      * @return void
  58.      */
  59.     public function handle(FlowEvent $event): void
  60.     {
  61.         $this->logger->info("FTS - Started flow for order",[]);
  62.         $orderEvent $event->getEvent();
  63.         if ( !$orderEvent instanceof OrderAware) {
  64.             return;
  65.         }
  66.         // @fourtwosix-edit: wait on state change because it may happen together with order placed
  67.         if ($orderEvent instanceof OrderStateMachineStateChangeEvent) {
  68.             sleep(10);
  69.         }
  70.         /** @var OrderEntity $order */
  71.         $order $orderEvent->getOrder();
  72.         $client = new Client();
  73.         $headers = [
  74.             'Content-Type' => 'application/json',
  75.         ];
  76.         $host rtrim($this->systemConfigService->get('FourtwosixThemeExtension.config.hubUrl'), '/');
  77.         $secret $this->systemConfigService->get('FourtwosixThemeExtension.config.hubSecret');
  78.         $endpoint sprintf("/forward-orders/%s"$order->getId());
  79.         $completeUrl sprintf("%s%s?secret=%s"$host$endpointurlencode($secret));
  80.         $body '';
  81.         $guzzleRequest = new GuzzleRequest('POST'$completeUrl$headers$body);
  82.         $client->sendAsync($guzzleRequest)->wait();
  83.         $this->logger->info("Forward order with id {$order->getId()}",[]);
  84.     }
  85. }