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

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\EntityRepository;
  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\System\User\Aggregate\UserConfig\UserConfigCollection;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class SalesChannelUserConfigSubscriber implements EventSubscriberInterface
  11. {
  12.     public const CONFIG_KEY 'sales-channel-favorites';
  13.     private EntityRepository $userConfigRepository;
  14.     public function __construct(EntityRepository $userConfigRepository)
  15.     {
  16.         $this->userConfigRepository $userConfigRepository;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             'sales_channel.deleted' => 'onSalesChannelDeleted',
  22.         ];
  23.     }
  24.     public function onSalesChannelDeleted(EntityDeletedEvent $deletedEvent): void
  25.     {
  26.         $context $deletedEvent->getContext();
  27.         $deletedSalesChannelIds $deletedEvent->getIds();
  28.         $writeUserConfigs = [];
  29.         foreach ($this->getAllFavoriteUserConfigs($context) as $userConfigEntity) {
  30.             $salesChannelIds $userConfigEntity->getValue();
  31.             if ($salesChannelIds === null) {
  32.                 continue;
  33.             }
  34.             // Find matching IDs
  35.             $matchingIds array_intersect($deletedSalesChannelIds$salesChannelIds);
  36.             if (!$matchingIds) {
  37.                 continue;
  38.             }
  39.             // Removes the IDs from $matchingIds from the array
  40.             $newUserConfigArray array_diff($salesChannelIds$matchingIds);
  41.             $writeUserConfigs[] = [
  42.                 'id' => $userConfigEntity->getId(),
  43.                 'value' => array_values($newUserConfigArray),
  44.             ];
  45.         }
  46.         $this->userConfigRepository->upsert($writeUserConfigs$context);
  47.     }
  48.     private function getAllFavoriteUserConfigs(Context $context): UserConfigCollection
  49.     {
  50.         $criteria = new Criteria();
  51.         $criteria->addFilter(new EqualsFilter('key'self::CONFIG_KEY));
  52.         /** @var UserConfigCollection $result */
  53.         $result $this->userConfigRepository->search($criteria$context)->getEntities();
  54.         return $result;
  55.     }
  56. }