custom/plugins/MadMykey/src/Core/System/SalesChannel/Context/SalesChannelContextService.php line 82

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Madco\Mykey\Core\System\SalesChannel\Context;
  4. use Madco\Mykey\Constants;
  5. use Bodymed\Webshop\Core\Content\CustomerPartnerMap\CustomerPartnerMapEntity;
  6. use Bodymed\Webshop\Core\Content\BodymedCenter\BodymedCenterService;
  7. use Madco\Mykey\Struct\CurrentMykeyAffiliate;
  8. use Madco\Mykey\Struct\CurrentMykeyPartner;
  9. use Madco\Mykey\Struct\MykeyDataStruct;
  10. use Shopware\Core\Checkout\Customer\CustomerEntity;
  11. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
  12. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. /**
  17.  * @deprecated
  18.  * @todo Muss ab Shopware 6.4.0 über SalesChannelContextFactory gelöst werden
  19.  */
  20. class SalesChannelContextService implements SalesChannelContextServiceInterface
  21. {
  22.     private SalesChannelContextServiceInterface $originalService;
  23.     private RequestStack $requestStack;
  24.     private BodymedCenterService $bodymedCenterService;
  25.     public function __construct(
  26.         SalesChannelContextServiceInterface $originalService,
  27.         RequestStack $requestStack,
  28.         BodymedCenterService $bodymedCenterService
  29.     ) {
  30.         $this->originalService $originalService;
  31.         $this->requestStack $requestStack;
  32.         $this->bodymedCenterService $bodymedCenterService;
  33.     }
  34.     public function get(SalesChannelContextServiceParameters $parameters): SalesChannelContext
  35.     {
  36.         $salesChannelContext $this->originalService->get($parameters);
  37.         $request $this->requestStack->getCurrentRequest();
  38.         if ($request) {
  39.             $session $request->getSession();
  40.             if ($salesChannelContext->getCustomer() instanceof CustomerEntity) {
  41.                 $this->migrateMykeyDataFromCustomerToContext(
  42.                     $salesChannelContext->getCustomer(),
  43.                     $salesChannelContext
  44.                 );
  45.             } else {
  46.                 $this->migrateMykeyDataFromSessionToContext(
  47.                     $session,
  48.                     $salesChannelContext,
  49.                     Constants::MYKEY_AFFILIATE_CONTEXT_EXTENSION_KEY
  50.                 );
  51.                 $this->migrateMykeyDataFromSessionToContext(
  52.                     $session,
  53.                     $salesChannelContext,
  54.                     Constants::MYKEY_PARTNER_CONTEXT_EXTENSION_KEY
  55.                 );
  56.             }
  57.         }
  58.         return $salesChannelContext;
  59.     }
  60.     private function migrateMykeyDataFromSessionToContext(
  61.         SessionInterface $session,
  62.         SalesChannelContext $salesChannelContext,
  63.         string $dataKey
  64.     ): void {
  65.         $affiliateRequestAttribute $session->get(
  66.             $dataKey,
  67.             null
  68.         );
  69.         if ($affiliateRequestAttribute instanceof MykeyDataStruct) {
  70.             $bodymedCenter $this->bodymedCenterService->getCenterByMykeyAffiliate(
  71.                 $affiliateRequestAttribute,
  72.                 $salesChannelContext->getContext()
  73.             );
  74.             if ($bodymedCenter) {
  75.                 $affiliateRequestAttribute->setBodymedCenter($bodymedCenter);
  76.             }
  77.             $salesChannelContext->addExtension(
  78.                 $dataKey,
  79.                 $affiliateRequestAttribute
  80.             );
  81.             $salesChannelContext->getContext()->addExtension(
  82.                 $dataKey,
  83.                 $affiliateRequestAttribute
  84.             );
  85.         }
  86.     }
  87.     private function migrateMykeyDataFromCustomerToContext(
  88.         CustomerEntity $customerEntity,
  89.         SalesChannelContext $salesChannelContext
  90.     ): void {
  91.         $customerPartnerMap $customerEntity->getExtension(Constants::MYKEY_CUSTOMER_PARTNER_MAP_EXTENSION_KEY);
  92.         if ($customerPartnerMap instanceof CustomerPartnerMapEntity) {
  93.             $partnerCenter $customerPartnerMap->getPartnerBodymedCenter();
  94.             $affiliateCenter $customerPartnerMap->getAffiliateBodymedCenter();
  95.             $affiliateData null;
  96.             $partnerData null;
  97.             if ($affiliateCenter) {
  98.                 $affiliateData CurrentMykeyAffiliate::fromCenter(
  99.                     $affiliateCenter,
  100.                     $customerPartnerMap->getAffiliateOrigin(),
  101.                     $customerPartnerMap->getReferrer()
  102.                 );
  103.                 $salesChannelContext->addExtension(
  104.                     Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE,
  105.                     $affiliateData
  106.                 );
  107.                 $salesChannelContext->getContext()->addExtension(
  108.                     Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE,
  109.                     $affiliateData
  110.                 );
  111.             }
  112.             if ($partnerCenter) {
  113.                 $partnerData CurrentMykeyPartner::fromCenter(
  114.                     $partnerCenter,
  115.                     $customerPartnerMap->getAffiliateOrigin(),
  116.                     $customerPartnerMap->getReferrer(),
  117.                 );
  118.                 $salesChannelContext->addExtension(
  119.                     Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE,
  120.                     $partnerData
  121.                 );
  122.                 $salesChannelContext->getContext()->addExtension(
  123.                     Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE,
  124.                     $partnerData
  125.                 );
  126.             }
  127.             $request $this->requestStack->getCurrentRequest();
  128.             if ($request) {
  129.                 $session $request->getSession();
  130.                 $sessionAffiliateData $session->get(Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE);
  131.                 if ($sessionAffiliateData) {
  132.                     $this->migrateMykeyDataFromSessionToContext(
  133.                         $session,
  134.                         $salesChannelContext,
  135.                         Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE
  136.                     );
  137.                 }
  138.                 $sessionPartnerData $session->get(Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE);
  139.                 if ($sessionPartnerData) {
  140.                     $this->migrateMykeyDataFromSessionToContext(
  141.                         $session,
  142.                         $salesChannelContext,
  143.                         Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE
  144.                     );
  145.                 }
  146.             }
  147.         }
  148.     }
  149. }