vendor/crehler/advanced-sort/src/Subscriber/IndexerSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright 2023 Crehler Sp. z o.o.
  4.  *
  5.  * https://crehler.com/
  6.  * support@crehler.com
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Crehler\AdvancedSort\Subscriber;
  12. use Crehler\AdvancedSort\Services\Indexer\CategoryItemsUpdaterInterface;
  13. use Crehler\AdvancedSort\Services\PropagatePositionService;
  14. use Doctrine\DBAL\Connection;
  15. use Doctrine\DBAL\Driver\Exception;
  16. use Monolog\Logger;
  17. use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
  18. use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
  19. use Shopware\Core\Framework\Context;
  20. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  21. use Shopware\Core\Framework\Uuid\Uuid;
  22. use Shopware\Core\Profiling\Profiler;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. class IndexerSubscriber implements EventSubscriberInterface
  25. {
  26.     private EntityRepository $productRepository;
  27.     private Logger $crehlerAdvancedSortLogger;
  28.     private CategoryItemsUpdaterInterface $categoryItemsUpdater;
  29.     private Connection $connection;
  30.     private PropagatePositionService $propagatePositionService;
  31.     public function __construct(EntityRepository $productRepositoryLogger $crehlerAdvancedSortLoggerCategoryItemsUpdaterInterface $categoryItemsUpdaterConnection $connectionPropagatePositionService $propagatePositionService)
  32.     {
  33.         $this->productRepository $productRepository;
  34.         $this->crehlerAdvancedSortLogger $crehlerAdvancedSortLogger;
  35.         $this->categoryItemsUpdater $categoryItemsUpdater;
  36.         $this->connection $connection;
  37.         $this->propagatePositionService =$propagatePositionService;
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42. //            CategoryIndexerEvent::class => 'onCategoryIndexer',
  43.             ProductIndexerEvent::class => 'onProductIndexer',
  44.         ];
  45.     }
  46.     public function onCategoryIndexer(CategoryIndexerEvent $event)
  47.     {
  48.         $this->crehlerAdvancedSortLogger->info('Indexer Subscriber onCategoryIndexer');
  49.         Profiler::trace('crehler-advanced-sort::indekser::category', function () use ($event) {
  50.             $this->updateByCategory($event->getIds(), $event->getContext());
  51.         });
  52. //        $this->propagatePositionService->inheritPositionsOnIndexer($event->getIds());
  53.     }
  54.     /**
  55.      * Błędne założenie, jeśli ustawimy sortowanie dla root kategorii to przy każdym produkcie będzie się wykonywać na tej kategorii
  56.      *
  57.      * @throws Exception
  58.      * @throws \Doctrine\DBAL\Exception
  59.      */
  60.     public function onProductIndexer(ProductIndexerEvent $event)
  61.     {
  62.         return;
  63.         $this->crehlerAdvancedSortLogger->info('Indexer Subscriber onProductIndexer');
  64.         Profiler::trace('crehler-advanced-sort::indekser::product', function () use ($event) {
  65.             $categories = [];
  66.             foreach ($event->getIds() as $id) {
  67.                 $categories array_merge($categories$this->getCategoriesByProduct($id));
  68.             }
  69.             $this->updateByCategory($categories$event->getContext());
  70.         });
  71.     }
  72.     private function updateByCategory(array $categoriesContext $context)
  73.     {
  74.         $categories array_filter(array_unique($categories));
  75.         foreach ($categories as $category) {
  76.             $this->crehlerAdvancedSortLogger->info('Indexer Subscriber updateByCategory ' $category);
  77.         }
  78.         $this->categoryItemsUpdater->update($categories$context);
  79.     }
  80.     /**
  81.      * @throws Exception
  82.      * @throws \Doctrine\DBAL\Exception
  83.      */
  84.     private function getCategoriesByProduct(string $productId): array
  85.     {
  86.         try {
  87.             $result $this->connection
  88.                 ->createQueryBuilder()
  89.                 ->select('LOWER(HEX(category_id))')
  90.                 ->from('product_category')
  91.                 ->where('product_id = :productId')
  92.                 ->setParameter('productId'Uuid::fromHexToBytes($productId))
  93.                 ->execute()
  94.                 ->fetchFirstColumn();
  95.         } catch (\Throwable $exception) {
  96.             return [];
  97.         }
  98.         return $result ?? [];
  99.     }
  100. }