vendor/coreshop/core-shop/src/CoreShop/Component/Tracking/Tracker/CompositeTracker.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * CoreShop
  5.  *
  6.  * This source file is available under two different licenses:
  7.  *  - GNU General Public License version 3 (GPLv3)
  8.  *  - CoreShop Commercial License (CCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) CoreShop GmbH (https://www.coreshop.org)
  13.  * @license    https://www.coreshop.org/license     GPLv3 and CCL
  14.  *
  15.  */
  16. namespace CoreShop\Component\Tracking\Tracker;
  17. use CoreShop\Component\Registry\ServiceRegistryInterface;
  18. use CoreShop\Component\Tracking\Extractor\TrackingExtractorInterface;
  19. class CompositeTracker implements TrackerInterface
  20. {
  21.     private ?bool $enabled null;
  22.     public function __construct(
  23.         private TrackingExtractorInterface $extractor,
  24.         private ServiceRegistryInterface $trackerRegistry,
  25.     ) {
  26.     }
  27.     public function isEnabled(): bool
  28.     {
  29.         if ($this->enabled !== null) {
  30.             return $this->enabled;
  31.         }
  32.         foreach ($this->trackerRegistry->all() as $tracker) {
  33.             if ($tracker->isEnabled()) {
  34.                 return $this->enabled true;
  35.             }
  36.         }
  37.         return $this->enabled false;
  38.     }
  39.     public function setEnabled(bool $enabled): void
  40.     {
  41.     }
  42.     public function trackProduct($product): void
  43.     {
  44.         if (!$this->isEnabled()) {
  45.             return;
  46.         }
  47.         $data $this->extractTrackingData($product);
  48.         $this->compositeTrackerCall('trackProduct', [$data]);
  49.     }
  50.     public function trackProductImpression($product): void
  51.     {
  52.         if (!$this->isEnabled()) {
  53.             return;
  54.         }
  55.         $data $this->extractTrackingData($product);
  56.         $this->compositeTrackerCall('trackProductImpression', [$data]);
  57.     }
  58.     public function trackCartAdd($cart$productfloat $quantity 1.0): void
  59.     {
  60.         if (!$this->isEnabled()) {
  61.             return;
  62.         }
  63.         $cart $this->extractTrackingData($cart);
  64.         $product $this->extractTrackingData($product);
  65.         $this->compositeTrackerCall('trackCartAdd', [$cart$product$quantity]);
  66.     }
  67.     public function trackCartRemove($cart$productfloat $quantity 1.0): void
  68.     {
  69.         if (!$this->isEnabled()) {
  70.             return;
  71.         }
  72.         $cart $this->extractTrackingData($cart);
  73.         $product $this->extractTrackingData($product);
  74.         $this->compositeTrackerCall('trackCartRemove', [$cart$product$quantity]);
  75.     }
  76.     public function trackCheckoutStep($cart$stepIdentifier nullbool $isFirstStep false$checkoutOption null): void
  77.     {
  78.         if (!$this->isEnabled()) {
  79.             return;
  80.         }
  81.         $cart $this->extractTrackingData($cart);
  82.         $this->compositeTrackerCall('trackCheckoutStep', [$cart$stepIdentifier$isFirstStep$checkoutOption]);
  83.     }
  84.     public function trackCheckoutComplete($order): void
  85.     {
  86.         if (!$this->isEnabled()) {
  87.             return;
  88.         }
  89.         $order $this->extractTrackingData($order);
  90.         $this->compositeTrackerCall('trackCheckoutComplete', [$order]);
  91.     }
  92.     private function compositeTrackerCall(string $function, array $data): void
  93.     {
  94.         /**
  95.          * @var TrackerInterface $tracker
  96.          */
  97.         foreach ($this->trackerRegistry->all() as $tracker) {
  98.             if (!$tracker->isEnabled()) {
  99.                 continue;
  100.             }
  101.             call_user_func_array([$tracker$function], $data);
  102.         }
  103.     }
  104.     private function extractTrackingData($object): array
  105.     {
  106.         return $this->extractor->updateMetadata($object);
  107.     }
  108. }