src/Component/ProductComparer/Manager/ProductComparerManager.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\ProductComparer\Manager;
  4. use BitBag\OpenMarketplace\Component\Product\Entity\ProductInterface;
  5. use BitBag\OpenMarketplace\Component\ProductComparer\Entity\ComparerInterface;
  6. use BitBag\OpenMarketplace\Component\ProductComparer\Entity\ComparerProductInterface;
  7. use BitBag\OpenMarketplace\Component\ProductComparer\Provider\ComparerProviderInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Sylius\Component\Resource\Factory\FactoryInterface;
  10. final class ProductComparerManager implements ProductComparerManagerInterface
  11. {
  12.     public function __construct(
  13.         private ComparerProviderInterface $provider,
  14.         private FactoryInterface $comparerProductFactory,
  15.         private EntityManagerInterface $entityManager,
  16.         ) {
  17.     }
  18.     /** @inheritDoc */
  19.     public function addProductToCompare(ProductInterface $product): void
  20.     {
  21.         $comparer $this->provider->provide();
  22.         /** @var ComparerProductInterface $comparerProduct */
  23.         $comparerProduct $this->comparerProductFactory->createNew();
  24.         $comparerProduct->setComparer($comparer);
  25.         $comparerProduct->setProduct($product);
  26.         $comparer->addComparerProduct($comparerProduct);
  27.         $this->entityManager->persist($comparer);
  28.         $this->entityManager->flush();
  29.     }
  30.     /** @inheritDoc */
  31.     public function removeProductFromCompare(ProductInterface $product): void
  32.     {
  33.         $comparer $this->provider->provide();
  34.         $comparerProduct $comparer->getComparerProductByProduct($product);
  35.         if (!$comparerProduct instanceof ComparerProductInterface) {
  36.             return;
  37.         }
  38.         $comparer->removeComparerProduct($comparerProduct);
  39.         $this->entityManager->persist($comparer);
  40.         $this->entityManager->flush();
  41.     }
  42.     /** @inheritDoc */
  43.     public function getComparer(): ComparerInterface
  44.     {
  45.         return $this->provider->provide();
  46.     }
  47. }