<?php
declare(strict_types=1);
namespace App\CoreBundle\EventSubscriber;
use App\CoreBundle\Command\WooDeleteAttributeCommand;
use App\CoreBundle\Command\WooSyncAttributeCommand;
use App\CoreBundle\Component\WooCommerce\WooCommerce;
use App\CoreBundle\Model\Attribute\Attribute;
use App\CoreBundle\Model\Attribute\AttributeDataFactoryInterface;
use App\CoreBundle\Model\Attribute\AttributeEvent;
use App\CoreBundle\Model\Attribute\AttributeFacade;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Process\Process;
class AttributeEventSubscriber implements EventSubscriberInterface
{
/**
* @var \App\CoreBundle\Component\WooCommerce\WooCommerce
*/
private $wooCommerce;
/**
* @var \App\CoreBundle\Command\WooSyncAttributeCommand
*/
private $wooSyncAttributeCommand;
/**
* @var \App\CoreBundle\Command\WooDeleteAttributeCommand
*/
private $woodDeleteAttributeCommand;
/**
* @var \App\CoreBundle\Model\Attribute\AttributeFacade
*/
private $attributeFacade;
/**
* @var \App\CoreBundle\Model\Attribute\AttributeDataFactoryInterface
*/
private $attributeDataFactory;
/**
* @param \App\CoreBundle\Component\WooCommerce\WooCommerce $wooCommerce
* @param \App\CoreBundle\Command\WooSyncAttributeCommand $wooSyncAttributeCommand
* @param \App\CoreBundle\Command\WooDeleteAttributeCommand $woodDeleteAttributeCommand
* @param \App\CoreBundle\Model\Attribute\AttributeFacade $attributeFacade
* @param \App\CoreBundle\Model\Attribute\AttributeDataFactoryInterface $attributeDataFactory
*/
public function __construct(
WooCommerce $wooCommerce,
WooSyncAttributeCommand $wooSyncAttributeCommand,
WooDeleteAttributeCommand $woodDeleteAttributeCommand,
AttributeFacade $attributeFacade,
AttributeDataFactoryInterface $attributeDataFactory
)
{
$this->wooCommerce = $wooCommerce;
$this->wooSyncAttributeCommand = $wooSyncAttributeCommand;
$this->woodDeleteAttributeCommand = $woodDeleteAttributeCommand;
$this->attributeFacade = $attributeFacade;
$this->attributeDataFactory = $attributeDataFactory;
}
/**
* @param \App\CoreBundle\Model\Attribute\AttributeEvent $attributeEvent
*/
public function syncAllAttributes(AttributeEvent $attributeEvent): void
{
$command = $this->wooSyncAttributeCommand->getShellCommand();
$process = \Symfony\Component\Process\Process::fromShellCommandline($command);
$process->start();
}
/**
* @param \App\CoreBundle\Model\Attribute\AttributeEvent $attributeEvent
*/
public function deleteAttribute(AttributeEvent $attributeEvent): void
{
$attribute = $attributeEvent->getAttribute();
if ($attribute instanceof Attribute) {
$command = $this->woodDeleteAttributeCommand->getShellCommand($attribute->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 [
AttributeEvent::CREATE => 'syncAllAttributes',
AttributeEvent::UPDATE => 'syncAllAttributes',
AttributeEvent::DELETE => 'deleteAttribute',
];
}
}