platform/src/Core/Checkout/Customer/Subscriber/CustomerRemoteAddressSubscriber.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. class CustomerRemoteAddressSubscriber implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var EntityRepositoryInterface
  11.      */
  12.     private $customerRepository;
  13.     /**
  14.      * @var RequestStack
  15.      */
  16.     private $requestStack;
  17.     public function __construct(
  18.         EntityRepositoryInterface $customerRepository,
  19.         RequestStack $requestStack
  20.     ) {
  21.         $this->customerRepository $customerRepository;
  22.         $this->requestStack $requestStack;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             CustomerLoginEvent::class => 'updateRemoteAddressByLogin',
  28.         ];
  29.     }
  30.     public function updateRemoteAddressByLogin(CustomerLoginEvent $event): void
  31.     {
  32.         $request $this->requestStack
  33.             ->getMainRequest();
  34.         if (!$request) {
  35.             return;
  36.         }
  37.         $this->customerRepository->update([
  38.             [
  39.                 'id' => $event->getCustomer()->getId(),
  40.                 'remoteAddress' => $request->getClientIp(),
  41.             ],
  42.         ], $event->getContext());
  43.     }
  44. }