<?php
declare(strict_types=1);
namespace App\CoreBundle\EventSubscriber;
use App\CoreBundle\Command\WooSyncOrderBatchCommand;
use App\CoreBundle\Command\WooSyncOrderCommand;
use App\CoreBundle\Component\WooCommerce\WooCommerce;
use App\CoreBundle\Model\Order\Order;
use App\CoreBundle\Model\Order\OrderDataFactoryInterface;
use App\CoreBundle\Model\Order\OrderEvent;
use App\CoreBundle\Model\Order\OrderFacade;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Process\Process;
class OrderEventSubscriber implements EventSubscriberInterface
{
/**
* @var \App\CoreBundle\Component\WooCommerce\WooCommerce
*/
private $wooCommerce;
/**
* @var \App\CoreBundle\Command\WooSyncOrderCommand
*/
private $wooSyncOrderCommand;
/**
* @var \App\CoreBundle\Command\WooSyncOrderBatchCommand
*/
private $wooSyncOrderBatchCommand;
/**
* @var \App\CoreBundle\Model\Order\OrderFacade
*/
private $orderFacade;
/**
* @var \App\CoreBundle\Model\Order\OrderDataFactoryInterface
*/
private $orderDataFactory;
public function __construct(
WooCommerce $wooCommerce,
WooSyncOrderCommand $wooSyncOrderCommand,
WooSyncOrderBatchCommand $wooSyncOrderBatchCommand,
OrderFacade $orderFacade,
OrderDataFactoryInterface $orderDataFactory
)
{
$this->wooCommerce = $wooCommerce;
$this->wooSyncOrderCommand = $wooSyncOrderCommand;
$this->wooSyncOrderBatchCommand = $wooSyncOrderBatchCommand;
$this->orderFacade = $orderFacade;
$this->orderDataFactory = $orderDataFactory;
}
/**
* @param \App\CoreBundle\Model\Order\OrderEvent $orderEvent
*/
public function syncAllOrders(OrderEvent $orderEvent): void
{
$order = $orderEvent->getOrder();
// more efficient, it will only process the modified order
$command = $this->wooSyncOrderBatchCommand->getShellCommand(array($order)); // $command = $this->wooSyncOrderCommand->getShellCommand();
$process = \Symfony\Component\Process\Process::fromShellCommandline($command);
$process->start();
}
public function waitProcess(Process $process)
{
while ($process) {
if ($process->isRunning() === true) {
break;
}
$process = null;
}
sleep(1);
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
OrderEvent::UPDATE => 'syncAllOrders',
];
}
}