vendor/shopware/core/Checkout/Customer/Subscriber/CustomerDefaultSalutationSubscriber.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity;
  7. use Shopware\Core\Checkout\Order\OrderEvents;
  8. use Shopware\Core\Defaults;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\Feature;
  14. use Shopware\Core\System\Salutation\SalutationEntity;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class CustomerDefaultSalutationSubscriber implements EventSubscriberInterface
  17. {
  18.     private EntityRepositoryInterface $salutationRepository;
  19.     private ?SalutationEntity $defaultSalutation null;
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(EntityRepositoryInterface $salutationRepository)
  24.     {
  25.         $this->salutationRepository $salutationRepository;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         if (Feature::isActive('FEATURE_NEXT_7739')) {
  30.             return [];
  31.         }
  32.         return [
  33.             CustomerEvents::CUSTOMER_LOADED_EVENT => [
  34.                 ['loaded'],
  35.             ],
  36.             CustomerEvents::CUSTOMER_ADDRESS_LOADED_EVENT => [
  37.                 ['loaded'],
  38.             ],
  39.             OrderEvents::ORDER_ADDRESS_LOADED_EVENT => [
  40.                 ['loaded'],
  41.             ],
  42.         ];
  43.     }
  44.     public function loaded(EntityLoadedEvent $event): void
  45.     {
  46.         /** @var CustomerEntity|CustomerAddressEntity|OrderAddressEntity $entity */
  47.         foreach ($event->getEntities() as $entity) {
  48.             if ($entity->getSalutation() === null) {
  49.                 $entity->setSalutation($this->getDefaultSalutation($event->getContext()));
  50.             }
  51.             if ($entity->getSalutationId() === null) {
  52.                 $entity->setSalutationId($this->getDefaultSalutation($event->getContext())->getId());
  53.             }
  54.         }
  55.     }
  56.     private function getDefaultSalutation(Context $context): SalutationEntity
  57.     {
  58.         if ($this->defaultSalutation !== null) {
  59.             return $this->defaultSalutation;
  60.         }
  61.         $criteria = new Criteria([Defaults::SALUTATION]);
  62.         $criteria->setTitle('default-salutation-loading');
  63.         $this->defaultSalutation $this->salutationRepository->search($criteria$context)->first();
  64.         return $this->defaultSalutation;
  65.     }
  66. }