<?php
declare(strict_types=1);
namespace App\CoreBundle\EventSubscriber;
use App\CoreBundle\Command\WooDeleteProductCommand;
use App\CoreBundle\Command\WooSyncProductCommand;
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\ProductVariantFacade;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Process\Process;
class ProductEventSubscriber implements EventSubscriberInterface
{
/**
* @var \App\CoreBundle\Component\WooCommerce\WooCommerce
*/
private $wooCommerce;
/**
* @var \App\CoreBundle\Command\WooSyncProductCommand
*/
private $wooSyncProductCommand;
/**
* @var \App\CoreBundle\Command\WooDeleteProductCommand
*/
private $wooDeleteProductCommand;
/**
* @var \App\CoreBundle\Model\Product\ProductFacade
*/
private $prouctFacade;
/**
* @var \App\CoreBundle\Model\Product\ProductDataFactoryInterface
*/
private $productDataFactory;
/**
* @var \App\CoreBundle\Model\Product\Variant\ProductVariantFacade
*/
private $productVariantFacade;
/**
* @param \App\CoreBundle\Component\WooCommerce\WooCommerce $wooCommerce
* @param \App\CoreBundle\Command\WooSyncProductCommand $wooSyncProductCommand
* @param \App\CoreBundle\Command\WooDeleteProductCommand $wooDeleteProductCommand
* @param \App\CoreBundle\Model\Product\ProductFacade $productFacade
* @param \App\CoreBundle\Model\Product\Variant\ProductVariantFacade $productVariantFacade
* @param \App\CoreBundle\Model\Product\ProductDataFactoryInterface $productDataFactory
*/
public function __construct(
WooCommerce $wooCommerce,
WooSyncProductCommand $wooSyncProductCommand,
WooDeleteProductCommand $wooDeleteProductCommand,
ProductFacade $productFacade,
ProductVariantFacade $productVariantFacade,
ProductDataFactoryInterface $productDataFactory
)
{
$this->wooCommerce = $wooCommerce;
$this->wooSyncProductCommand = $wooSyncProductCommand;
$this->wooDeleteProductCommand = $wooDeleteProductCommand;
$this->productFacade = $productFacade;
$this->productVariantFacade = $productVariantFacade;
$this->productDataFactory = $productDataFactory;
}
/**
* @param \App\CoreBundle\Model\Product\ProductEvent $productVariant
*/
public function syncAllProducts(ProductEvent $productVariant): void
{
$test = false;
if (!$test) {
$command = $this->wooSyncProductCommand->getShellCommand();
$process = \Symfony\Component\Process\Process::fromShellCommandline($command);
$process->start();
} else {
$products = $this->productFacade->getAllProductsToSync();
if ($products) {
foreach ($products as $product) {
$productData = $this->productDataFactory->createFromProduct($product);
foreach ($product->getProductEshops() as $productEshop) {
$eshopId = $productEshop->getEshopId();
if (!$productEshop->getEshopProductId()) {
$result = $this->wooCommerce->createProduct($product, $eshopId);
if($result && $result->id) {
$eshopProductId = $result->id;
$productData->synced[$eshopId] = true;
$productData->eshopProductId[$eshopId] = $result->id;
}
} else {
$result = $this->wooCommerce->updateProduct($product, $eshopId);
if ($result && $result->id) {
$eshopProductId = $result->id;
$productData->synced[$eshopId] = true;
}
}
$this->productFacade->sync($product, $eshopProductId, $eshopId);
// $this->productFacade->editAfterSync($product->getId(), $productData);
if ($eshopProductId && $product->isVariable() && $product->hasProductVariants()) {
foreach ($product->getProductVariants() as $productVariant) {
$resultVariant = $this->wooCommerce->refreshProductVariant($productVariant, $eshopId, $eshopProductId);
if ($resultVariant && $resultVariant->id) {
$this->productVariantFacade->sync($productVariant, $resultVariant->id, $eshopId);
}
}
}
}
}
}
}
}
/**
* @param \App\CoreBundle\Model\Product\ProductEvent $productVariant
*/
public function deleteProduct(ProductEvent $productVariant): void
{
$product = $productVariant->getProduct();
if ($product instanceof Product) {
$command = $this->wooDeleteProductCommand->getShellCommand($product->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 [
ProductEvent::CREATE => 'syncAllProducts',
ProductEvent::UPDATE => 'syncAllProducts',
ProductEvent::DELETE => 'deleteProduct',
];
}
}