<?php
declare(strict_types=1);
namespace Bodymed\Webshop\Core\Subscriber;
use Bodymed\Webshop\Core\Checkout\Customer\CustomerPharmacyCooperation\CustomerPharmacyCooperationEntity;
use Bodymed\Webshop\Core\Content\BodymedCenter\BodymedCenterEntity;
use Bodymed\Webshop\Core\Content\CustomerPartnerMap\CustomerPartnerMapEntity;
use Madco\Mykey\Constants;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomerSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $customerPharmacyCooperationRepository;
private EntityRepositoryInterface $customerPartnerMapRepository;
private EntityRepositoryInterface $bodymedCenterRepository;
public function __construct(
EntityRepositoryInterface $customerPharmacyCooperationRepository,
EntityRepositoryInterface $customerPartnerMapRepository,
EntityRepositoryInterface $bodymedCenterRepository
) {
$this->customerPharmacyCooperationRepository = $customerPharmacyCooperationRepository;
$this->customerPartnerMapRepository = $customerPartnerMapRepository;
$this->bodymedCenterRepository = $bodymedCenterRepository;
}
public static function getSubscribedEvents(): array
{
return [
'customer.loaded' => 'onCustomerLoaded',
];
}
public function onCustomerLoaded(EntityLoadedEvent $event): void
{
$context = $event->getContext();
/* @var $customer CustomerEntity */
foreach ($event->getEntities() as $customer) {
if (false === $customer->hasExtension(CustomerPharmacyCooperationEntity::CUSTOMER_EXTENSION_KEY)) {
$partnerCooperation = $this->customerPharmacyCooperationRepository->search(
(new Criteria())->addFilter(new EqualsFilter('pharmacyCustomerId', $customer->getId())),
$context
)->first();
if ($partnerCooperation instanceof CustomerPharmacyCooperationEntity) {
$customer->addExtension(CustomerPharmacyCooperationEntity::CUSTOMER_EXTENSION_KEY, $partnerCooperation);
}
}
if (false === $customer->hasExtension(Constants::MYKEY_CUSTOMER_PARTNER_MAP_EXTENSION_KEY)) {
$customerPartnerMap = $this->customerPartnerMapRepository->search(
(new Criteria())->addFilter(new EqualsFilter('customerId', $customer->getId()))
->addAssociation('partnerBodymedCenter')
->addAssociation('affiliateBodymedCenter'), $context
)->first();
if (null === $customerPartnerMap){
$customerPartnerMap = $this->bodymedCenterRepository->search(
(new Criteria())->addFilter(new EqualsFilter('customerNumber', $customer->getCustomerNumber())),
$context
)->first();
}
if ($customerPartnerMap instanceof CustomerPartnerMapEntity) {
$customer->addExtension(Constants::MYKEY_CUSTOMER_PARTNER_MAP_EXTENSION_KEY, $customerPartnerMap);
} elseif($customerPartnerMap instanceof BodymedCenterEntity){
$customer->addExtension('centerMap', $customerPartnerMap);
}
}
}
}
}