app/Plugin/CheckProduct42/Service/CheckProductService.php line 32

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2024 SYSTEM FRIEND INC.
  4.  */
  5. namespace Plugin\CheckProduct42\Service;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Eccube\Entity\Master\ProductStatus;
  8. use Eccube\Entity\Product;
  9. class CheckProductService
  10. {
  11.     protected EntityManagerInterface $entityManager;
  12.     protected CookieHelper $cookieHelper;
  13.     protected \Plugin\CheckProduct42\Common\EccubeConfigEx $checkProductConfig;
  14.     public function __construct(
  15.         EntityManagerInterface $entityManager,
  16.         CookieHelper $cookieHelper,
  17.         \Plugin\CheckProduct42\Common\EccubeConfigEx $checkProductConfig
  18.     )
  19.     {
  20.         $this->entityManager $entityManager;
  21.         $this->cookieHelper $cookieHelper;
  22.         $this->checkProductConfig $checkProductConfig;
  23.     }
  24.     public function getCheckProducts()
  25.     {
  26.         $product_ids $this->cookieHelper->getProductIds() ? : [];
  27.         $CheckProducts = [];
  28.         $max_count $this->checkProductConfig->number_of_items();
  29.         // メモリ削減のためクッキーをもとに商品IDを取得
  30.         $temp $this->getProducts('p.id'$product_idstrue);
  31.         $show_product_ids = [];
  32.         foreach ($temp as $row) {
  33.             $id $row['id'];
  34.             $show_product_ids[$id] = $id;
  35.         }
  36.         // 表示する件数に絞る
  37.         $target_ids = [];
  38.         $count 0;
  39.         foreach ($product_ids as $id) {
  40.             if (isset($show_product_ids[$id])) {
  41.                 $target_ids[] = $id;
  42.                 $count++;
  43.             }
  44.             if ($count >= $max_count) {
  45.                 break;
  46.             }
  47.         }
  48.         // 表示するものだけに絞ったうえで実際のEntityを取得
  49.         $temp $this->getProducts('p'$target_idsfalse);
  50.         $Products = [];
  51.         /** @var Product $product */
  52.         foreach ($temp as $product) {
  53.             $Products[$product->getId()] = $product;
  54.         }
  55.         // 閲覧順に並び替え
  56.         foreach ($target_ids as $id) {
  57.             if (isset($Products[$id])) {
  58.                 $CheckProducts[] = $Products[$id];
  59.             }
  60.         }
  61.         return $CheckProducts;
  62.     }
  63.     /**
  64.      * @param $col
  65.      * @param $product_ids
  66.      * @param bool $isShow
  67.      * @return Product[]|array
  68.      */
  69.     protected function getProducts($col$product_ids$isShow=true)
  70.     {
  71.         $qb $this->entityManager->createQueryBuilder();
  72.         $qb
  73.             ->select($col)
  74.             ->from(Product::class, 'p')
  75.             ->where($qb->expr()->in('p.id'':product_ids'))
  76.             ->setParameter('product_ids'$product_ids)
  77.         ;
  78.         if ($isShow){
  79.             $qb
  80.                 ->andWhere('p.Status = :status')
  81.                 ->setParameter('status'ProductStatus::DISPLAY_SHOW)
  82.             ;
  83.         }
  84.         return $qb->getQuery()->getResult();
  85.     }
  86. }