src/Component/SellerPlan/EventListener/SellerPlanListener.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\SellerPlan\EventListener;
  4. use BitBag\OpenMarketplace\Component\SellerPlan\Entity\SellerPlanInterface;
  5. use BitBag\OpenMarketplace\Component\SellerPlan\Provider\ProductProviderInterface;
  6. use BitBag\OpenMarketplace\Component\SellerPlan\Synchronizer\ProductSynchronizerInterface;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  9. final class SellerPlanListener
  10. {
  11.     public function __construct(
  12.         private ProductProviderInterface $provider,
  13.         private ProductSynchronizerInterface $synchronizer,
  14.         private EntityManagerInterface $entityManager,
  15.         ) {
  16.     }
  17.     public function synchronizeProduct(ResourceControllerEvent $event): void
  18.     {
  19.         $sellerPlan $event->getSubject();
  20.         if (!$sellerPlan instanceof SellerPlanInterface) {
  21.             return;
  22.         }
  23.         $product $this->provider->provideProduct($sellerPlan);
  24.         $productVariant $this->provider->provideProductVariant($product$sellerPlan);
  25.         $this->synchronizer->synchronizeTranslations($sellerPlan$product);
  26.         $this->synchronizer->synchronizePricing($sellerPlan$product$productVariant);
  27.         $sellerPlan->setProduct($product);
  28.         $this->entityManager->persist($product);
  29.         $this->entityManager->flush();
  30.     }
  31. }