vendor/hofff/contao-consent-core/src/Action/StatusAction.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hofff\Contao\Consent\Core\Action;
  4. use Hofff\Contao\Consent\Bridge\ConsentToolManager;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use function json_encode;
  11. use function sprintf;
  12. use function str_replace;
  13. /** @internal */
  14. final class StatusAction extends AbstractConsentToolAwareAction
  15. {
  16.     /** @var string */
  17.     private $cookieName;
  18.     /** @var RouterInterface */
  19.     private $router;
  20.     /** @var string */
  21.     private $bannerPosition;
  22.     public function __construct(
  23.         ConsentToolManager $consentToolManager,
  24.         RouterInterface $router,
  25.         string $cookieName,
  26.         string $bannerPosition
  27.     ) {
  28.         parent::__construct($consentToolManager);
  29.         $this->cookieName     $cookieName;
  30.         $this->router         $router;
  31.         $this->bannerPosition $bannerPosition;
  32.     }
  33.     public function __invoke(Request $request): Response
  34.     {
  35.         $consentTool $this->createConsentTool($request);
  36.         $rootTag     $consentTool->rootTag();
  37.         if ($rootTag === null) {
  38.             throw new BadRequestHttpException();
  39.         }
  40.         $response = new Response(
  41.             sprintf(
  42.                 'var HOFFF_CONTENT_CONSENT_STATUS = %s',
  43.                 json_encode(
  44.                     [
  45.                         'cookieName'         => $this->cookieName '_' str_replace(
  46.                             'tag:',
  47.                             '',
  48.                             $rootTag->tagId()->toString()
  49.                         ),
  50.                         'tags'               => $rootTag,
  51.                         'theme'              => $rootTag->theme(),
  52.                         'effectiveFrom'      => $rootTag->effectiveFrom()->format('Y-m-d\TH:i:sP'),
  53.                         'consentIds'         => $rootTag->consentIds(),
  54.                         'banner'             => $request->query->getBoolean('banner'true),
  55.                         'bannerUrl'          => $this->router->generate(
  56.                             'hofff_contao_consent_banner',
  57.                             [
  58.                                 'pageId' => $request->query->getInt('pageId'),
  59.                                 'v'      => $rootTag->effectiveFrom()->getTimestamp(),
  60.                             ],
  61.                             RouterInterface::ABSOLUTE_URL
  62.                         ),
  63.                         'bannerPosition'     => $this->bannerPosition,
  64.                         'privacySettingsUrl' => $rootTag->privacySettingsPageUrl(),
  65.                     ],
  66.                     JsonResponse::DEFAULT_ENCODING_OPTIONS
  67.                 )
  68.             ),
  69.             200,
  70.             ['content-type' => 'application/javascript']
  71.         );
  72.         $response->headers->set('Cache-Control''no-cache, no-store');
  73.         $response->setPrivate();
  74.         return $response;
  75.     }
  76. }