vendor/shopware/core/Checkout/Customer/Subscriber/CustomerGroupSubscriber.php line 81

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Cocur\Slugify\SlugifyInterface;
  4. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupCollection;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroupTranslation\CustomerGroupTranslationCollection;
  6. use Shopware\Core\Content\Seo\SeoUrlPersister;
  7. use Shopware\Core\Defaults;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Shopware\Core\System\Language\LanguageCollection;
  16. use Shopware\Core\System\Language\LanguageEntity;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. /**
  19.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  20.  */
  21. class CustomerGroupSubscriber implements EventSubscriberInterface
  22. {
  23.     private const ROUTE_NAME 'frontend.account.customer-group-registration.page';
  24.     private EntityRepositoryInterface $customerGroupRepository;
  25.     private SeoUrlPersister $persister;
  26.     private SlugifyInterface $slugify;
  27.     private EntityRepositoryInterface $seoUrlRepository;
  28.     private EntityRepositoryInterface $languageRepository;
  29.     /**
  30.      * @internal
  31.      */
  32.     public function __construct(
  33.         EntityRepositoryInterface $customerGroupRepository,
  34.         EntityRepositoryInterface $seoUrlRepository,
  35.         EntityRepositoryInterface $languageRepository,
  36.         SeoUrlPersister $persister,
  37.         SlugifyInterface $slugify
  38.     ) {
  39.         $this->customerGroupRepository $customerGroupRepository;
  40.         $this->seoUrlRepository $seoUrlRepository;
  41.         $this->persister $persister;
  42.         $this->slugify $slugify;
  43.         $this->languageRepository $languageRepository;
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             'customer_group_translation.written' => 'updatedCustomerGroup',
  49.             'customer_group_registration_sales_channels.written' => 'newSalesChannelAddedToCustomerGroup',
  50.             'customer_group_translation.deleted' => 'deleteCustomerGroup',
  51.         ];
  52.     }
  53.     public function newSalesChannelAddedToCustomerGroup(EntityWrittenEvent $event): void
  54.     {
  55.         $ids = [];
  56.         foreach ($event->getWriteResults() as $writeResult) {
  57.             /** @var array<string, string> $pk */
  58.             $pk $writeResult->getPrimaryKey();
  59.             $ids[] = $pk['customerGroupId'];
  60.         }
  61.         if (\count($ids) === 0) {
  62.             return;
  63.         }
  64.         $this->createUrls($ids$event->getContext());
  65.     }
  66.     public function updatedCustomerGroup(EntityWrittenEvent $event): void
  67.     {
  68.         $ids = [];
  69.         foreach ($event->getWriteResults() as $writeResult) {
  70.             if ($writeResult->hasPayload('registrationTitle')) {
  71.                 /** @var array<string, string> $pk */
  72.                 $pk $writeResult->getPrimaryKey();
  73.                 $ids[] = $pk['customerGroupId'];
  74.             }
  75.         }
  76.         if (\count($ids) === 0) {
  77.             return;
  78.         }
  79.         $this->createUrls($ids$event->getContext());
  80.     }
  81.     public function deleteCustomerGroup(EntityDeletedEvent $event): void
  82.     {
  83.         $ids = [];
  84.         foreach ($event->getWriteResults() as $writeResult) {
  85.             /** @var array<string, string> $pk */
  86.             $pk $writeResult->getPrimaryKey();
  87.             $ids[] = $pk['customerGroupId'];
  88.         }
  89.         if (\count($ids) === 0) {
  90.             return;
  91.         }
  92.         $criteria = new Criteria();
  93.         $criteria->addFilter(new EqualsAnyFilter('foreignKey'$ids));
  94.         $criteria->addFilter(new EqualsFilter('routeName'self::ROUTE_NAME));
  95.         /** @var array<string> $ids */
  96.         $ids array_values($this->seoUrlRepository->searchIds($criteria$event->getContext())->getIds());
  97.         if (\count($ids) === 0) {
  98.             return;
  99.         }
  100.         $this->seoUrlRepository->delete(array_map(function (string $id) {
  101.             return ['id' => $id];
  102.         }, $ids), $event->getContext());
  103.     }
  104.     /**
  105.      * @param list<string> $ids
  106.      */
  107.     private function createUrls(array $idsContext $context): void
  108.     {
  109.         $criteria = new Criteria($ids);
  110.         $criteria->addFilter(new EqualsFilter('registrationActive'true));
  111.         $criteria->addAssociation('registrationSalesChannels.languages');
  112.         $criteria->addAssociation('translations');
  113.         /** @var CustomerGroupCollection $groups */
  114.         $groups $this->customerGroupRepository->search($criteria$context)->getEntities();
  115.         $buildUrls = [];
  116.         foreach ($groups as $group) {
  117.             if ($group->getRegistrationSalesChannels() === null) {
  118.                 continue;
  119.             }
  120.             foreach ($group->getRegistrationSalesChannels() as $registrationSalesChannel) {
  121.                 if ($registrationSalesChannel->getLanguages() === null) {
  122.                     continue;
  123.                 }
  124.                 /** @var array<string> $languageIds */
  125.                 $languageIds $registrationSalesChannel->getLanguages()->getIds();
  126.                 $criteria = new Criteria($languageIds);
  127.                 /** @var LanguageCollection $languageCollection */
  128.                 $languageCollection $this->languageRepository->search($criteria$context)->getEntities();
  129.                 foreach ($languageIds as $languageId) {
  130.                     /** @var LanguageEntity $language */
  131.                     $language $languageCollection->get($languageId);
  132.                     $title $this->getTranslatedTitle($group->getTranslations(), $language);
  133.                     if (!isset($buildUrls[$languageId])) {
  134.                         $buildUrls[$languageId] = [
  135.                             'urls' => [],
  136.                             'salesChannel' => $registrationSalesChannel,
  137.                         ];
  138.                     }
  139.                     $buildUrls[$languageId]['urls'][] = [
  140.                         'salesChannelId' => $registrationSalesChannel->getId(),
  141.                         'foreignKey' => $group->getId(),
  142.                         'routeName' => self::ROUTE_NAME,
  143.                         'pathInfo' => '/customer-group-registration/' $group->getId(),
  144.                         'isCanonical' => true,
  145.                         'seoPathInfo' => '/' $this->slugify->slugify($title),
  146.                     ];
  147.                 }
  148.             }
  149.         }
  150.         foreach ($buildUrls as $languageId => $config) {
  151.             $context = new Context(
  152.                 $context->getSource(),
  153.                 $context->getRuleIds(),
  154.                 $context->getCurrencyId(),
  155.                 [$languageId]
  156.             );
  157.             $this->persister->updateSeoUrls(
  158.                 $context,
  159.                 self::ROUTE_NAME,
  160.                 array_column($config['urls'], 'foreignKey'),
  161.                 $config['urls'],
  162.                 $config['salesChannel']
  163.             );
  164.         }
  165.     }
  166.     private function getTranslatedTitle(?CustomerGroupTranslationCollection $translationsLanguageEntity $language): string
  167.     {
  168.         if ($translations === null) {
  169.             return '';
  170.         }
  171.         // Requested translation
  172.         foreach ($translations as $translation) {
  173.             if ($translation->getLanguageId() === $language->getId() && $translation->getRegistrationTitle() !== null) {
  174.                 return $translation->getRegistrationTitle();
  175.             }
  176.         }
  177.         // Inherited translation
  178.         foreach ($translations as $translation) {
  179.             if ($translation->getLanguageId() === $language->getParentId() && $translation->getRegistrationTitle() !== null) {
  180.                 return $translation->getRegistrationTitle();
  181.             }
  182.         }
  183.         // System Language
  184.         foreach ($translations as $translation) {
  185.             if ($translation->getLanguageId() === Defaults::LANGUAGE_SYSTEM && $translation->getRegistrationTitle() !== null) {
  186.                 return $translation->getRegistrationTitle();
  187.             }
  188.         }
  189.         return '';
  190.     }
  191. }