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

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