app/Customize/Controller/CustomTopController.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.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 Customize\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. class CustomTopController extends AbstractController
  19. {
  20.     public function __construct(
  21.         TokenStorageInterface $tokenStorage
  22.     ) {
  23.         $this->tokenStorage $tokenStorage;
  24.     }
  25.     /**
  26.      * @Route("/", name="homepage")
  27.      * @Template("index.twig")
  28.      */
  29.     public function index(Request $request)
  30.     {
  31.         $LoginTypeInfo $this->getLoginTypeInfo();
  32.         $LoginType $LoginTypeInfo['LoginType'];
  33.         if ( $LoginType == ) {
  34.             return $this->redirectToRoute('product_list');
  35.         }
  36.         // ブログ情報を取得
  37.         $response wp_remote_get($request->getSchemeAndHttpHost().$request->getBasePath().'/shop/wp-json/wp/v2/posts?per_page=3&_embed');
  38.         if ($response) {
  39.             if (is_wp_error($response)) {
  40.                 $posts false;
  41.             } else {
  42.                 $posts json_decode($response["body"]);
  43.             }
  44.         } else {
  45.             $posts false;
  46.         }
  47.         $blogDatas = [];
  48.         if ($posts) {
  49.             foreach ($posts as $data) {
  50.                 $item = [];
  51.                 $item['title'] = $data->title;
  52.                 $item['date'] = $data->date;
  53.                 $item['link'] = $data->link;
  54.                 $name 'wp:featuredmedia';
  55.                 if (isset($data->_embedded->{$name})) {
  56.                     $item['attachment'] = $data->_embedded->{$name}[0];
  57.                 }
  58.                 $name 'wp:term';
  59.                 if (isset($data->_embedded->{$name})) {
  60.                     $item['category'] = $data->_embedded->{$name}[0];
  61.                 }
  62.                 $blogDatas[] = $item;
  63.             }
  64.         }
  65.         return [
  66.             'blogDatas' => $blogDatas,
  67.         ];
  68.     }
  69.     function getLoginTypeInfo()
  70.     {
  71.         $LoginType 1;         //Default is guest
  72.         $Customer $this->getCurrentUser();
  73.         $ChainStore null;
  74.         $ContractType null;
  75.         if (is_object($Customer)) {
  76.             $ChainStore $Customer->getChainStore();
  77.             if(is_object($ChainStore)){
  78.                 $LoginType 3;         //ChainStore member
  79.                 $ContractType $ChainStore->getContractType();
  80.             }else{
  81.                 $LoginType 2;         //Normal member
  82.             }
  83.         }else{
  84.             $Customer null;
  85.         }
  86.         return [
  87.             'LoginType' => $LoginType,
  88.             'Customer' => $Customer,
  89.             'ChainStore' => $ChainStore,
  90.             'ContractType' => $ContractType,
  91.         ];
  92.     }
  93.     function getCurrentUser()
  94.     {
  95.         if(!$this->tokenStorage){
  96.             return null;
  97.         }
  98.         if (!$token $this->tokenStorage->getToken()) {
  99.             return null;
  100.         }
  101.         if (!$token->isAuthenticated()) {
  102.             return null;
  103.         }
  104.         if(!$user $token->getUser()){
  105.             return null;
  106.         }
  107.         if(is_object($user)){
  108.             return $user;
  109.         }
  110.         return null;
  111.     }
  112. }