vendor/shopware/administration/System/SalesChannel/Subscriber/SalesChannelUserConfigSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Administration\System\SalesChannel\Subscriber;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\System\User\Aggregate\UserConfig\UserConfigCollection;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  13.  */
  14. #[Package('system-settings')]
  15. class SalesChannelUserConfigSubscriber implements EventSubscriberInterface
  16. {
  17.     public const CONFIG_KEY 'sales-channel-favorites';
  18.     private EntityRepositoryInterface $userConfigRepository;
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(EntityRepositoryInterface $userConfigRepository)
  23.     {
  24.         $this->userConfigRepository $userConfigRepository;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             'sales_channel.deleted' => 'onSalesChannelDeleted',
  30.         ];
  31.     }
  32.     public function onSalesChannelDeleted(EntityDeletedEvent $deletedEvent): void
  33.     {
  34.         $context $deletedEvent->getContext();
  35.         $deletedSalesChannelIds $deletedEvent->getIds();
  36.         $writeUserConfigs = [];
  37.         foreach ($this->getAllFavoriteUserConfigs($context) as $userConfigEntity) {
  38.             $salesChannelIds $userConfigEntity->getValue();
  39.             if ($salesChannelIds === null) {
  40.                 continue;
  41.             }
  42.             // Find matching IDs
  43.             $matchingIds array_intersect($deletedSalesChannelIds$salesChannelIds);
  44.             if (!$matchingIds) {
  45.                 continue;
  46.             }
  47.             // Removes the IDs from $matchingIds from the array
  48.             $newUserConfigArray array_diff($salesChannelIds$matchingIds);
  49.             $writeUserConfigs[] = [
  50.                 'id' => $userConfigEntity->getId(),
  51.                 'value' => array_values($newUserConfigArray),
  52.             ];
  53.         }
  54.         $this->userConfigRepository->upsert($writeUserConfigs$context);
  55.     }
  56.     private function getAllFavoriteUserConfigs(Context $context): UserConfigCollection
  57.     {
  58.         $criteria = new Criteria();
  59.         $criteria->addFilter(new EqualsFilter('key'self::CONFIG_KEY));
  60.         /** @var UserConfigCollection $result */
  61.         $result $this->userConfigRepository->search($criteria$context)->getEntities();
  62.         return $result;
  63.     }
  64. }