app/Plugin/RefineCookie42/Controller/CookieController.php line 44

Open in your IDE?
  1. <?php
  2. namespace Plugin\RefineCookie42\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Plugin\RefineCookie42\Entity\Config;
  5. use Plugin\RefineCookie42\Repository\ConfigRepository;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  10. class CookieController extends AbstractController
  11. {
  12.     /**
  13.      * @var ConfigRepository
  14.      */
  15.     protected $configRepository;
  16.     /**
  17.      * @var AuthorizationCheckerInterface
  18.      */
  19.     private $authorizationChecker;
  20.     /**
  21.      * ConfigController constructor.
  22.      *
  23.      * @param AuthorizationCheckerInterface $authorizationChecker
  24.      * @param ConfigRepository $configRepository
  25.      */
  26.     public function __construct(
  27.         AuthorizationCheckerInterface $authorizationChecker,
  28.         ConfigRepository $configRepository
  29.     ) {
  30.         $this->authorizationChecker $authorizationChecker;
  31.         $this->configRepository $configRepository;
  32.     }
  33.     /**
  34.      * @Route("/block/cookie", name="block_cookie")
  35.      * @Template("Block/cookie.twig")
  36.      */
  37.     public function index(Request $request)
  38.     {
  39.         $cookie_config $this->configRepository->get();
  40.         $publishTarget $cookie_config->getPublishTarget();
  41.         $cookie_expiration_date $cookie_config->getCookieExpirationDate();
  42.         // Cookie同意ポップアップの表示ロジック
  43.         $cookie false;
  44.         // Cookie同意ポップアップ表示制限を満たせているか
  45.         $cookie_target false;
  46.         // Cookie同意ポップアップ表示制限(全ユーザーに表示)
  47.         if ($publishTarget == 0) {
  48.             $cookie_target true;
  49.         // Cookie同意ポップアップ表示制限(ログインユーザーにのみ表示)
  50.         } elseif ($publishTarget == 1) {
  51.             if ($this->authorizationChecker->isGranted('ROLE_USER')) {
  52.                 $cookie_target true;
  53.             };
  54.         // Cookie同意ポップアップ表示制限(非ログインユーザーにのみ表示)
  55.         } elseif ($publishTarget == 2) {
  56.             if (!($this->authorizationChecker->isGranted('ROLE_USER'))) {
  57.                 $cookie_target true;
  58.             };
  59.         };
  60.         // 表示制限を満たせているか
  61.         if ($cookie_target) {
  62.             $cookie true;
  63.         };
  64.         return [
  65.             'cookie' => $cookie,
  66.             'cookie_expiration_date' => $cookie_expiration_date
  67.         ];
  68.     }
  69. }