<?php
declare(strict_types=1);
namespace BitBag\OpenMarketplace\Component\SellerPlan\EventListener;
use BitBag\OpenMarketplace\Component\SellerPlan\Entity\SellerPlanInterface;
use BitBag\OpenMarketplace\Component\SellerPlan\Provider\ProductProviderInterface;
use BitBag\OpenMarketplace\Component\SellerPlan\Synchronizer\ProductSynchronizerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
final class SellerPlanListener
{
public function __construct(
private ProductProviderInterface $provider,
private ProductSynchronizerInterface $synchronizer,
private EntityManagerInterface $entityManager,
) {
}
public function synchronizeProduct(ResourceControllerEvent $event): void
{
$sellerPlan = $event->getSubject();
if (!$sellerPlan instanceof SellerPlanInterface) {
return;
}
$product = $this->provider->provideProduct($sellerPlan);
$productVariant = $this->provider->provideProductVariant($product, $sellerPlan);
$this->synchronizer->synchronizeTranslations($sellerPlan, $product);
$this->synchronizer->synchronizePricing($sellerPlan, $product, $productVariant);
$sellerPlan->setProduct($product);
$this->entityManager->persist($product);
$this->entityManager->flush();
}
}