src/Eccube/Controller/PageCountdownController.php line 48

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 Eccube\Controller;
  13. use Eccube\Repository\PageCountdownHistoryRepository;
  14. use Eccube\Repository\PageCountdownRepository;
  15. use Eccube\Service\PageCountdownService;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Knp\Component\Pager\Paginator;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  21. class PageCountdownController extends AbstractController
  22. {
  23.     protected $countdownRepository;
  24.     protected $countdownHistoryRepository;
  25.     protected $pageCountdownService;
  26.     
  27.     const ERROR_NO_USER 1;
  28.     public function __construct(
  29.         PageCountdownRepository        $countdownRepository,
  30.         PageCountdownHistoryRepository $countdownHistoryRepository,
  31.         PageCountdownService           $pageCountdownService
  32.     ) {
  33.         $this->countdownRepository $countdownRepository;
  34.         $this->countdownHistoryRepository $countdownHistoryRepository;
  35.         $this->pageCountdownService $pageCountdownService;
  36.     }
  37.     /**
  38.      * @Route("/countdowns/info", name="page_countdown_info", methods={"GET"})
  39.      *
  40.      */
  41.     public function info(Request $request)
  42.     {
  43.         $customer $this->getUser();
  44.         $countdownConfig $this->countdownRepository->getCountdown();
  45.         if (!$countdownConfig) {
  46.             return;
  47.         }
  48.         $numGetReward $this->countdownHistoryRepository->getRewardsToday($customer);
  49.         $lastReward $this->countdownHistoryRepository->getLastRewardByCustomer($customer);
  50.         $info $this->pageCountdownService->getInfoCountdownInfo($countdownConfig$numGetReward$lastReward);
  51.         return $this->json([
  52.             'active' => $info['active'] ?? false,
  53.             'key' => $info['key'] ?? '',
  54.             'time' => $info['time'] ?? 0,
  55.             'second' => $info['second'] ?? 0,
  56.             'point' => $info['point'] ?? 0,
  57.             'next_time_get_reward' => $countdownConfig->getInterval(),
  58.             'last_time_get_reward' => $lastReward $lastReward->getCreatedAt()->getTimestamp() : 0,
  59.         ]);
  60.     }
  61.     /**
  62.      * @Route("/countdowns/reward", name="page_countdown_reward", methods={"POST"})
  63.      *
  64.      */
  65.     public function getReward(Request $request)
  66.     {
  67.         $customer $this->getUser();
  68.         $countdownConfig $this->countdownRepository->getCountdown();
  69.         if (!$countdownConfig) {
  70.             return;
  71.         }
  72.         $errorCode $customer null self::ERROR_NO_USER;
  73.         $data $request->request->all();
  74.         $numGetReward $this->countdownHistoryRepository->getRewardsToday($customer);
  75.         $lastReward $this->countdownHistoryRepository->getLastRewardByCustomer($customer);
  76.         try {
  77.             $this->entityManager->beginTransaction();
  78.             $point $this->pageCountdownService->reward($data$countdownConfig$numGetReward$customer);
  79.             $this->entityManager->commit();
  80.             return $this->json([
  81.                 'point' => $point,
  82.                 'userPoint' => $customer ? ($customer->getPoint()) : 0,
  83.                 'second' => $countdownConfig->getSecond(),
  84.                 'next_time_get_reward' => $countdownConfig->getInterval(),
  85.                 'last_time_get_reward' => $lastReward $lastReward->getCreatedAt()->getTimestamp() : 0,
  86.                 'errorCode' => $errorCode
  87.             ]);
  88.         } catch (\Exception $e) {
  89.             $this->entityManager->rollBack();
  90.             throw $e;
  91.         }
  92.     }
  93. }