vendor/bluue/prestashop-connector-bundle/src/EventSubscriber/StockProductEditSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Thomas HERISSON (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\PrestashopConnectorBundle\EventSubscriber;
  9. use App\Services\CheckBundleInstall;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use ReflectionClass;
  12. use Symfony\Component\Messenger\MessageBusInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Bluue\PrestashopConnectorBundle\Message\UpdateStockProductToPrestaMessage;
  15. class StockProductEditSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var MessageBusInterface
  19.      */
  20.     protected MessageBusInterface $messageBus;
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     protected EntityManagerInterface $em;
  25.     public function __construct(
  26.         MessageBusInterface $messageBus,
  27.         EntityManagerInterface $em
  28.     ) {
  29.         $this->messageBus $messageBus;
  30.         $this->em $em;
  31.     }
  32.     /**
  33.      * @return string[]
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         if (CheckBundleInstall::exist('stocks-bundle')) {
  38.             $reflectionClass = new ReflectionClass('Bluue\StocksBundle\Event\StockProductEditEvent');
  39.             return [
  40.                 $reflectionClass->getConstant('NAME') => 'onChange'
  41.             ];
  42.         }
  43.         return [];
  44.     }
  45.     /**
  46.      * @param object $event
  47.      * @return void
  48.      */
  49.     public function onChange(object $event): void
  50.     {
  51.         $entityId $event->getId();
  52.         $entity $this->em->find('StocksBundle:StockProduct'$entityId);
  53.         if ($entity) {
  54.             $this->messageBus->dispatch(new UpdateStockProductToPrestaMessage($entityId->toRfc4122()));
  55.         }
  56.     }
  57. }