src/Component/ProductComparer/Provider/ComparerProvider.php line 100

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\ProductComparer\Provider;
  4. use BitBag\OpenMarketplace\Component\Customer\Model\CustomerInterface;
  5. use BitBag\OpenMarketplace\Component\ProductComparer\Entity\ComparerInterface;
  6. use BitBag\OpenMarketplace\Component\ProductComparer\Event\MissingCookieException;
  7. use BitBag\OpenMarketplace\Component\ProductComparer\Factory\ComparerFactoryInterface;
  8. use BitBag\OpenMarketplace\Component\ProductComparer\Repository\ComparerRepositoryInterface;
  9. use BitBag\OpenMarketplace\Component\Vendor\Entity\ShopUserInterface;
  10. use DateTime;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Sylius\Component\Core\Context\ShopperContextInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. final class ComparerProvider implements ComparerProviderInterface
  15. {
  16.     public function __construct(
  17.         private ComparerFactoryInterface $comparerFactory,
  18.         private ComparerRepositoryInterface $comparerRepository,
  19.         private ShopperContextInterface $shopperContext,
  20.         private EntityManagerInterface $entityManager,
  21.         private RequestStack $requestStack
  22.     ) {
  23.     }
  24.     /** @inheritDoc */
  25.     public function provide(): ComparerInterface
  26.     {
  27.         $shopUser $this->getShopUserFromContext();
  28.         $comparer $shopUser instanceof ShopUserInterface
  29.             $this->getComparerForShopUser($shopUser)
  30.             : $this->getComparerForAnonymousUser()
  31.         ;
  32.         $this->clearOldProducts($comparer);
  33.         return $comparer;
  34.     }
  35.     private function clearOldProducts(ComparerInterface $comparer): void
  36.     {
  37.         if (false === $this->isOlderThanOneDay($comparer)) {
  38.             return;
  39.         }
  40.         foreach ($comparer->getComparerProducts() as $comparerProduct) {
  41.             $comparer->removeComparerProduct($comparerProduct);
  42.         }
  43.         $comparer->setCreatedAt(new DateTime());
  44.         $this->entityManager->persist($comparer);
  45.         $this->entityManager->flush();
  46.     }
  47.     private function isOlderThanOneDay(ComparerInterface $comparer): bool
  48.     {
  49.         $createdAt $comparer->getCreatedAt();
  50.         if (null === $createdAt) {
  51.             return true;
  52.         }
  53.         $now = new DateTime();
  54.         $diff $now->diff($createdAt);
  55.         return <= $diff->days;
  56.     }
  57.     private function getShopUserFromContext(): ?ShopUserInterface
  58.     {
  59.         $customer $this->shopperContext->getCustomer();
  60.         if (!$customer instanceof CustomerInterface) {
  61.             return null;
  62.         }
  63.         $shopUser $customer->getUser();
  64.         if (!$shopUser instanceof ShopUserInterface) {
  65.             return null;
  66.         }
  67.         return $shopUser;
  68.     }
  69.     private function getComparerForShopUser(ShopUserInterface $shopUser): ComparerInterface
  70.     {
  71.         $comparer $this->comparerRepository->findOneByShopUser($shopUser);
  72.         if ($comparer instanceof ComparerInterface) {
  73.             return $comparer;
  74.         }
  75.         return $this->comparerFactory->createNewForShopUser($shopUser);
  76.     }
  77.     private function getComparerForAnonymousUser(): ComparerInterface
  78.     {
  79.         $session $this->requestStack->getSession();
  80.         $comparerToken $session->getId();
  81.         if ('' === $comparerToken) {
  82.             throw new MissingCookieException('Session ID cannot be null');
  83.         }
  84.         $comparer $this->comparerRepository->findOneByToken($comparerToken);
  85.         if ($comparer instanceof ComparerInterface) {
  86.             return $comparer;
  87.         }
  88.         return $this->comparerFactory->createNewForToken($comparerToken);
  89.     }
  90. }