vendor/crehler/mojebambino-catalog/src/Service/CatalogReader/Loader/TableOfContentsLoader.php line 56

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\Service\CatalogReader\Loader;
  14. use Crehler\Catalog\Entity\Catalog\Aggregate\CatalogPage\CatalogPageEntity;
  15. use Crehler\Catalog\Service\CatalogReader\UrlGenerator;
  16. use Crehler\Catalog\Service\Media\MediaLoaderInterface;
  17. use Crehler\Catalog\Struct\CatalogMedia\CatalogMedia;
  18. use Crehler\Catalog\Struct\TableOfContentsElementStruct;
  19. use Crehler\Catalog\Struct\TableOfContentsStruct;
  20. use Shopware\Core\Content\Media\MediaEntity;
  21. use Shopware\Core\Content\Media\Pathname\UrlGeneratorInterface;
  22. use Shopware\Core\Framework\Context;
  23. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  24. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  25. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  26. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  27. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  28. class TableOfContentsLoader
  29. {
  30.     private EntityRepositoryInterface $catalogPageRepository;
  31.     private UrlGenerator $urlGenerator;
  32.     private UrlGeneratorInterface $shopwareUrlGenerator;
  33.     private MediaLoaderInterface $mediaLoaderService;
  34.     public function __construct(EntityRepositoryInterface $catalogPageRepositoryUrlGenerator $urlGeneratorUrlGeneratorInterface $shopwareUrlGeneratorMediaLoaderInterface $mediaLoaderService)
  35.     {
  36.         $this->catalogPageRepository $catalogPageRepository;
  37.         $this->urlGenerator $urlGenerator;
  38.         $this->shopwareUrlGenerator $shopwareUrlGenerator;
  39.         $this->mediaLoaderService $mediaLoaderService;
  40.     }
  41.     public function load(string $catalogIdSalesChannelContext $salesChannelContextint $offset 0int $limit 50): TableOfContentsStruct
  42.     {
  43.         $criteria = new Criteria();
  44.         $criteria->addFilter(new EqualsFilter('catalogId'$catalogId));
  45.         $criteria->addSorting(new FieldSorting('pageNumber''ASC'));
  46.         $criteria->setOffset($offset);
  47.         $criteria->setLimit($limit);
  48.         $criteria->addAssociations(['media''bookmarks''media.thumbnails']);
  49.         $pages $this->catalogPageRepository->search($criteria$salesChannelContext->getContext());
  50.         $total $this->countPages($catalogId$salesChannelContext->getContext());
  51.         $struct = new TableOfContentsStruct();
  52.         $struct->setLimit($limit)
  53.             ->setTotal($total)
  54.             ->setOffset($offset);
  55.         foreach ($pages->getEntities() as $entity) {
  56.             $struct->getContents()->append($this->factorElement($entity$salesChannelContext));
  57.         }
  58.         return $struct;
  59.     }
  60.     private function countPages(string $catalogIdContext $context): int
  61.     {
  62.         $criteria = new Criteria();
  63.         $criteria->addFilter(new EqualsFilter('catalogId'$catalogId));
  64.         return $this->catalogPageRepository->searchIds($criteria$context)->getTotal();
  65.     }
  66.     private function factorElement(CatalogPageEntity $catalogPageEntitySalesChannelContext $salesChannelContext): TableOfContentsElementStruct
  67.     {
  68.         $struct = new TableOfContentsElementStruct();
  69.         $struct->setId($catalogPageEntity->getId());
  70.         $struct->setPageNumber($catalogPageEntity->getPageDisplayNumber());
  71.         $struct->setUrl($this->urlGenerator->generateDetailurl($catalogPageEntity$salesChannelContext)); //TODO bookmark or home
  72.         $struct->setRealPageNumber($catalogPageEntity->getPageNumber());
  73.         return $this->insertMediaToStruct($struct$catalogPageEntity->getMedia());
  74.     }
  75.     private function insertMediaToStruct(TableOfContentsElementStruct $structMediaEntity $mediaEntity): TableOfContentsElementStruct
  76.     {
  77.         $media $this->mediaLoaderService->loadMediaStruct($mediaEntity)->getItems();
  78.         if (array_key_exists('300'$media)) {
  79.             /** @var CatalogMedia $files */
  80.             $files $media['300'];
  81.             if ($files->getPng() === null) {
  82.                 $files $media['original'];
  83.             }
  84.         } else {
  85.             /** @var CatalogMedia $files */
  86.             $files $media['original'];
  87.         }
  88.         if ($files->getPng() === null) {
  89.             return $struct;
  90.         }
  91.         $struct->setWebp($files->getWebp());
  92.         $struct->setAvif($files->getAvif());
  93.         $struct->setPng($files->getPng());
  94.         return $struct;
  95.     }
  96. }