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

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. /**
  17.  * @deprecated tag:v6.5.0 - reason:remove-subscriber - Will be removed with FEATURE_NEXT_7739
  18.  */
  19. class CustomerDefaultSalutationSubscriber implements EventSubscriberInterface
  20. {
  21.     private EntityRepositoryInterface $salutationRepository;
  22.     private ?SalutationEntity $defaultSalutation null;
  23.     /**
  24.      * @internal
  25.      */
  26.     public function __construct(EntityRepositoryInterface $salutationRepository)
  27.     {
  28.         $this->salutationRepository $salutationRepository;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         if (Feature::isActive('FEATURE_NEXT_7739')) {
  33.             return [];
  34.         }
  35.         return [
  36.             CustomerEvents::CUSTOMER_LOADED_EVENT => [
  37.                 ['loaded'],
  38.             ],
  39.             CustomerEvents::CUSTOMER_ADDRESS_LOADED_EVENT => [
  40.                 ['loaded'],
  41.             ],
  42.             OrderEvents::ORDER_ADDRESS_LOADED_EVENT => [
  43.                 ['loaded'],
  44.             ],
  45.         ];
  46.     }
  47.     public function loaded(EntityLoadedEvent $event): void
  48.     {
  49.         /** @var CustomerEntity|CustomerAddressEntity|OrderAddressEntity $entity */
  50.         foreach ($event->getEntities() as $entity) {
  51.             if ($entity->getSalutation() === null) {
  52.                 $entity->setSalutation($this->getDefaultSalutation($event->getContext()));
  53.             }
  54.             if ($entity->getSalutationId() === null) {
  55.                 $entity->setSalutationId($this->getDefaultSalutation($event->getContext())->getId());
  56.             }
  57.         }
  58.     }
  59.     private function getDefaultSalutation(Context $context): SalutationEntity
  60.     {
  61.         if ($this->defaultSalutation !== null) {
  62.             return $this->defaultSalutation;
  63.         }
  64.         $criteria = new Criteria([Defaults::SALUTATION]);
  65.         $criteria->setTitle('default-salutation-loading');
  66.         $this->defaultSalutation $this->salutationRepository->search($criteria$context)->first();
  67.         return $this->defaultSalutation;
  68.     }
  69. }