src/CoreBundle/EventSubscriber/OrderEventSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\CoreBundle\EventSubscriber;
  4. use App\CoreBundle\Command\WooSyncOrderBatchCommand;
  5. use App\CoreBundle\Command\WooSyncOrderCommand;
  6. use App\CoreBundle\Component\WooCommerce\WooCommerce;
  7. use App\CoreBundle\Model\Order\Order;
  8. use App\CoreBundle\Model\Order\OrderDataFactoryInterface;
  9. use App\CoreBundle\Model\Order\OrderEvent;
  10. use App\CoreBundle\Model\Order\OrderFacade;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Process\Process;
  13. class OrderEventSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var \App\CoreBundle\Component\WooCommerce\WooCommerce
  17.      */
  18.     private $wooCommerce;
  19.     /**
  20.      * @var \App\CoreBundle\Command\WooSyncOrderCommand
  21.      */
  22.     private $wooSyncOrderCommand;
  23.     /**
  24.      * @var \App\CoreBundle\Command\WooSyncOrderBatchCommand
  25.      */
  26.     private $wooSyncOrderBatchCommand;
  27.     /**
  28.      * @var \App\CoreBundle\Model\Order\OrderFacade
  29.      */
  30.     private $orderFacade;
  31.     /**
  32.      * @var \App\CoreBundle\Model\Order\OrderDataFactoryInterface
  33.      */
  34.     private $orderDataFactory;
  35.     public function __construct(
  36.         WooCommerce $wooCommerce,
  37.         WooSyncOrderCommand $wooSyncOrderCommand,
  38.         WooSyncOrderBatchCommand $wooSyncOrderBatchCommand,
  39.         OrderFacade $orderFacade,
  40.         OrderDataFactoryInterface $orderDataFactory
  41.     )
  42.     {
  43.         $this->wooCommerce $wooCommerce;
  44.         $this->wooSyncOrderCommand $wooSyncOrderCommand;
  45.         $this->wooSyncOrderBatchCommand $wooSyncOrderBatchCommand;
  46.         $this->orderFacade $orderFacade;
  47.         $this->orderDataFactory $orderDataFactory;
  48.     }
  49.     /**
  50.      * @param \App\CoreBundle\Model\Order\OrderEvent $orderEvent
  51.      */
  52.     public function syncAllOrders(OrderEvent $orderEvent): void
  53.     {
  54.         $order $orderEvent->getOrder();
  55.         // more efficient, it will only process the modified order
  56.         $command $this->wooSyncOrderBatchCommand->getShellCommand(array($order)); // $command = $this->wooSyncOrderCommand->getShellCommand();
  57.         $process = \Symfony\Component\Process\Process::fromShellCommandline($command);
  58.         $process->start();
  59.     }
  60.     public function waitProcess(Process $process)
  61.     {
  62.         while ($process) {
  63.             if ($process->isRunning() === true) {
  64.                 break;
  65.             }
  66.             $process null;
  67.         }
  68.         sleep(1);
  69.     }
  70.     /**
  71.      * @return array
  72.      */
  73.     public static function getSubscribedEvents(): array
  74.     {
  75.         return [
  76.             OrderEvent::UPDATE => 'syncAllOrders',
  77.         ];
  78.     }
  79. }