vendor/pimcore/pimcore/lib/Targeting/EventListener/VisitedPagesCountListener.php line 59

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\EventListener;
  16. use Pimcore\Event\Targeting\TargetingEvent;
  17. use Pimcore\Event\TargetingEvents;
  18. use Pimcore\Targeting\Service\VisitedPagesCounter;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class VisitedPagesCountListener implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var VisitedPagesCounter
  24.      */
  25.     private $visitedPagesCounter;
  26.     /**
  27.      * @var bool
  28.      */
  29.     private $recordPageCount false;
  30.     public function __construct(VisitedPagesCounter $visitedPagesCounter)
  31.     {
  32.         $this->visitedPagesCounter $visitedPagesCounter;
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             TargetingEvents::VISITED_PAGES_COUNT_MATCH => 'onVisitedPagesCountMatch'// triggered from conditions depending on page count
  41.             TargetingEvents::POST_RESOLVE => 'onPostResolveVisitorInfo',
  42.         ];
  43.     }
  44.     public function onVisitedPagesCountMatch()
  45.     {
  46.         // increment page count after matching proceeded
  47.         $this->recordPageCount true;
  48.     }
  49.     public function onPostResolveVisitorInfo(TargetingEvent $event)
  50.     {
  51.         // TODO currently the pages count is only recorded if there's a condition depending on
  52.         // the count. This is good for minimizing storage data and writes, but implies that the
  53.         // page count is not recorded if there's no rule with a condition depending on the page
  54.         // count. Alternatively this could be done blindly after resolving the visitor info, but
  55.         // that would trigger a write/increment on every request without actually needing the data.
  56.         if (!$this->recordPageCount) {
  57.             return;
  58.         }
  59.         $this->visitedPagesCounter->increment($event->getVisitorInfo());
  60.     }
  61. }