vendor/crehler/inspirations/src/Service/InspirationService.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   10 sie 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\Inspirations\Service;
  13. use Crehler\Arrangements\Entity\Arrangement\ArrangementEntity;
  14. use Crehler\Inspirations\Entity\Inspiration\Aggregate\InspirationProduct\InspirationProductCollection;
  15. use Crehler\Inspirations\Entity\Inspiration\Aggregate\InspirationProduct\InspirationProductEntity;
  16. use Crehler\Inspirations\Entity\Inspiration\InspirationEntity;
  17. use Crehler\Tools\Enum\LoaderFlags;
  18. use Crehler\Tools\Product\ProductLoaderInterface;
  19. use Shopware\Core\Framework\Context;
  20. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  21. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  24. class InspirationService
  25. {
  26.     private EntityRepository $inspirationRepository;
  27.     private ProductLoaderInterface $productLoader;
  28.     public function __construct(
  29.         EntityRepository $inspirationRepository,
  30.         ProductLoaderInterface $productLoader
  31.     )
  32.     {
  33.         $this->productLoader $productLoader;
  34.         $this->inspirationRepository $inspirationRepository;
  35.     }
  36.     public function getInspirationById(string $inspirationIdContext $context): ?InspirationEntity
  37.     {
  38.         $criteria = new Criteria([$inspirationId]);
  39.         $criteria->addAssociation('media')
  40.             ->addAssociation('cover')
  41.             ->addAssociation('inspirationProducts');
  42.         $criteria->getAssociation('inspirationProducts')->addSorting(new FieldSorting('createdAt','ASC'));
  43.         $criteria->getAssociation('inspirationProducts')->addAssociation('product.options.group');
  44.         $criteria->getAssociation('media')->addSorting(new FieldSorting('position'));
  45.         $criteria->getAssociation('inspirationProducts.product')->addAssociation('cover')->addAssociation('media');
  46.         /** @var InspirationEntity $inspiration */
  47.         $inspiration =  $this->inspirationRepository->search($criteria$context)->first();
  48.         /** @var InspirationProductEntity $inspirationProduct */
  49.         foreach ($inspiration->getInspirationProducts() as $inspirationProduct){
  50.             $inspirationProduct->setProduct($this->productLoader->load(
  51.                 $inspirationProduct->getProductId(),
  52.                 null,
  53.                 LoaderFlags::SkipPageLoading,
  54.                 LoaderFlags::SkipSeoCategory,
  55.                 LoaderFlags::IgnoreHideCloseoutProductsWhenOutOfStock)
  56.                 ->first()); ;
  57.         }
  58.         return $inspiration;
  59.     }
  60. }