vendor/crehler/mojebambino-catalog/src/Subscriber/IndexerSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @copyright 2020 Crehler Sp. z o. o.
  5.  * @link https://crehler.com/
  6.  * @support support@crehler.com
  7.  *
  8.  * @author Mateusz FlasiƄski
  9.  *
  10.  * For the full copyright and license information, please view the LICENSE
  11.  * file that was distributed with this source code.
  12.  */
  13. namespace Crehler\Catalog\Subscriber;
  14. use Crehler\Catalog\Entity\Catalog\Aggregate\CatalogPage\CatalogPageEntity;
  15. use Crehler\Catalog\Enums\Config\Seo;
  16. use Crehler\Catalog\Service\Indexer\CatalogBookmarkIndexerEvent;
  17. use Crehler\Catalog\Service\Indexer\CatalogIndexerEvent;
  18. use Crehler\Catalog\Service\Indexer\CatalogPageIndexerEvent;
  19. use Crehler\Catalog\Service\Media\MediaLoaderInterface;
  20. use Crehler\Catalog\Service\Media\NextGenerationImageCreatorService;
  21. use Shopware\Core\Content\Seo\SeoUrlUpdater;
  22. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. class IndexerSubscriber implements EventSubscriberInterface
  26. {
  27.     private SeoUrlUpdater $seoUrlUpdater;
  28.     private EntityRepositoryInterface $catalogPageRepository;
  29.     private NextGenerationImageCreatorService $nextGenerationImageCreatorService;
  30.     private MediaLoaderInterface $mediaLoader;
  31.     public function __construct(SeoUrlUpdater $seoUrlUpdaterEntityRepositoryInterface $catalogPageRepositoryNextGenerationImageCreatorService $nextGenerationImageCreatorServiceMediaLoaderInterface $mediaLoader)
  32.     {
  33.         $this->seoUrlUpdater $seoUrlUpdater;
  34.         $this->catalogPageRepository $catalogPageRepository;
  35.         $this->nextGenerationImageCreatorService $nextGenerationImageCreatorService;
  36.         $this->mediaLoader $mediaLoader;
  37.     }
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             CatalogBookmarkIndexerEvent::class => 'generateBookmarks',
  42.             CatalogPageIndexerEvent::class => 'generatePage',
  43.             CatalogIndexerEvent::class => 'generateCatalog',
  44.         ];
  45.     }
  46.     public function generateBookmarks(CatalogBookmarkIndexerEvent $event): void
  47.     {
  48.         $this->seoUrlUpdater->update(Seo::ROUTE_BOOKMARK$event->getIds());
  49.     }
  50.     public function generatePage(CatalogPageIndexerEvent $event): void
  51.     {
  52.         try {
  53.             $this->seoUrlUpdater->update(Seo::ROUTE_PAGE$event->getIds());
  54.         } catch (\Throwable $e) {
  55.         }
  56.         foreach ($event->getIds() as $id) {
  57.             $criteria = new Criteria([$id]);
  58.             $criteria->addAssociation('media');
  59.             /** @var CatalogPageEntity|null $catalogPage */
  60.             $catalogPage $this->catalogPageRepository->search($criteria$event->getContext())->first();
  61.             if ($catalogPage !== null && $catalogPage->getMedia() !== null) {
  62.                 $this->mediaLoader->loadMediaStruct($catalogPage->getMedia());
  63.                 //   $this->nextGenerationImageCreatorService->prepare($catalogPage->getMediaId());
  64.             }
  65.         }
  66.     }
  67.     public function generateCatalog(CatalogIndexerEvent $event): void
  68.     {
  69.         $this->seoUrlUpdater->update(Seo::ROUTE_CATALOG$event->getIds());
  70.     }
  71. }