custom/plugins/WbfkExtensions/src/Subscriber/CheckoutPageSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use WbfkExtensions\Service\GeneralTermsFactory;
  9. class CheckoutPageSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         protected readonly GeneralTermsFactory $generalTermsFactory,
  13.         protected readonly LoggerInterface $logger
  14.     )
  15.     {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             CustomerRegisterEvent::class => 'onCustomerRegisterEvent',
  21.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
  22.         ];
  23.     }
  24.     public function onCustomerRegisterEvent(CustomerRegisterEvent $event): void
  25.     {
  26.         $ipAddress $this->getIpAddress();
  27.         $this->generalTermsFactory->trackGeneralTerms($event->getCustomerId(), $ipAddress);
  28.     }
  29.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
  30.     {
  31.         try {
  32.             $ipAddress $this->getIpAddress();
  33.             $this->generalTermsFactory->trackGeneralTerms($event->getCustomerId(), $ipAddress$event->getOrderId());
  34.         } catch (\Exception $exception) {
  35.             //Don't let the App die because of tracking issues
  36.             $this->logger->error($exception->getMessage());
  37.         }
  38.     }
  39.     protected function getIpAddress(): ?string
  40.     {
  41.         if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  42.             // According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
  43.             // the format of the header is "X-Forwarded-For: <client>, <proxy1>, <proxy2>"
  44.             // we only care for the client IP
  45.             $ipAddresses explode(','$_SERVER['HTTP_X_FORWARDED_FOR']);
  46.             $ip trim($ipAddresses[0]);
  47.         } else {
  48.             $ip $_SERVER['REMOTE_ADDR'];
  49.         }
  50.         return $ip;
  51.     }
  52. }