<?php
namespace Plugin\RefineCookie42\Controller;
use Eccube\Controller\AbstractController;
use Plugin\RefineCookie42\Entity\Config;
use Plugin\RefineCookie42\Repository\ConfigRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class CookieController extends AbstractController
{
/**
* @var ConfigRepository
*/
protected $configRepository;
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* ConfigController constructor.
*
* @param AuthorizationCheckerInterface $authorizationChecker
* @param ConfigRepository $configRepository
*/
public function __construct(
AuthorizationCheckerInterface $authorizationChecker,
ConfigRepository $configRepository
) {
$this->authorizationChecker = $authorizationChecker;
$this->configRepository = $configRepository;
}
/**
* @Route("/block/cookie", name="block_cookie")
* @Template("Block/cookie.twig")
*/
public function index(Request $request)
{
$cookie_config = $this->configRepository->get();
$publishTarget = $cookie_config->getPublishTarget();
$cookie_expiration_date = $cookie_config->getCookieExpirationDate();
// Cookie同意ポップアップの表示ロジック
$cookie = false;
// Cookie同意ポップアップ表示制限を満たせているか
$cookie_target = false;
// Cookie同意ポップアップ表示制限(全ユーザーに表示)
if ($publishTarget == 0) {
$cookie_target = true;
// Cookie同意ポップアップ表示制限(ログインユーザーにのみ表示)
} elseif ($publishTarget == 1) {
if ($this->authorizationChecker->isGranted('ROLE_USER')) {
$cookie_target = true;
};
// Cookie同意ポップアップ表示制限(非ログインユーザーにのみ表示)
} elseif ($publishTarget == 2) {
if (!($this->authorizationChecker->isGranted('ROLE_USER'))) {
$cookie_target = true;
};
};
// 表示制限を満たせているか
if ($cookie_target) {
$cookie = true;
};
return [
'cookie' => $cookie,
'cookie_expiration_date' => $cookie_expiration_date
];
}
}