<?php
declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o.
* @link https://crehler.com/
* @support support@crehler.com
*
* @author Mateusz FlasiĆski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Catalog\Subscriber;
use Crehler\Catalog\Entity\Catalog\Aggregate\CatalogPage\CatalogPageEntity;
use Crehler\Catalog\Enums\Config\Seo;
use Crehler\Catalog\Service\Indexer\CatalogBookmarkIndexerEvent;
use Crehler\Catalog\Service\Indexer\CatalogIndexerEvent;
use Crehler\Catalog\Service\Indexer\CatalogPageIndexerEvent;
use Crehler\Catalog\Service\Media\MediaLoaderInterface;
use Crehler\Catalog\Service\Media\NextGenerationImageCreatorService;
use Shopware\Core\Content\Seo\SeoUrlUpdater;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class IndexerSubscriber implements EventSubscriberInterface
{
private SeoUrlUpdater $seoUrlUpdater;
private EntityRepositoryInterface $catalogPageRepository;
private NextGenerationImageCreatorService $nextGenerationImageCreatorService;
private MediaLoaderInterface $mediaLoader;
public function __construct(SeoUrlUpdater $seoUrlUpdater, EntityRepositoryInterface $catalogPageRepository, NextGenerationImageCreatorService $nextGenerationImageCreatorService, MediaLoaderInterface $mediaLoader)
{
$this->seoUrlUpdater = $seoUrlUpdater;
$this->catalogPageRepository = $catalogPageRepository;
$this->nextGenerationImageCreatorService = $nextGenerationImageCreatorService;
$this->mediaLoader = $mediaLoader;
}
public static function getSubscribedEvents()
{
return [
CatalogBookmarkIndexerEvent::class => 'generateBookmarks',
CatalogPageIndexerEvent::class => 'generatePage',
CatalogIndexerEvent::class => 'generateCatalog',
];
}
public function generateBookmarks(CatalogBookmarkIndexerEvent $event): void
{
$this->seoUrlUpdater->update(Seo::ROUTE_BOOKMARK, $event->getIds());
}
public function generatePage(CatalogPageIndexerEvent $event): void
{
try {
$this->seoUrlUpdater->update(Seo::ROUTE_PAGE, $event->getIds());
} catch (\Throwable $e) {
}
foreach ($event->getIds() as $id) {
$criteria = new Criteria([$id]);
$criteria->addAssociation('media');
/** @var CatalogPageEntity|null $catalogPage */
$catalogPage = $this->catalogPageRepository->search($criteria, $event->getContext())->first();
if ($catalogPage !== null && $catalogPage->getMedia() !== null) {
$this->mediaLoader->loadMediaStruct($catalogPage->getMedia());
// $this->nextGenerationImageCreatorService->prepare($catalogPage->getMediaId());
}
}
}
public function generateCatalog(CatalogIndexerEvent $event): void
{
$this->seoUrlUpdater->update(Seo::ROUTE_CATALOG, $event->getIds());
}
}