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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Refine
  4.  *
  5.  * Copyright(c) 2021 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 Eccube\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.         $Products = array();
  55.         foreach ($productIds as $productId) {
  56.             /** @var \Eccube\Entity\Product $Product */
  57.             $Product $this->productRepository->find($productId);
  58.             $hasStock false;
  59.             if ($Product) {
  60.                 /** @var \Eccube\Entity\ProductClass $ProductClass */
  61.                 foreach ($Product->getProductClasses() as $ProductClass) {
  62.                     if ($ProductClass->isStockUnlimited() || $ProductClass->getStock() > 0) {
  63.                         $hasStock true;
  64.                         break;
  65.                     }
  66.                 }
  67.                 $Products[] = [
  68.                     'Product' => $Product,
  69.                     'hasStock' => $hasStock,
  70.                 ];
  71.             }
  72.         }
  73.         return [
  74.             'Products' => $Products,
  75.         ];
  76.     }
  77.     /**
  78.      * Cookie の取得
  79.      *
  80.      * @param Request $request
  81.      * @return array|mixed
  82.      */
  83.     private function getProductIdsFromCookie(Request $request)
  84.     {
  85.         $cookie $request->cookies->get(\Plugin\RefineCheckItem42\Event::COOKIE_NAME);
  86.         return json_decode($cookietrue) ?? [];
  87.     }
  88. }