<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\PrestashopConnectorBundle\EventSubscriber;
use App\Services\CheckBundleInstall;
use Doctrine\ORM\EntityManagerInterface;
use ReflectionClass;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Bluue\PrestashopConnectorBundle\Message\UpdateStockProductToPrestaMessage;
class StockProductEditSubscriber implements EventSubscriberInterface
{
/**
* @var MessageBusInterface
*/
protected MessageBusInterface $messageBus;
/**
* @var EntityManagerInterface
*/
protected EntityManagerInterface $em;
public function __construct(
MessageBusInterface $messageBus,
EntityManagerInterface $em
) {
$this->messageBus = $messageBus;
$this->em = $em;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
if (CheckBundleInstall::exist('stocks-bundle')) {
$reflectionClass = new ReflectionClass('Bluue\StocksBundle\Event\StockProductEditEvent');
return [
$reflectionClass->getConstant('NAME') => 'onChange'
];
}
return [];
}
/**
* @param object $event
* @return void
*/
public function onChange(object $event): void
{
$entityId = $event->getId();
$entity = $this->em->find('StocksBundle:StockProduct', $entityId);
if ($entity) {
$this->messageBus->dispatch(new UpdateStockProductToPrestaMessage($entityId->toRfc4122()));
}
}
}