src/CoreBundle/EventSubscriber/ProductEventSubscriber.php line 126

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\Component\WooCommerce\WooCommerce;
  7. use App\CoreBundle\Model\Product\Product;
  8. use App\CoreBundle\Model\Product\ProductDataFactoryInterface;
  9. use App\CoreBundle\Model\Product\ProductEvent;
  10. use App\CoreBundle\Model\Product\ProductFacade;
  11. use App\CoreBundle\Model\Product\Variant\ProductVariantFacade;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Process\Process;
  14. class ProductEventSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var \App\CoreBundle\Component\WooCommerce\WooCommerce
  18.      */
  19.     private $wooCommerce;
  20.     /**
  21.      * @var \App\CoreBundle\Command\WooSyncProductCommand
  22.      */
  23.     private $wooSyncProductCommand;
  24.     /**
  25.      * @var \App\CoreBundle\Command\WooDeleteProductCommand
  26.      */
  27.     private $wooDeleteProductCommand;
  28.     /**
  29.      * @var \App\CoreBundle\Model\Product\ProductFacade
  30.      */
  31.     private $prouctFacade;
  32.     /**
  33.      * @var \App\CoreBundle\Model\Product\ProductDataFactoryInterface
  34.      */
  35.     private $productDataFactory;
  36.     /**
  37.      * @var \App\CoreBundle\Model\Product\Variant\ProductVariantFacade
  38.      */
  39.     private $productVariantFacade;
  40.     /**
  41.      * @param \App\CoreBundle\Component\WooCommerce\WooCommerce $wooCommerce
  42.      * @param \App\CoreBundle\Command\WooSyncProductCommand $wooSyncProductCommand
  43.      * @param \App\CoreBundle\Command\WooDeleteProductCommand $wooDeleteProductCommand
  44.      * @param \App\CoreBundle\Model\Product\ProductFacade $productFacade
  45.      * @param \App\CoreBundle\Model\Product\Variant\ProductVariantFacade $productVariantFacade
  46.      * @param \App\CoreBundle\Model\Product\ProductDataFactoryInterface $productDataFactory
  47.      */
  48.     public function __construct(
  49.         WooCommerce $wooCommerce,
  50.         WooSyncProductCommand $wooSyncProductCommand,
  51.         WooDeleteProductCommand $wooDeleteProductCommand,
  52.         ProductFacade $productFacade,
  53.         ProductVariantFacade $productVariantFacade,
  54.         ProductDataFactoryInterface $productDataFactory
  55.     )
  56.     {
  57.         $this->wooCommerce $wooCommerce;
  58.         $this->wooSyncProductCommand $wooSyncProductCommand;
  59.         $this->wooDeleteProductCommand $wooDeleteProductCommand;
  60.         $this->productFacade $productFacade;
  61.         $this->productVariantFacade $productVariantFacade;
  62.         $this->productDataFactory $productDataFactory;
  63.     }
  64.     /**
  65.      * @param \App\CoreBundle\Model\Product\ProductEvent $productVariant
  66.      */
  67.     public function syncAllProducts(ProductEvent $productVariant): void
  68.     {
  69.         $test false;
  70.         if (!$test) {
  71.             $command $this->wooSyncProductCommand->getShellCommand();
  72.             $process = \Symfony\Component\Process\Process::fromShellCommandline($command);
  73.             $process->start();
  74.         } else {
  75.             $products $this->productFacade->getAllProductsToSync();
  76.             if ($products) {
  77.                 foreach ($products as $product) {
  78.                     $productData $this->productDataFactory->createFromProduct($product);
  79.                     foreach ($product->getProductEshops() as $productEshop) {
  80.                         $eshopId $productEshop->getEshopId();
  81.                         if (!$productEshop->getEshopProductId()) {
  82.                             $result $this->wooCommerce->createProduct($product$eshopId);
  83.                             if($result && $result->id) {
  84.                                 $eshopProductId $result->id;
  85.                                 $productData->synced[$eshopId] = true;
  86.                                 $productData->eshopProductId[$eshopId] = $result->id;
  87.                             }
  88.                         } else {
  89.                             $result $this->wooCommerce->updateProduct($product$eshopId);
  90.                             if ($result && $result->id) {
  91.                                 $eshopProductId $result->id;
  92.                                 $productData->synced[$eshopId] = true;
  93.                             }
  94.                         }
  95.                         $this->productFacade->sync($product$eshopProductId$eshopId);
  96.                         // $this->productFacade->editAfterSync($product->getId(), $productData);
  97.                         if ($eshopProductId && $product->isVariable() && $product->hasProductVariants()) {
  98.                             foreach ($product->getProductVariants() as $productVariant) {
  99.                                 $resultVariant $this->wooCommerce->refreshProductVariant($productVariant$eshopId$eshopProductId);
  100.                                 if ($resultVariant && $resultVariant->id) {
  101.                                     $this->productVariantFacade->sync($productVariant$resultVariant->id$eshopId);
  102.                                 }
  103.                             }
  104.                         }
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.     }
  110.     /**
  111.      * @param \App\CoreBundle\Model\Product\ProductEvent $productVariant
  112.      */
  113.     public function deleteProduct(ProductEvent $productVariant): void
  114.     {
  115.         $product $productVariant->getProduct();
  116.         if ($product instanceof Product) {
  117.             $command $this->wooDeleteProductCommand->getShellCommand($product->getId());
  118.             $process = \Symfony\Component\Process\Process::fromShellCommandline($command);
  119.             $process->start();
  120.             $this->waitProcess($process);
  121.         }
  122.     }
  123.     public function waitProcess(Process $process)
  124.     {
  125.         while ($process) {
  126.             if ($process->isRunning() === true) {
  127.                 break;
  128.             }
  129.             $process null;
  130.         }
  131.         sleep(1);
  132.     }
  133.     /**
  134.      * @return array
  135.      */
  136.     public static function getSubscribedEvents(): array
  137.     {
  138.         return [
  139.             ProductEvent::CREATE => 'syncAllProducts',
  140.             ProductEvent::UPDATE => 'syncAllProducts',
  141.             ProductEvent::DELETE => 'deleteProduct',
  142.         ];
  143.     }
  144. }