<?php
declare(strict_types=1);
namespace Madco\Mykey\Core\System\SalesChannel\Context;
use Madco\Mykey\Constants;
use Bodymed\Webshop\Core\Content\CustomerPartnerMap\CustomerPartnerMapEntity;
use Bodymed\Webshop\Core\Content\BodymedCenter\BodymedCenterService;
use Madco\Mykey\Struct\CurrentMykeyAffiliate;
use Madco\Mykey\Struct\CurrentMykeyPartner;
use Madco\Mykey\Struct\MykeyDataStruct;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* @deprecated
* @todo Muss ab Shopware 6.4.0 über SalesChannelContextFactory gelöst werden
*/
class SalesChannelContextService implements SalesChannelContextServiceInterface
{
private SalesChannelContextServiceInterface $originalService;
private RequestStack $requestStack;
private BodymedCenterService $bodymedCenterService;
public function __construct(
SalesChannelContextServiceInterface $originalService,
RequestStack $requestStack,
BodymedCenterService $bodymedCenterService
) {
$this->originalService = $originalService;
$this->requestStack = $requestStack;
$this->bodymedCenterService = $bodymedCenterService;
}
public function get(SalesChannelContextServiceParameters $parameters): SalesChannelContext
{
$salesChannelContext = $this->originalService->get($parameters);
$request = $this->requestStack->getCurrentRequest();
if ($request) {
$session = $request->getSession();
if ($salesChannelContext->getCustomer() instanceof CustomerEntity) {
$this->migrateMykeyDataFromCustomerToContext(
$salesChannelContext->getCustomer(),
$salesChannelContext
);
} else {
$this->migrateMykeyDataFromSessionToContext(
$session,
$salesChannelContext,
Constants::MYKEY_AFFILIATE_CONTEXT_EXTENSION_KEY
);
$this->migrateMykeyDataFromSessionToContext(
$session,
$salesChannelContext,
Constants::MYKEY_PARTNER_CONTEXT_EXTENSION_KEY
);
}
}
return $salesChannelContext;
}
private function migrateMykeyDataFromSessionToContext(
SessionInterface $session,
SalesChannelContext $salesChannelContext,
string $dataKey
): void {
$affiliateRequestAttribute = $session->get(
$dataKey,
null
);
if ($affiliateRequestAttribute instanceof MykeyDataStruct) {
$bodymedCenter = $this->bodymedCenterService->getCenterByMykeyAffiliate(
$affiliateRequestAttribute,
$salesChannelContext->getContext()
);
if ($bodymedCenter) {
$affiliateRequestAttribute->setBodymedCenter($bodymedCenter);
}
$salesChannelContext->addExtension(
$dataKey,
$affiliateRequestAttribute
);
$salesChannelContext->getContext()->addExtension(
$dataKey,
$affiliateRequestAttribute
);
}
}
private function migrateMykeyDataFromCustomerToContext(
CustomerEntity $customerEntity,
SalesChannelContext $salesChannelContext
): void {
$customerPartnerMap = $customerEntity->getExtension(Constants::MYKEY_CUSTOMER_PARTNER_MAP_EXTENSION_KEY);
if ($customerPartnerMap instanceof CustomerPartnerMapEntity) {
$partnerCenter = $customerPartnerMap->getPartnerBodymedCenter();
$affiliateCenter = $customerPartnerMap->getAffiliateBodymedCenter();
$affiliateData = null;
$partnerData = null;
if ($affiliateCenter) {
$affiliateData = CurrentMykeyAffiliate::fromCenter(
$affiliateCenter,
$customerPartnerMap->getAffiliateOrigin(),
$customerPartnerMap->getReferrer()
);
$salesChannelContext->addExtension(
Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE,
$affiliateData
);
$salesChannelContext->getContext()->addExtension(
Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE,
$affiliateData
);
}
if ($partnerCenter) {
$partnerData = CurrentMykeyPartner::fromCenter(
$partnerCenter,
$customerPartnerMap->getAffiliateOrigin(),
$customerPartnerMap->getReferrer(),
);
$salesChannelContext->addExtension(
Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE,
$partnerData
);
$salesChannelContext->getContext()->addExtension(
Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE,
$partnerData
);
}
$request = $this->requestStack->getCurrentRequest();
if ($request) {
$session = $request->getSession();
$sessionAffiliateData = $session->get(Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE);
if ($sessionAffiliateData) {
$this->migrateMykeyDataFromSessionToContext(
$session,
$salesChannelContext,
Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE
);
}
$sessionPartnerData = $session->get(Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE);
if ($sessionPartnerData) {
$this->migrateMykeyDataFromSessionToContext(
$session,
$salesChannelContext,
Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE
);
}
}
}
}
}