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

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