src/Core/Subscriber/CustomerSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bodymed\Webshop\Core\Subscriber;
  4. use Bodymed\Webshop\Core\Checkout\Customer\CustomerPharmacyCooperation\CustomerPharmacyCooperationEntity;
  5. use Bodymed\Webshop\Core\Content\BodymedCenter\BodymedCenterEntity;
  6. use Bodymed\Webshop\Core\Content\CustomerPartnerMap\CustomerPartnerMapEntity;
  7. use Madco\Mykey\Constants;
  8. use Shopware\Core\Checkout\Customer\CustomerEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class CustomerSubscriber implements EventSubscriberInterface
  15. {
  16.     private EntityRepositoryInterface $customerPharmacyCooperationRepository;
  17.     private EntityRepositoryInterface $customerPartnerMapRepository;
  18.     private EntityRepositoryInterface $bodymedCenterRepository;
  19.     public function __construct(
  20.         EntityRepositoryInterface $customerPharmacyCooperationRepository,
  21.         EntityRepositoryInterface $customerPartnerMapRepository,
  22.         EntityRepositoryInterface $bodymedCenterRepository
  23.     ) {
  24.         $this->customerPharmacyCooperationRepository $customerPharmacyCooperationRepository;
  25.         $this->customerPartnerMapRepository $customerPartnerMapRepository;
  26.         $this->bodymedCenterRepository $bodymedCenterRepository;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             'customer.loaded' => 'onCustomerLoaded',
  32.         ];
  33.     }
  34.     public function onCustomerLoaded(EntityLoadedEvent $event): void
  35.     {
  36.         $context $event->getContext();
  37.         /* @var $customer CustomerEntity */
  38.         foreach ($event->getEntities() as $customer) {
  39.             if (false === $customer->hasExtension(CustomerPharmacyCooperationEntity::CUSTOMER_EXTENSION_KEY)) {
  40.                 $partnerCooperation $this->customerPharmacyCooperationRepository->search(
  41.                     (new Criteria())->addFilter(new EqualsFilter('pharmacyCustomerId'$customer->getId())),
  42.                     $context
  43.                 )->first();
  44.                 if ($partnerCooperation instanceof CustomerPharmacyCooperationEntity) {
  45.                     $customer->addExtension(CustomerPharmacyCooperationEntity::CUSTOMER_EXTENSION_KEY$partnerCooperation);
  46.                 }
  47.             }
  48.             if (false === $customer->hasExtension(Constants::MYKEY_CUSTOMER_PARTNER_MAP_EXTENSION_KEY)) {
  49.                 $customerPartnerMap $this->customerPartnerMapRepository->search(
  50.                     (new Criteria())->addFilter(new EqualsFilter('customerId'$customer->getId()))
  51.                                     ->addAssociation('partnerBodymedCenter')
  52.                                     ->addAssociation('affiliateBodymedCenter'), $context
  53.                 )->first();
  54.                 if (null === $customerPartnerMap){
  55.                     $customerPartnerMap $this->bodymedCenterRepository->search(
  56.                         (new Criteria())->addFilter(new EqualsFilter('customerNumber'$customer->getCustomerNumber())),
  57.                         $context
  58.                     )->first();
  59.                 }
  60.                 if ($customerPartnerMap instanceof CustomerPartnerMapEntity) {
  61.                     $customer->addExtension(Constants::MYKEY_CUSTOMER_PARTNER_MAP_EXTENSION_KEY$customerPartnerMap);
  62.                 } elseif($customerPartnerMap instanceof BodymedCenterEntity){
  63.                     $customer->addExtension('centerMap'$customerPartnerMap);
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }