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

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