<?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\Service\CatalogReader\Loader;
use Crehler\Catalog\Entity\Catalog\Aggregate\CatalogPage\CatalogPageEntity;
use Crehler\Catalog\Service\CatalogReader\UrlGenerator;
use Crehler\Catalog\Service\Media\MediaLoaderInterface;
use Crehler\Catalog\Struct\CatalogMedia\CatalogMedia;
use Crehler\Catalog\Struct\TableOfContentsElementStruct;
use Crehler\Catalog\Struct\TableOfContentsStruct;
use Shopware\Core\Content\Media\MediaEntity;
use Shopware\Core\Content\Media\Pathname\UrlGeneratorInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
class TableOfContentsLoader
{
private EntityRepositoryInterface $catalogPageRepository;
private UrlGenerator $urlGenerator;
private UrlGeneratorInterface $shopwareUrlGenerator;
private MediaLoaderInterface $mediaLoaderService;
public function __construct(EntityRepositoryInterface $catalogPageRepository, UrlGenerator $urlGenerator, UrlGeneratorInterface $shopwareUrlGenerator, MediaLoaderInterface $mediaLoaderService)
{
$this->catalogPageRepository = $catalogPageRepository;
$this->urlGenerator = $urlGenerator;
$this->shopwareUrlGenerator = $shopwareUrlGenerator;
$this->mediaLoaderService = $mediaLoaderService;
}
public function load(string $catalogId, SalesChannelContext $salesChannelContext, int $offset = 0, int $limit = 50): TableOfContentsStruct
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('catalogId', $catalogId));
$criteria->addSorting(new FieldSorting('pageNumber', 'ASC'));
$criteria->setOffset($offset);
$criteria->setLimit($limit);
$criteria->addAssociations(['media', 'bookmarks', 'media.thumbnails']);
$pages = $this->catalogPageRepository->search($criteria, $salesChannelContext->getContext());
$total = $this->countPages($catalogId, $salesChannelContext->getContext());
$struct = new TableOfContentsStruct();
$struct->setLimit($limit)
->setTotal($total)
->setOffset($offset);
foreach ($pages->getEntities() as $entity) {
$struct->getContents()->append($this->factorElement($entity, $salesChannelContext));
}
return $struct;
}
private function countPages(string $catalogId, Context $context): int
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('catalogId', $catalogId));
return $this->catalogPageRepository->searchIds($criteria, $context)->getTotal();
}
private function factorElement(CatalogPageEntity $catalogPageEntity, SalesChannelContext $salesChannelContext): TableOfContentsElementStruct
{
$struct = new TableOfContentsElementStruct();
$struct->setId($catalogPageEntity->getId());
$struct->setPageNumber($catalogPageEntity->getPageDisplayNumber());
$struct->setUrl($this->urlGenerator->generateDetailurl($catalogPageEntity, $salesChannelContext)); //TODO bookmark or home
$struct->setRealPageNumber($catalogPageEntity->getPageNumber());
return $this->insertMediaToStruct($struct, $catalogPageEntity->getMedia());
}
private function insertMediaToStruct(TableOfContentsElementStruct $struct, MediaEntity $mediaEntity): TableOfContentsElementStruct
{
$media = $this->mediaLoaderService->loadMediaStruct($mediaEntity)->getItems();
if (array_key_exists('300', $media)) {
/** @var CatalogMedia $files */
$files = $media['300'];
if ($files->getPng() === null) {
$files = $media['original'];
}
} else {
/** @var CatalogMedia $files */
$files = $media['original'];
}
if ($files->getPng() === null) {
return $struct;
}
$struct->setWebp($files->getWebp());
$struct->setAvif($files->getAvif());
$struct->setPng($files->getPng());
return $struct;
}
}