custom/plugins/SwagEnterpriseSearchPlatform/src/Boosting/DataAbstractionLayer/BoostingDeletionSubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\EnterpriseSearch\Boosting\DataAbstractionLayer;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Swag\EnterpriseSearch\Boosting\BoostingDefinition;
  10. use Swag\EnterpriseSearch\Boosting\BoostingEntity;
  11. use Swag\EnterpriseSearch\Boosting\EntityStream\Service\EntityStreamResolver;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class BoostingDeletionSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EntityRepositoryInterface
  17.      */
  18.     private $entityStreamRepository;
  19.     /**
  20.      * @var EntityRepositoryInterface
  21.      */
  22.     private $boostingRepository;
  23.     public function __construct(
  24.         EntityRepositoryInterface $entityStreamRepository,
  25.         EntityRepositoryInterface $boostingRepository
  26.     ) {
  27.         $this->entityStreamRepository $entityStreamRepository;
  28.         $this->boostingRepository $boostingRepository;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             PreWriteValidationEvent::class => 'checkAndDeleteEntityStream',
  34.         ];
  35.     }
  36.     public function checkAndDeleteEntityStream(PreWriteValidationEvent $preWriteValidationEvent): void
  37.     {
  38.         $commands $preWriteValidationEvent->getCommands();
  39.         $boostingIds $this->getBoostingIdsFromDeleteCommands($commands);
  40.         if (!$boostingIds) {
  41.             return;
  42.         }
  43.         $boostingIds Uuid::fromBytesToHexList($boostingIds);
  44.         $idsToDelete $this->getEntityStreamIdsFromBoostingIds($boostingIds$preWriteValidationEvent);
  45.         if (!$idsToDelete) {
  46.             return;
  47.         }
  48.         $this->entityStreamRepository->delete($idsToDelete$preWriteValidationEvent->getContext());
  49.     }
  50.     /**
  51.      * @param WriteCommand[] $commands
  52.      */
  53.     private function getBoostingIdsFromDeleteCommands(array $commands): array
  54.     {
  55.         $boostingIds = [];
  56.         foreach ($commands as $command) {
  57.             if (!$command instanceof DeleteCommand) {
  58.                 continue;
  59.             }
  60.             if (!$command->getDefinition() instanceof BoostingDefinition) {
  61.                 continue;
  62.             }
  63.             $boostingIds array_merge($command->getPrimaryKey(), $boostingIds);
  64.         }
  65.         return $boostingIds;
  66.     }
  67.     /**
  68.      * @param string[] $boostingIds
  69.      */
  70.     private function getEntityStreamIdsFromBoostingIds(
  71.         array $boostingIds,
  72.         PreWriteValidationEvent $preWriteValidationEvent
  73.     ): array {
  74.         $criteria = new Criteria($boostingIds);
  75.         $boostingCollection $this->boostingRepository->search($criteria$preWriteValidationEvent->getContext());
  76.         $idsToDelete = [];
  77.         /** @var BoostingEntity $boosting */
  78.         foreach ($boostingCollection->getElements() as $boosting) {
  79.             if ($boosting->getType() !== EntityStreamResolver::TYPE) {
  80.                 continue;
  81.             }
  82.             $idsToDelete[] = ['id' => $boosting->getTypeId()];
  83.         }
  84.         return $idsToDelete;
  85.     }
  86. }