platform/src/Core/Checkout/Customer/Subscriber/CustomerGroupSubscriber.php line 57

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\LanguageEntity;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class CustomerGroupSubscriber implements EventSubscriberInterface
  18. {
  19.     private const ROUTE_NAME 'frontend.account.customer-group-registration.page';
  20.     private EntityRepositoryInterface $customerGroupRepository;
  21.     private SeoUrlPersister $persister;
  22.     private SlugifyInterface $slugify;
  23.     private EntityRepositoryInterface $seoUrlRepository;
  24.     private EntityRepositoryInterface $languageRepository;
  25.     public function __construct(
  26.         EntityRepositoryInterface $customerGroupRepository,
  27.         EntityRepositoryInterface $seoUrlRepository,
  28.         EntityRepositoryInterface $languageRepository,
  29.         SeoUrlPersister $persister,
  30.         SlugifyInterface $slugify
  31.     ) {
  32.         $this->customerGroupRepository $customerGroupRepository;
  33.         $this->seoUrlRepository $seoUrlRepository;
  34.         $this->persister $persister;
  35.         $this->slugify $slugify;
  36.         $this->languageRepository $languageRepository;
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             'customer_group_translation.written' => 'updatedCustomerGroup',
  42.             'customer_group_registration_sales_channels.written' => 'newSalesChannelAddedToCustomerGroup',
  43.             'customer_group_translation.deleted' => 'deleteCustomerGroup',
  44.         ];
  45.     }
  46.     public function newSalesChannelAddedToCustomerGroup(EntityWrittenEvent $event): void
  47.     {
  48.         $ids = [];
  49.         foreach ($event->getWriteResults() as $writeResult) {
  50.             $ids[] = $writeResult->getPrimaryKey()['customerGroupId'];
  51.         }
  52.         if (\count($ids) === 0) {
  53.             return;
  54.         }
  55.         $this->createUrls($ids$event->getContext());
  56.     }
  57.     public function updatedCustomerGroup(EntityWrittenEvent $event): void
  58.     {
  59.         $ids = [];
  60.         foreach ($event->getWriteResults() as $writeResult) {
  61.             if ($writeResult->hasPayload('registrationTitle')) {
  62.                 $ids[] = $writeResult->getPrimaryKey()['customerGroupId'];
  63.             }
  64.         }
  65.         if (\count($ids) === 0) {
  66.             return;
  67.         }
  68.         $this->createUrls($ids$event->getContext());
  69.     }
  70.     public function deleteCustomerGroup(EntityDeletedEvent $event): void
  71.     {
  72.         $ids = [];
  73.         foreach ($event->getWriteResults() as $writeResult) {
  74.             $ids[] = $writeResult->getPrimaryKey()['customerGroupId'];
  75.         }
  76.         if (\count($ids) === 0) {
  77.             return;
  78.         }
  79.         $criteria = new Criteria();
  80.         $criteria->addFilter(new EqualsAnyFilter('foreignKey'$ids));
  81.         $criteria->addFilter(new EqualsFilter('routeName'self::ROUTE_NAME));
  82.         $ids array_values($this->seoUrlRepository->searchIds($criteria$event->getContext())->getIds());
  83.         if (\count($ids) === 0) {
  84.             return;
  85.         }
  86.         $this->seoUrlRepository->delete(array_map(function (string $id) {
  87.             return ['id' => $id];
  88.         }, $ids), $event->getContext());
  89.     }
  90.     private function createUrls(array $idsContext $context): void
  91.     {
  92.         $criteria = new Criteria($ids);
  93.         $criteria->addFilter(new EqualsFilter('registrationActive'true));
  94.         $criteria->addAssociation('registrationSalesChannels.languages');
  95.         $criteria->addAssociation('translations');
  96.         /** @var CustomerGroupCollection $groups */
  97.         $groups $this->customerGroupRepository->search($criteria$context)->getEntities();
  98.         $buildUrls = [];
  99.         foreach ($groups as $group) {
  100.             if ($group->getRegistrationSalesChannels() === null) {
  101.                 continue;
  102.             }
  103.             foreach ($group->getRegistrationSalesChannels() as $registrationSalesChannel) {
  104.                 if ($registrationSalesChannel->getLanguages() === null) {
  105.                     continue;
  106.                 }
  107.                 $languageIds $registrationSalesChannel->getLanguages()->getIds();
  108.                 $criteria = new Criteria($languageIds);
  109.                 $languageCollection $this->languageRepository->search($criteria$context)->getEntities();
  110.                 foreach ($languageIds as $languageId) {
  111.                     $title $this->getTranslatedTitle($group->getTranslations(), $languageCollection->get($languageId));
  112.                     if (!isset($buildUrls[$languageId])) {
  113.                         $buildUrls[$languageId] = [
  114.                             'urls' => [],
  115.                             'salesChannel' => $registrationSalesChannel,
  116.                         ];
  117.                     }
  118.                     $buildUrls[$languageId]['urls'][] = [
  119.                         'salesChannelId' => $registrationSalesChannel->getId(),
  120.                         'foreignKey' => $group->getId(),
  121.                         'routeName' => self::ROUTE_NAME,
  122.                         'pathInfo' => '/customer-group-registration/' $group->getId(),
  123.                         'isCanonical' => true,
  124.                         'seoPathInfo' => '/' $this->slugify->slugify($title),
  125.                     ];
  126.                 }
  127.             }
  128.         }
  129.         foreach ($buildUrls as $languageId => $config) {
  130.             $context = new Context(
  131.                 $context->getSource(),
  132.                 $context->getRuleIds(),
  133.                 $context->getCurrencyId(),
  134.                 [$languageId]
  135.             );
  136.             $this->persister->updateSeoUrls(
  137.                 $context,
  138.                 self::ROUTE_NAME,
  139.                 array_column($config['urls'], 'foreignKey'),
  140.                 $config['urls'],
  141.                 $config['salesChannel']
  142.             );
  143.         }
  144.     }
  145.     private function getTranslatedTitle(?CustomerGroupTranslationCollection $translationsLanguageEntity $language): string
  146.     {
  147.         if ($translations === null) {
  148.             return '';
  149.         }
  150.         // Requested translation
  151.         foreach ($translations as $translation) {
  152.             if ($translation->getLanguageId() === $language->getId() && $translation->getRegistrationTitle() !== null) {
  153.                 return $translation->getRegistrationTitle();
  154.             }
  155.         }
  156.         // Inherited translation
  157.         foreach ($translations as $translation) {
  158.             if ($translation->getLanguageId() === $language->getParentId() && $translation->getRegistrationTitle() !== null) {
  159.                 return $translation->getRegistrationTitle();
  160.             }
  161.         }
  162.         // System Language
  163.         foreach ($translations as $translation) {
  164.             if ($translation->getLanguageId() === Defaults::LANGUAGE_SYSTEM && $translation->getRegistrationTitle() !== null) {
  165.                 return $translation->getRegistrationTitle();
  166.             }
  167.         }
  168.         return '';
  169.     }
  170. }