vendor/crehler/inspirations/src/Subscriber/InspirationDeleteSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   22.01.2021
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\Inspirations\Subscriber;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class InspirationDeleteSubscriber implements EventSubscriberInterface
  19. {
  20.     private EntityRepositoryInterface $productRepository;
  21.     public function __construct(EntityRepositoryInterface $productRepository)
  22.     {
  23.         $this->productRepository $productRepository;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             'inspiration.deleted' => 'onInspirationDeleted'
  29.         ];
  30.     }
  31.     public function onInspirationDeleted(EntityDeletedEvent $event)
  32.     {
  33.         $ids $event->getIds();
  34.         $context $event->getContext();
  35.         $criteria = new Criteria();
  36.         $criteria->addFilter(new EqualsAnyFilter('inspirationId'$ids));
  37.         $productsIdsToDelete $this->productRepository->searchIds($criteria$context);
  38.         if ($productsIdsToDelete->getTotal() === 0) return;
  39.         $keys array_map(function ($id) {
  40.             return ['id' => $id];
  41.         }, $productsIdsToDelete->getIds());
  42.         $this->productRepository->delete($keys$context);
  43.     }
  44. }