custom/plugins/NimbitsTracking/src/Subscriber/PartnerSubscriber.php line 113

Open in your IDE?
  1. <?php
  2. namespace Nimbits\NimbitsTracking\Subscriber;
  3. use Nimbits\NimbitsTracking\Service\SettingService;
  4. use Nimbits\NimbitsTracking\Service\VisitorCounterService;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class PartnerSubscriber implements EventSubscriberInterface
  14. {
  15.     private VisitorCounterService $visitorCounterService;
  16.     private SettingService $settings;
  17.     private EntityRepositoryInterface $customerRepository;
  18.     private EntityRepositoryInterface $orderRepository;
  19.     private RequestStack $requestStack;
  20.     public function __construct(
  21.         VisitorCounterService     $visitorCounterService,
  22.         SettingService            $settingService,
  23.         EntityRepositoryInterface $customerRepository,
  24.         EntityRepositoryInterface $orderRepository,
  25.         RequestStack              $requestStack
  26.     )
  27.     {
  28.         $this->visitorCounterService $visitorCounterService;
  29.         $this->settings $settingService;
  30.         $this->customerRepository $customerRepository;
  31.         $this->orderRepository $orderRepository;
  32.         $this->requestStack $requestStack;
  33.     }
  34.     /**
  35.      * @inheritDoc
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             KernelEvents::RESPONSE => 'onKernelResponse',
  41.             CustomerLoginEvent::class => 'onCustomerLogin',
  42.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced'
  43.         ];
  44.     }
  45.     public function onKernelResponse(ResponseEvent $event)
  46.     {
  47.         $this->visitorCounterService->countVisit($this->requestStack->getCurrentRequest(), $this->getPartner());
  48.         if (!empty($this->settings->getSetting('parameterName'))) {
  49.             $partner $event->getRequest()->query->get($this->settings->getSetting('parameterName'));
  50.             if (!empty($partner)) {
  51.                 if (empty($event->getRequest()->getSession()->get('nb_tracking_partner')) || $this->settings->getSetting('overrideSession')) {
  52.                     $event->getRequest()->getSession()->set('nb_tracking_partner'$partner);
  53.                 }
  54.                 if ($this->isCookieAllowed()) {
  55.                     if (empty($event->getRequest()->cookies->get($this->settings->getSetting('cookieName'))) || $this->settings->getSetting('overrideSession')) {
  56.                         $event->getResponse()->headers->setCookie(new Cookie(
  57.                             $this->settings->getSetting('cookieName'), $partner,
  58.                             time() + substr($this->settings->getSetting('cookieDuration'), 1)
  59.                         ));
  60.                     }
  61.                 }
  62.             } else {
  63.                 if (
  64.                     $this->isCookieAllowed() &&
  65.                     !empty($event->getRequest()->getSession()->get('nb_tracking_partner'))
  66.                     && empty($event->getRequest()->cookies->get($this->settings->getSetting('cookieName')))
  67.                 ) {
  68.                     $event->getResponse()->headers->setCookie(new Cookie(
  69.                         $this->settings->getSetting('cookieName'), $event->getRequest()->getSession()->get('nb_tracking_partner'),
  70.                         time() + substr($this->settings->getSetting('cookieDuration'), 1)
  71.                     ));
  72.                 } else if (
  73.                     !empty($event->getRequest()->cookies->get($this->settings->getSetting('cookieName')))
  74.                     && empty($event->getRequest()->getSession()->get('nb_tracking_partner'))
  75.                 ) {
  76.                     $event->getRequest()->getSession()->set('nb_tracking_partner'$event->getRequest()->cookies->get($this->settings->getSetting('cookieName')));
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     public function onCustomerLogin(CustomerLoginEvent $event)
  82.     {
  83.         // Filter for registration
  84.         if (!empty($event->getCustomer()->getLastLogin())) {
  85.             return;
  86.         }
  87.         $partner $this->getPartner();
  88.         if (!empty($partner)) {
  89.             $customer $event->getCustomer();
  90.             $this->customerRepository->update([[
  91.                 'id' => $customer->getId(),
  92.                 'customFields' => array_merge(
  93.                     $customer->getCustomFields() ?? [],
  94.                     ['nb_tracking_partner' => $partner]
  95.                 )
  96.             ]], $event->getContext());
  97.         }
  98.     }
  99.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event)
  100.     {
  101.         $partner $this->getPartner();
  102.         if (!empty($partner)) {
  103.             $order $event->getOrder();
  104.             $this->orderRepository->update([[
  105.                 'id' => $order->getId(),
  106.                 'customFields' => array_merge(
  107.                     $order->getCustomFields() ?? [],
  108.                     ['nb_tracking_partner' => $partner]
  109.                 )
  110.             ]], $event->getContext());
  111.         }
  112.     }
  113.     private function isCookieAllowed(): bool
  114.     {
  115.         return ($this->settings->getSetting('enableCookie') ?? true) &&
  116.             ($this->settings->getSetting('cookieType') == 'cookie.groupRequired' || !empty($this->requestStack->getCurrentRequest()->cookies->get($this->settings->getSetting('cookieName') . '_enabled')));
  117.     }
  118.     private function getPartner(): ?string
  119.     {
  120.         $request $this->requestStack->getCurrentRequest();
  121.         return $request->getSession()->get('nb_tracking_partner') ?? $request->cookies->get($this->settings->getSetting('cookieName')) ?? null;
  122.     }
  123. }