vendor/pimcore/pimcore/lib/Targeting/Storage/Cookie/AbstractCookieSaveHandler.php line 81

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Enterprise License (PEL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  14.  */
  15. namespace Pimcore\Targeting\Storage\Cookie;
  16. use Symfony\Component\HttpFoundation\Cookie;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. abstract class AbstractCookieSaveHandler implements CookieSaveHandlerInterface
  21. {
  22.     /**
  23.      * @var array
  24.      */
  25.     protected $options = [];
  26.     /**
  27.      * @param array $options
  28.      */
  29.     public function __construct(array $options = [])
  30.     {
  31.         $resolver = new OptionsResolver();
  32.         $this->configureOptions($resolver);
  33.         $this->options $resolver->resolve($options);
  34.     }
  35.     protected function configureOptions(OptionsResolver $resolver)
  36.     {
  37.         $resolver->setDefaults([
  38.             'domain' => null,
  39.             'secure' => false,
  40.             'httpOnly' => true
  41.         ]);
  42.         $resolver->setAllowedTypes('domain', ['null''string']);
  43.         $resolver->setAllowedTypes('secure', ['bool']);
  44.         $resolver->setAllowedTypes('httpOnly', ['bool']);
  45.     }
  46.     /**
  47.      * @inheritdoc
  48.      */
  49.     public function load(Request $requeststring $scopestring $name): array
  50.     {
  51.         $data $request->cookies->get($namenull);
  52.         $result $this->parseData($scope$name$data);
  53.         return $result;
  54.     }
  55.     /**
  56.      * @inheritdoc
  57.      */
  58.     public function save(Response $responsestring $scopestring $name$expire$data)
  59.     {
  60.         $value $this->prepareData($scope$name$expire$data);
  61.         $response->headers->setCookie(new Cookie(
  62.             $name,
  63.             $value,
  64.             $expire,
  65.             '/',
  66.             null,
  67.             false,
  68.             true
  69.         ));
  70.     }
  71.     /**
  72.      * Parse loaded data
  73.      *
  74.      * @param string $scope
  75.      * @param string $name
  76.      * @param string|null $data
  77.      *
  78.      * @return array
  79.      */
  80.     abstract protected function parseData(string $scopestring $name$data): array;
  81.     /**
  82.      * Prepare data for saving
  83.      *
  84.      * @param string $scope
  85.      * @param string $name
  86.      * @param int|string|\DateTimeInterface $expire
  87.      * @param array|null $data
  88.      *
  89.      * @return string|null
  90.      */
  91.     abstract protected function prepareData(string $scopestring $name$expire$data);
  92. }