src/CoreBundle/EventSubscriber/ProductVariantEventSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\CoreBundle\EventSubscriber;
  4. use App\CoreBundle\Command\WooDeleteProductCommand;
  5. use App\CoreBundle\Command\WooSyncProductCommand;
  6. use App\CoreBundle\Command\WooSyncProductVariantCommand;
  7. use App\CoreBundle\Component\WooCommerce\WooCommerce;
  8. use App\CoreBundle\Model\Product\Product;
  9. use App\CoreBundle\Model\Product\ProductDataFactoryInterface;
  10. use App\CoreBundle\Model\Product\ProductEvent;
  11. use App\CoreBundle\Model\Product\ProductFacade;
  12. use App\CoreBundle\Model\Product\Variant\ProductVariant;
  13. use App\CoreBundle\Model\Product\Variant\ProductVariantEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Process\Process;
  16. class ProductVariantEventSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var \App\CoreBundle\Component\WooCommerce\WooCommerce
  20.      */
  21.     private $wooCommerce;
  22.     /**
  23.      * @var \App\CoreBundle\Command\WooDeleteProductVariantCommand
  24.      */
  25.     private $wooDeleteProductVariantCommand;
  26.     /**
  27.      * @var \App\CoreBundle\Model\Product\ProductFacade
  28.      */
  29.     private $attributeFacade;
  30.     /**
  31.      * @var \App\CoreBundle\Model\Product\ProductDataFactoryInterface
  32.      */
  33.     private $attributeDataFactory;
  34.     /**
  35.      * @param \App\CoreBundle\Component\WooCommerce\WooCommerce $wooCommerce
  36.      * @param \App\CoreBundle\Command\WooDeleteProductVariantCommand $wooDeleteProductVariantCommand
  37.      * @param \App\CoreBundle\Model\Product\ProductFacade $attributeFacade
  38.      * @param \App\CoreBundle\Model\Product\ProductDataFactoryInterface $attributeDataFactory
  39.      */
  40.     public function __construct(
  41.         WooCommerce $wooCommerce,
  42.         WooDeleteProductCommand $wooDeleteProductVariantCommand,
  43.         ProductFacade $attributeFacade,
  44.         ProductDataFactoryInterface $attributeDataFactory
  45.     )
  46.     {
  47.         $this->wooCommerce $wooCommerce;
  48.         $this->wooDeleteProductVariantCommand $wooDeleteProductVariantCommand;
  49.         $this->attributeFacade $attributeFacade;
  50.         $this->attributeDataFactory $attributeDataFactory;
  51.     }
  52.     /**
  53.      * @param \App\CoreBundle\Model\Product\Variant\ProductVariantEvent $productVariantEvent
  54.      */
  55.     public function deleteProductVariant(ProductVariantEvent $productVariantEvent): void
  56.     {
  57.         $productVariant $productVariantEvent->getProductVariant();
  58.         if ($productVariant instanceof ProductVariant) {
  59.             $command $this->wooDeleteProductVariantCommand->getShellCommand($productVariant->getId());
  60.             $process = \Symfony\Component\Process\Process::fromShellCommandline($command);
  61.             $process->start();
  62.             $this->waitProcess($process);
  63.         }
  64.     }
  65.     public function waitProcess(Process $process)
  66.     {
  67.         while ($process) {
  68.             if ($process->isRunning() === true) {
  69.                 break;
  70.             }
  71.             $process null;
  72.         }
  73.         sleep(1);
  74.     }
  75.     /**
  76.      * @return array
  77.      */
  78.     public static function getSubscribedEvents(): array
  79.     {
  80.         return [
  81.             ProductVariantEvent::DELETE => 'deleteProductVariant',
  82.         ];
  83.     }
  84. }