vendor/pimcore/pimcore/lib/Targeting/Model/VisitorInfo.php line 25

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 Commercial License (PCL)
  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 PCL
  14.  */
  15. namespace Pimcore\Targeting\Model;
  16. use Pimcore\Model\Tool\Targeting\Rule;
  17. use Pimcore\Model\Tool\Targeting\TargetGroup;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. class VisitorInfo implements \IteratorAggregate
  21. {
  22.     const VISITOR_ID_COOKIE_NAME '_pc_vis';
  23.     const SESSION_ID_COOKIE_NAME '_pc_ses';
  24.     const ACTION_SCOPE_RESPONSE 'response';
  25.     /**
  26.      * @var Request
  27.      */
  28.     private $request;
  29.     /**
  30.      * @var string|null
  31.      */
  32.     private $visitorId;
  33.     /**
  34.      * @var string|null
  35.      */
  36.     private $sessionId;
  37.     /**
  38.      * Matched targeting rules
  39.      *
  40.      * @var Rule[]
  41.      */
  42.     private $matchingTargetingRules = [];
  43.     /**
  44.      * Assigned target groups with count
  45.      *
  46.      * @var TargetGroupAssignment[]
  47.      */
  48.     private $targetGroupAssignments = [];
  49.     /**
  50.      * Target group assignments sorted by count
  51.      *
  52.      * @var TargetGroupAssignment[]|null
  53.      */
  54.     private $sortedTargetGroupAssignments;
  55.     /**
  56.      * Plain list of assigned target groups
  57.      *
  58.      * @var TargetGroup[]|null
  59.      */
  60.     private $targetGroups;
  61.     /**
  62.      * @var array
  63.      */
  64.     private $data = [];
  65.     /**
  66.      * @var array
  67.      */
  68.     private $actions = [];
  69.     /**
  70.      * List of frontend data providers which are expected to provide data
  71.      *
  72.      * @var array
  73.      */
  74.     private $frontendDataProviders = [];
  75.     /**
  76.      * @var Response
  77.      */
  78.     private $response;
  79.     public function __construct(Request $requeststring $visitorId nullstring $sessionId null)
  80.     {
  81.         $this->request $request;
  82.         $this->visitorId $visitorId;
  83.         $this->sessionId $sessionId;
  84.     }
  85.     public static function fromRequest(Request $request): self
  86.     {
  87.         $visitorId $request->cookies->get(self::VISITOR_ID_COOKIE_NAME);
  88.         if (!empty($visitorId)) {
  89.             $visitorId = (string)$visitorId;
  90.         } else {
  91.             $visitorId null;
  92.         }
  93.         $sessionId $request->cookies->get(self::SESSION_ID_COOKIE_NAME);
  94.         if (!empty($sessionId)) {
  95.             $sessionId = (string)$sessionId;
  96.         } else {
  97.             $sessionId null;
  98.         }
  99.         return new static($request$visitorId$sessionId);
  100.     }
  101.     public function getRequest(): Request
  102.     {
  103.         return $this->request;
  104.     }
  105.     public function hasVisitorId(): bool
  106.     {
  107.         return !empty($this->visitorId);
  108.     }
  109.     /**
  110.      * @return string|null
  111.      */
  112.     public function getVisitorId()
  113.     {
  114.         return $this->visitorId;
  115.     }
  116.     public function hasSessionId(): bool
  117.     {
  118.         return !empty($this->sessionId);
  119.     }
  120.     /**
  121.      * @return string|null
  122.      */
  123.     public function getSessionId()
  124.     {
  125.         return $this->sessionId;
  126.     }
  127.     /**
  128.      * @return Rule[]
  129.      */
  130.     public function getMatchingTargetingRules(): array
  131.     {
  132.         return $this->matchingTargetingRules;
  133.     }
  134.     /**
  135.      * @param Rule[] $targetingRules
  136.      */
  137.     public function setMatchingTargetingRules(array $targetingRules = [])
  138.     {
  139.         $this->matchingTargetingRules = [];
  140.         foreach ($targetingRules as $targetingRule) {
  141.             $this->addMatchingTargetingRule($targetingRule);
  142.         }
  143.     }
  144.     public function addMatchingTargetingRule(Rule $targetingRule)
  145.     {
  146.         if (!in_array($targetingRule$this->matchingTargetingRulestrue)) {
  147.             $this->matchingTargetingRules[] = $targetingRule;
  148.         }
  149.     }
  150.     /**
  151.      * Returns target group assignments ordered by assignment count
  152.      *
  153.      * @return TargetGroupAssignment[]
  154.      */
  155.     public function getTargetGroupAssignments(): array
  156.     {
  157.         if (null !== $this->sortedTargetGroupAssignments) {
  158.             return $this->sortedTargetGroupAssignments;
  159.         }
  160.         /** @var TargetGroupAssignment[] $assignments */
  161.         $assignments array_values($this->targetGroupAssignments);
  162.         // sort reverse (highest count first)
  163.         usort($assignments, function (TargetGroupAssignment $aTargetGroupAssignment $b) {
  164.             $aCount $a->getCount();
  165.             $bCount $b->getCount();
  166.             if ($aCount === $bCount) {
  167.                 return 0;
  168.             }
  169.             return $aCount $bCount : -1;
  170.         });
  171.         $this->sortedTargetGroupAssignments $assignments;
  172.         return $this->sortedTargetGroupAssignments;
  173.     }
  174.     public function hasTargetGroupAssignment(TargetGroup $targetGroup): bool
  175.     {
  176.         return isset($this->targetGroupAssignments[$targetGroup->getId()]);
  177.     }
  178.     public function getTargetGroupAssignment(TargetGroup $targetGroup): TargetGroupAssignment
  179.     {
  180.         return $this->targetGroupAssignments[$targetGroup->getId()];
  181.     }
  182.     public function assignTargetGroup(TargetGroup $targetGroupint $count 1bool $overwrite false)
  183.     {
  184.         if ($count 1) {
  185.             throw new \InvalidArgumentException('Count must be greater than 0');
  186.         }
  187.         if (isset($this->targetGroupAssignments[$targetGroup->getId()])) {
  188.             if ($overwrite) {
  189.                 $this->targetGroupAssignments[$targetGroup->getId()]->setCount($count);
  190.             } else {
  191.                 $this->targetGroupAssignments[$targetGroup->getId()]->inc($count);
  192.             }
  193.         } else {
  194.             $this->targetGroupAssignments[$targetGroup->getId()] = new TargetGroupAssignment($targetGroup$count);
  195.         }
  196.         $this->targetGroups null;
  197.         $this->sortedTargetGroupAssignments null;
  198.     }
  199.     public function clearAssignedTargetGroup(TargetGroup $targetGroup)
  200.     {
  201.         if (isset($this->targetGroupAssignments[$targetGroup->getId()])) {
  202.             unset($this->targetGroupAssignments[$targetGroup->getId()]);
  203.             $this->targetGroups null;
  204.             $this->sortedTargetGroupAssignments null;
  205.         }
  206.     }
  207.     /**
  208.      * Returns assigned target groups ordered by assignment count
  209.      *
  210.      * @return TargetGroup[]
  211.      */
  212.     public function getAssignedTargetGroups(): array
  213.     {
  214.         if (null === $this->targetGroups) {
  215.             $this->targetGroups array_map(function (TargetGroupAssignment $assignment) {
  216.                 return $assignment->getTargetGroup();
  217.             }, $this->getTargetGroupAssignments());
  218.         }
  219.         return $this->targetGroups;
  220.     }
  221.     public function getFrontendDataProviders(): array
  222.     {
  223.         return $this->frontendDataProviders;
  224.     }
  225.     public function setFrontendDataProviders(array $providers)
  226.     {
  227.         $this->frontendDataProviders = [];
  228.         foreach ($providers as $provider) {
  229.             $this->addFrontendDataProvider($provider);
  230.         }
  231.     }
  232.     public function addFrontendDataProvider(string $key)
  233.     {
  234.         if (!in_array($key$this->frontendDataProviderstrue)) {
  235.             $this->frontendDataProviders[] = $key;
  236.         }
  237.     }
  238.     public function hasResponse(): bool
  239.     {
  240.         return null !== $this->response;
  241.     }
  242.     /**
  243.      * @return Response|null
  244.      */
  245.     public function getResponse()
  246.     {
  247.         return $this->response;
  248.     }
  249.     /**
  250.      * @param Response $response
  251.      */
  252.     public function setResponse(Response $response)
  253.     {
  254.         $this->response $response;
  255.     }
  256.     public function getData(): array
  257.     {
  258.         return $this->data;
  259.     }
  260.     public function setData(array $data)
  261.     {
  262.         $this->data $data;
  263.     }
  264.     public function getIterator()
  265.     {
  266.         return new \ArrayIterator($this->data);
  267.     }
  268.     public function has($key): bool
  269.     {
  270.         return isset($this->data[$key]);
  271.     }
  272.     public function get($key$default null)
  273.     {
  274.         return $this->data[$key] ?? $default;
  275.     }
  276.     public function set($key$value)
  277.     {
  278.         $this->data[$key] = $value;
  279.     }
  280.     public function addAction(array $action)
  281.     {
  282.         $this->actions[] = $action;
  283.     }
  284.     public function getActions(): array
  285.     {
  286.         return $this->actions;
  287.     }
  288.     public function hasActions(): bool
  289.     {
  290.         return count($this->actions) > 0;
  291.     }
  292. }