custom/plugins/b2bsellerscore/addons/B2bOffer/Subscriber/CustomerDisplayNameSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace B2bOffer\Subscriber;
  3. use B2bOffer\Components\Offer\Aggregate\OfferCustomer\OfferCustomerEntity;
  4. use B2bOffer\Components\Offer\OfferEntity;
  5. use B2bSellersCore\Components\Customer\CustomerDisplayName;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class CustomerDisplayNameSubscriber implements EventSubscriberInterface
  9. {
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         return [
  13.             'b2b_offer.loaded' => 'onOfferLoadedAddDisplayName',
  14.             'b2b_offer_customer.loaded' => 'onOfferCustomerLoadedAddDisplayName'
  15.         ];
  16.     }
  17.     public function onOfferLoadedAddDisplayName(EntityLoadedEvent $event): void
  18.     {
  19.         /** @var OfferEntity $offerEntity */
  20.         foreach ($event->getEntities() as $offerEntity) {
  21.             if ($offerEntity->getOfferCustomer()) {
  22.                 $this->addDisplayNameExtension($offerEntity->getOfferCustomer());
  23.                 if ($offerEntity->getOfferCustomer()->getCustomer()) {
  24.                     $this->addDisplayNameExtension($offerEntity->getOfferCustomer()->getCustomer());
  25.                 }
  26.             }
  27.         }
  28.     }
  29.     public function onOfferCustomerLoadedAddDisplayName(EntityLoadedEvent $event): void
  30.     {
  31.         /** @var OfferCustomerEntity $offerCustomerEntity */
  32.         foreach ($event->getEntities() as $offerCustomerEntity) {
  33.             $this->addDisplayNameExtension($offerCustomerEntity);
  34.             if ($offerCustomerEntity->getCustomer()) {
  35.                 $this->addDisplayNameExtension($offerCustomerEntity->getCustomer());
  36.             }
  37.         }
  38.     }
  39.     private function addDisplayNameExtension($entity): void
  40.     {
  41.         $shortName $entity->getFirstName() . ' ' $entity->getLastName();
  42.         $name $shortName;
  43.         $longName $shortName;
  44.         if (!empty($entity->getTitle())) {
  45.             $longName $entity->getTitle() . ' ' $shortName;
  46.         }
  47.         if ($entity->getSalutation() && $entity->getSalutation()->getSalutationKey() !== 'not_specified') {
  48.             $name $entity->getSalutation()->getTranslation('displayName') . ' ' $name;
  49.             $longName $entity->getSalutation()->getTranslation('displayName') . ' ' $longName;
  50.         }
  51.         if (!empty($entity->getCompany())) {
  52.             $longName $entity->getCompany() . ' - ' $shortName;
  53.             $shortName $entity->getCompany();
  54.             $name $entity->getCompany();
  55.             if ((empty($entity->getFirstName()) || empty($entity->getLastName()))) {
  56.                 $longName $entity->getCompany();
  57.             }
  58.         }
  59.         if($entity instanceof OfferCustomerEntity) {
  60.             $entity->setDisplayName(new CustomerDisplayName($shortName$name$longName));
  61.             return;
  62.         }
  63.         $entity->addExtension('displayName', new CustomerDisplayName($shortName$name$longName));
  64.     }
  65. }