<?php
declare(strict_types=1);
namespace App\CoreBundle\EventSubscriber;
use App\CoreBundle\Command\WooDeleteProductCommand;
use App\CoreBundle\Command\WooSyncProductCommand;
use App\CoreBundle\Command\WooSyncProductVariantCommand;
use App\CoreBundle\Component\WooCommerce\WooCommerce;
use App\CoreBundle\Model\Product\Product;
use App\CoreBundle\Model\Product\ProductDataFactoryInterface;
use App\CoreBundle\Model\Product\ProductEvent;
use App\CoreBundle\Model\Product\ProductFacade;
use App\CoreBundle\Model\Product\Variant\ProductVariant;
use App\CoreBundle\Model\Product\Variant\ProductVariantEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Process\Process;
class ProductVariantEventSubscriber implements EventSubscriberInterface
{
/**
* @var \App\CoreBundle\Component\WooCommerce\WooCommerce
*/
private $wooCommerce;
/**
* @var \App\CoreBundle\Command\WooDeleteProductVariantCommand
*/
private $wooDeleteProductVariantCommand;
/**
* @var \App\CoreBundle\Model\Product\ProductFacade
*/
private $attributeFacade;
/**
* @var \App\CoreBundle\Model\Product\ProductDataFactoryInterface
*/
private $attributeDataFactory;
/**
* @param \App\CoreBundle\Component\WooCommerce\WooCommerce $wooCommerce
* @param \App\CoreBundle\Command\WooDeleteProductVariantCommand $wooDeleteProductVariantCommand
* @param \App\CoreBundle\Model\Product\ProductFacade $attributeFacade
* @param \App\CoreBundle\Model\Product\ProductDataFactoryInterface $attributeDataFactory
*/
public function __construct(
WooCommerce $wooCommerce,
WooDeleteProductCommand $wooDeleteProductVariantCommand,
ProductFacade $attributeFacade,
ProductDataFactoryInterface $attributeDataFactory
)
{
$this->wooCommerce = $wooCommerce;
$this->wooDeleteProductVariantCommand = $wooDeleteProductVariantCommand;
$this->attributeFacade = $attributeFacade;
$this->attributeDataFactory = $attributeDataFactory;
}
/**
* @param \App\CoreBundle\Model\Product\Variant\ProductVariantEvent $productVariantEvent
*/
public function deleteProductVariant(ProductVariantEvent $productVariantEvent): void
{
$productVariant = $productVariantEvent->getProductVariant();
if ($productVariant instanceof ProductVariant) {
$command = $this->wooDeleteProductVariantCommand->getShellCommand($productVariant->getId());
$process = \Symfony\Component\Process\Process::fromShellCommandline($command);
$process->start();
$this->waitProcess($process);
}
}
public function waitProcess(Process $process)
{
while ($process) {
if ($process->isRunning() === true) {
break;
}
$process = null;
}
sleep(1);
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
ProductVariantEvent::DELETE => 'deleteProductVariant',
];
}
}