app/Plugin/RefineCheckItem42/Controller/RefineCheckItemController.php line 96

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Refine
  4.  *
  5.  * Copyright(c) 2023 Refine Co.,Ltd. All Rights Reserved.
  6.  *
  7.  * https://www.re-fine.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\RefineCheckItem42\Controller;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Eccube\Controller\AbstractController;
  15. use Plugin\RefineCheckItem42\Repository\ProductRepository;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * Class RefineCheckItemController.
  21.  */
  22. class RefineCheckItemController extends AbstractController
  23. {
  24.     /**
  25.      * @var ProductRepository
  26.      */
  27.     private $productRepository;
  28.     /**
  29.      * RefineCheckItemController constructor.
  30.      *
  31.      * @param ProductRepository $productRepository
  32.      * @throws \Exception
  33.      */
  34.     public function __construct(ProductRepository $productRepository)
  35.     {
  36.         $this->productRepository $productRepository;
  37.     }
  38.     /**
  39.      * アクセス履歴
  40.      *
  41.      * @param Request $request
  42.      *
  43.      * @return array
  44.      *
  45.      * @Route("/block/refine_check_item42", name="block_refine_check_item")
  46.      * @Template("Block/refine_check_item.twig")
  47.      */
  48.     public function index(Request $request)
  49.     {
  50.         // Cookie 取得
  51.         $productIds = (new ArrayCollection($this->getProductIdsFromCookie($request)))->toArray();
  52.         // 表示用に順序を逆にする
  53.         $productIds array_reverse($productIds);
  54.         // 対象商品を取得する
  55.         $CheckedProducts $this->productRepository->getCheckedProducts($productIds);
  56.         $Products = array();
  57.         foreach ($CheckedProducts as $Product) {
  58.             $hasStock false;
  59.             /** @var \Eccube\Entity\ProductClass $ProductClass */
  60.             foreach ($Product->getProductClasses() as $ProductClass) {
  61.                 if ($ProductClass->isStockUnlimited() || $ProductClass->getStock() > 0) {
  62.                     $hasStock true;
  63.                     break;
  64.                 }
  65.             }
  66.             $Products[] = [
  67.                 'Product' => $Product,
  68.                 'hasStock' => $hasStock,
  69.             ];
  70.         }
  71.         return [
  72.             'Products' => $Products,
  73.         ];
  74.     }
  75.     /**
  76.      * Cookie の取得
  77.      *
  78.      * @param Request $request
  79.      * @return array|mixed
  80.      */
  81.     private function getProductIdsFromCookie(Request $request)
  82.     {
  83.         $cookie $request->cookies->get(\Plugin\RefineCheckItem42\Event::COOKIE_NAME);
  84.         return json_decode($cookietrue) ?? [];
  85.     }
  86. }