app/Plugin/RecentlyViewedProducts/Service/RecentlyViewedProductService.php line 63

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the RecentlyViewedProducts Plugin
  4.  *
  5.  * Copyright (C) 2023 Diezon.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\RecentlyViewedProducts\Service;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Eccube\Repository\ProductRepository;
  13. use Eccube\Util\StringUtil;
  14. use Eccube\Entity\Customer;
  15. use Eccube\Entity\Product;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Plugin\RecentlyViewedProducts\Entity\RecentlyViewedProduct;
  19. use Plugin\RecentlyViewedProducts\Repository\RecentlyViewedProductRepository;
  20. class RecentlyViewedProductService
  21. {
  22.     const SESSION_KEY 'plugin.recently_viewed_products.identification_key';
  23.     /**
  24.      * @var SessionInterface
  25.      */
  26.     protected $session;
  27.     /**
  28.      * @var \Doctrine\ORM\EntityManagerInterface
  29.      */
  30.     protected $entityManager;
  31.     /**
  32.      * @var ProductRepository
  33.      */
  34.     protected $productRepository;
  35.     /**
  36.      * @var RecentlyViewedProductRepository
  37.      */
  38.     protected $recentlyViewedProductRepository;
  39.     /**
  40.      * @var TokenStorageInterface
  41.      */
  42.     protected $tokenStorage;
  43.     /**
  44.      * RecentlyViewedProductService constructor.
  45.      */
  46.     public function __construct(
  47.         RequestStack $requestStack,
  48.         EntityManagerInterface $entityManager,
  49.         ProductRepository $productRepository,
  50.         RecentlyViewedProductRepository $recentlyViewedProductRepository,
  51.         TokenStorageInterface $tokenStorage
  52.     ) {
  53.         $this->session $requestStack->getSession();
  54.         $this->entityManager $entityManager;
  55.         $this->productRepository $productRepository;
  56.         $this->recentlyViewedProductRepository $recentlyViewedProductRepository;
  57.         $this->tokenStorage $tokenStorage;
  58.     }
  59.     /**
  60.      * @param Customer $Customer
  61.      * @return array
  62.      */
  63.     public function getPersistedProducts($Customer null)
  64.     {
  65.         return $this->recentlyViewedProductRepository->getRecentlyViewedProductsByCustomer($Customer $Customer $this->getUser());
  66.     }
  67.     /**
  68.      * @param Customer $Customer
  69.      * @return array
  70.      */
  71.     public function getAllPersistedProducts($Customer null)
  72.     {
  73.         return $this->recentlyViewedProductRepository->getRecentlyViewedProductsByCustomer($Customer $Customer $this->getUser(), true);
  74.     }
  75.     /**
  76.      * @return array
  77.      */
  78.     public function getSessionProducts()
  79.     {
  80.         $identificationKey $this->session->get(self::SESSION_KEYnull);
  81.         if (empty($identificationKey)) {
  82.             return [];
  83.         }
  84.         return $this->recentlyViewedProductRepository->getRecentlyViewedProductsByIdentificationKey($identificationKey);
  85.     }
  86.     /**
  87.      * @return array
  88.      */
  89.     public function getAllSessionProducts()
  90.     {
  91.         $identificationKey $this->session->get(self::SESSION_KEYnull);
  92.         if (empty($identificationKey)) {
  93.             return [];
  94.         }
  95.         return $this->recentlyViewedProductRepository->getRecentlyViewedProductsByIdentificationKey($identificationKeytrue);
  96.     }
  97.     /**
  98.      * @param Customer $Customer
  99.      * @return void
  100.      */
  101.     public function mergeFromPersistedProducts(Customer $Customer)
  102.     {
  103.         $PersistedProducts $this->getPersistedProducts();
  104.         $SessionProducts $this->getSessionProducts();
  105.         $productInfo = [];
  106.         foreach ($PersistedProducts as $PeristedProduct) {
  107.             $productInfo[$PeristedProduct->getProduct()->getId()] = $PeristedProduct;
  108.         }
  109.         foreach ($SessionProducts as $SessionProduct) {
  110.             if (isset($productInfo[$SessionProduct->getProduct()->getId()])) {
  111.                 $PeristedProduct $productInfo[$SessionProduct->getProduct()->getId()];
  112.                 if ($SessionProduct->getViewedDate() > $PeristedProduct->getViewedDate()) {
  113.                     $PeristedProduct->setViewedDate($SessionProduct->getViewedDate());
  114.                 }
  115.                 // 不要なレコードは削除
  116.                 $this->entityManager->remove($SessionProduct);
  117.             } else {
  118.                 $SessionProduct->setCustomer($Customer);
  119.             }
  120.         }
  121.         $this->entityManager->flush();
  122.     }
  123.     /**
  124.      * @return array
  125.      */
  126.     public function getRecentlyViewedProducts()
  127.     {
  128.         if ($this->getUser()) {
  129.             return $this->getPersistedProducts();
  130.         } else {
  131.             return $this->getSessionProducts();
  132.         }
  133.     }
  134.     /**
  135.      * @param Product $Product
  136.      * @return void
  137.      */
  138.     public function addViewedProduct($Product)
  139.     {
  140.         $Customer $this->getUser();
  141.         if ($Customer) {
  142.             $RecentlyViewedProduct $this->recentlyViewedProductRepository->findOneBy([
  143.                 'Customer' => $Customer,
  144.                 'Product' => $Product,
  145.             ]);
  146.         } else {
  147.             $identificationKey $this->getIdentificationKey();
  148.             $RecentlyViewedProduct $this->recentlyViewedProductRepository->findOneBy([
  149.                 'identification_key' => $identificationKey,
  150.                 'Product' => $Product,
  151.             ]);
  152.         }
  153.         if (!$RecentlyViewedProduct) {
  154.             $RecentlyViewedProduct = new RecentlyViewedProduct();
  155.             $RecentlyViewedProduct->setProduct($Product);
  156.             if ($Customer) {
  157.                 $RecentlyViewedProduct->setCustomer($Customer);
  158.             } else {
  159.                 $RecentlyViewedProduct->setIdentificationKey($identificationKey);
  160.             }
  161.             $this->entityManager->persist($RecentlyViewedProduct);
  162.         }
  163.         $RecentlyViewedProduct->setViewedDate(new \DateTime());
  164.         $this->entityManager->flush($RecentlyViewedProduct);
  165.     }
  166.     /**
  167.      * @return void
  168.      */
  169.     public function clear()
  170.     {
  171.         if ($this->getUser()) {
  172.             $RecentlyViewedProducts $this->getAllPersistedProducts();
  173.         } else {
  174.             $RecentlyViewedProducts $this->getAllSessionProducts();
  175.         }
  176.         foreach ($RecentlyViewedProducts as $RecentlyViewedProduct) {
  177.             $this->entityManager->remove($RecentlyViewedProduct);
  178.         }
  179.         $this->entityManager->flush();
  180.     }
  181.     /**
  182.      * @return string
  183.      */
  184.     protected function getIdentificationKey()
  185.     {
  186.         if ($this->session->has(self::SESSION_KEY)) {
  187.             return $this->session->get(self::SESSION_KEY);
  188.         }
  189.         do {
  190.             $identificationKey StringUtil::random(32);
  191.             $RecentlyViewedProducts $this->recentlyViewedProductRepository->findBy(['identification_key' => $identificationKey], [], 1);
  192.         } while (count($RecentlyViewedProducts) > 0);
  193.         $this->session->set(self::SESSION_KEY$identificationKey);
  194.         return $identificationKey;
  195.     }
  196.     /**
  197.      * @return Customer|null
  198.      */
  199.     protected function getUser()
  200.     {
  201.         if (null === $token $this->tokenStorage->getToken()) {
  202.             return;
  203.         }
  204.         if (!is_object($user $token->getUser())) {
  205.             // e.g. anonymous authentication
  206.             return;
  207.         }
  208.         return $user;
  209.     }
  210. }