custom/plugins/SwagSocialShopping/src/EventListener/ProductStreamEventListener.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace SwagSocialShopping\EventListener;
  9. use Shopware\Core\Content\ProductStream\ProductStreamDefinition;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Validator\ConstraintViolation;
  20. use Symfony\Component\Validator\ConstraintViolationInterface;
  21. use Symfony\Component\Validator\ConstraintViolationList;
  22. class ProductStreamEventListener implements EventSubscriberInterface
  23. {
  24.     private EntityRepositoryInterface $socialShoppingRepository;
  25.     public function __construct(EntityRepositoryInterface $socialShoppingRepository)
  26.     {
  27.         $this->socialShoppingRepository $socialShoppingRepository;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             PreWriteValidationEvent::class => 'preValidate',
  33.         ];
  34.     }
  35.     public function preValidate(PreWriteValidationEvent $event): void
  36.     {
  37.         $violations = new ConstraintViolationList();
  38.         foreach ($event->getCommands() as $command) {
  39.             if (!($command instanceof DeleteCommand) || $command->getDefinition()->getClass() !== ProductStreamDefinition::class) {
  40.                 continue;
  41.             }
  42.             $byteId $command->getPrimaryKey()['id'];
  43.             $hexId \mb_strtolower(Uuid::fromBytesToHex($byteId));
  44.             if (!$this->isProductStreamUsedBySocialShopping($hexId$event->getContext())) {
  45.                 continue;
  46.             }
  47.             $violations->add(
  48.                 $this->buildViolation(
  49.                     'The product stream {{ id }} is used by one or more social shopping sales channels.',
  50.                     ['{{ id }}' => $hexId]
  51.                 )
  52.             );
  53.         }
  54.         if ($violations->count() > 0) {
  55.             $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  56.         }
  57.     }
  58.     private function isProductStreamUsedBySocialShopping(string $productStreamIdContext $context): bool
  59.     {
  60.         $criteria = new Criteria();
  61.         $criteria->addFilter(new EqualsFilter('productStreamId'$productStreamId));
  62.         $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
  63.         return $this->socialShoppingRepository->searchIds($criteria$context)->getTotal() > 0;
  64.     }
  65.     private function buildViolation(string $messageTemplate, array $parameters): ConstraintViolationInterface
  66.     {
  67.         return new ConstraintViolation(
  68.             \str_replace(\array_keys($parameters), \array_values($parameters), $messageTemplate),
  69.             $messageTemplate,
  70.             $parameters,
  71.             null,
  72.             null,
  73.             null
  74.         );
  75.     }
  76. }