src/CoreBundle/EventSubscriber/AttributeEventSubscriber.php line 79

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\CoreBundle\EventSubscriber;
  4. use App\CoreBundle\Command\WooDeleteAttributeCommand;
  5. use App\CoreBundle\Command\WooSyncAttributeCommand;
  6. use App\CoreBundle\Component\WooCommerce\WooCommerce;
  7. use App\CoreBundle\Model\Attribute\Attribute;
  8. use App\CoreBundle\Model\Attribute\AttributeDataFactoryInterface;
  9. use App\CoreBundle\Model\Attribute\AttributeEvent;
  10. use App\CoreBundle\Model\Attribute\AttributeFacade;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Process\Process;
  13. class AttributeEventSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var \App\CoreBundle\Component\WooCommerce\WooCommerce
  17.      */
  18.     private $wooCommerce;
  19.     /**
  20.      * @var \App\CoreBundle\Command\WooSyncAttributeCommand
  21.      */
  22.     private $wooSyncAttributeCommand;
  23.     /**
  24.      * @var \App\CoreBundle\Command\WooDeleteAttributeCommand
  25.      */
  26.     private $woodDeleteAttributeCommand;
  27.     /**
  28.      * @var \App\CoreBundle\Model\Attribute\AttributeFacade
  29.      */
  30.     private $attributeFacade;
  31.     /**
  32.      * @var \App\CoreBundle\Model\Attribute\AttributeDataFactoryInterface
  33.      */
  34.     private $attributeDataFactory;
  35.     /**
  36.      * @param \App\CoreBundle\Component\WooCommerce\WooCommerce $wooCommerce
  37.      * @param \App\CoreBundle\Command\WooSyncAttributeCommand $wooSyncAttributeCommand
  38.      * @param \App\CoreBundle\Command\WooDeleteAttributeCommand $woodDeleteAttributeCommand
  39.      * @param \App\CoreBundle\Model\Attribute\AttributeFacade $attributeFacade
  40.      * @param \App\CoreBundle\Model\Attribute\AttributeDataFactoryInterface $attributeDataFactory
  41.      */
  42.     public function __construct(
  43.         WooCommerce $wooCommerce,
  44.         WooSyncAttributeCommand $wooSyncAttributeCommand,
  45.         WooDeleteAttributeCommand $woodDeleteAttributeCommand,
  46.         AttributeFacade $attributeFacade,
  47.         AttributeDataFactoryInterface $attributeDataFactory
  48.     )
  49.     {
  50.         $this->wooCommerce $wooCommerce;
  51.         $this->wooSyncAttributeCommand $wooSyncAttributeCommand;
  52.         $this->woodDeleteAttributeCommand $woodDeleteAttributeCommand;
  53.         $this->attributeFacade $attributeFacade;
  54.         $this->attributeDataFactory $attributeDataFactory;
  55.     }
  56.     /**
  57.      * @param \App\CoreBundle\Model\Attribute\AttributeEvent $attributeEvent
  58.      */
  59.     public function syncAllAttributes(AttributeEvent $attributeEvent): void
  60.     {
  61.         $command $this->wooSyncAttributeCommand->getShellCommand();
  62.         $process = \Symfony\Component\Process\Process::fromShellCommandline($command);
  63.         $process->start();
  64.     }
  65.     /**
  66.      * @param \App\CoreBundle\Model\Attribute\AttributeEvent $attributeEvent
  67.      */
  68.     public function deleteAttribute(AttributeEvent $attributeEvent): void
  69.     {
  70.         $attribute $attributeEvent->getAttribute();
  71.         if ($attribute instanceof Attribute) {
  72.             $command $this->woodDeleteAttributeCommand->getShellCommand($attribute->getId());
  73.             $process = \Symfony\Component\Process\Process::fromShellCommandline($command);
  74.             $process->start();
  75.             $this->waitProcess($process);
  76.         }
  77.     }
  78.     public function waitProcess(Process $process)
  79.     {
  80.         while ($process) {
  81.             if ($process->isRunning() === true) {
  82.                 break;
  83.             }
  84.             $process null;
  85.         }
  86.         sleep(1);
  87.     }
  88.     /**
  89.      * @return array
  90.      */
  91.     public static function getSubscribedEvents(): array
  92.     {
  93.         return [
  94.             AttributeEvent::CREATE => 'syncAllAttributes',
  95.             AttributeEvent::UPDATE => 'syncAllAttributes',
  96.             AttributeEvent::DELETE => 'deleteAttribute',
  97.         ];
  98.     }
  99. }